三部曲
1,新建适配器
2,给适配器添加数据
3,视图(ListView)加载适配器
- ArrayAdapter(集合或者数组)
- SimpleAdapter(特定泛型的集合)
private List<Map<String,Object>> datalists;
daralists = new ArrayList<Map<String,Object>>();
simple_adapter = new SimpleAdapter(MainActivity.this, initData(), R.layout.mylist, new String[] {"text"}, new int[] {R.id.myText});
listview.setAdapter(simple_adapter);
- OnItemClickListener(点击事件)
ListView.getItemAtPosition(position)
@return The data associated with the specified position in the list
String s = listview.getItemAtPosition(position) + " click";
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
- OnScrollListener(滚动监听)
scrollState:- SCROLL_STATE_FLING:手指离开屏幕之前,用户用力滑一下,listview根据惯性滑动
- SCROLL_STATE_IDLE:视图已经停止滑动
- SCROLL_STATE_TOUCH_SCROLL:手指没有离开屏幕,视图滑动
【notifyDatasetchanged动态更新视图中所包含的数据】
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case SCROLL_STATE_FLING:
Toast.makeText(MainActivity.this, "用力一滑", Toast.LENGTH_SHORT).show();
Map<String,Object> map = new HashMap<String, Object>();
//map.put("text", R.id.img);
map.put("text", "上拉刷新");
datalists.add(map);
simple_adapter.notifyDataSetChanged();
break;
case SCROLL_STATE_IDLE:
Toast.makeText(MainActivity.this, "停止滑动", Toast.LENGTH_SHORT).show();
break;
case SCROLL_STATE_TOUCH_SCROLL:
Toast.makeText(MainActivity.this, "正在滑动", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
有一个要记得的是,要在onCreate里面记得设置监听器的响应。listview.setOnItemClickListener(this);
listview.setOnScrollListener(this);