Android adapter and monitor tap and scroll usage in the ListView

Source: Internet
Author: User

Package com.example.demon08;

Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import android.support.v7.app.ActionBarActivity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.AbsListView;
Import Android.widget.AbsListView.OnScrollListener;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
Import Android.widget.Toast;


public class Mainactivity extends actionbaractivity implements onitemclicklistener,onscrolllistener{

Private ListView ListView;
Private arrayadapter<string> Arr_adapter;
Private Simpleadapter Simp_adapter;
Private list<map<string,object>>datalist;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
listview= (ListView) Findviewbyid (R.id.listview);
Create a new adapter
Arrayadapter (context, the layout file that corresponds to each list item loaded by the current ListView, data Source)
Adapter Load Data Source
string[]arr_data={"Combat Tyrannosaurus rex", "Mechanical Tyrannosaurus rex", "zombie Tyrannosaurus rex", "Tyrannosaurus rex", "Agur Beast", "Ball Beast"};
Arr_adapter=new arrayadapter<string> (this,android. R.layout.simple_list_item_1,arr_data);
Simpleadapter
Context
Data source list<? Extends Map<string,?>> data A Map composed of a list collection,
Each map will go to a row in the ListView list
The key in each map (key-value pair) must contain all the keys specified in the From
Resource: The layout file ID of the list item
Key names in the From:map
ID in the bound Data view, corresponding to from
Datalist=new arraylist<map<string,object>> ();
Simp_adapter=new Simpleadapter (This,getdata (), r.layout.item,new string[]{"pic", "text"},new Int[]{r.id.pic, R.id.text});
View Load Adapter
Listview.setadapter (Arr_adapter);
Listview.setadapter (Simp_adapter);
Listview.setonitemclicklistener (this);
Listview.setonscrolllistener (this);
}


Private List<map<string,object>> GetData () {
for (int i=0;i<20;i++) {
Map<string,object> map=new hashmap<string,object> ();
Map.put ("Pic", R.drawable.ic_launcher);
Map.put ("text", "Knife Punch" +i);
Datalist.add (map);
}

return dataList;
}


@Override
public void Onscroll (Abslistview view, int firstvisibleitem,
int visibleitemcount, int totalitemcount) {
TODO auto-generated Method Stub

}


@Override
public void onscrollstatechanged (Abslistview view, int scrollstate) {
TODO auto-generated Method Stub
Switch (scrollstate) {
Case scroll_state_fling:
LOG.I ("Main", "the user has left the screen, the view still relies on inertia to continue sliding, as a result of a hard slide");
Map<string,object>map=new hashmap<string,object> ();
Map.put ("Pic", R.drawable.ic_launcher);
Map.put ("Text", "Add Item");
Datalist.add (map);
Simp_adapter.notifydatasetchanged ();
Break
Case Scroll_state_idle:
LOG.I ("Main", "View has stopped sliding");
Break
Case Scroll_state_touch_scroll:
LOG.I ("Main", "finger does not leave the screen, the view is sliding");
Break
}
}


@Override
public void Onitemclick (adapterview<?> parent, view view, int position, long ID) {
TODO auto-generated Method Stub
String text=listview.getitematposition (position) + "";
Toast.maketext (This, "position=" +position+ "text=" +text,toast.length_short). Show ();
}

}

Android adapter and monitor tap and scroll usage in the ListView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.