两种监听器:
OnItemClickListener:处理视图中单个条目的点击事件
OnScrollListener:监视滚动的变换,常用于视图在滚动中加载数据
pre.用接口方法实现
public class MainActivity extends Activity implements OnItemClickListener ,
OnScrollListener
一.点击事件监听
1.设置监听器:
lv.setOnItemClickListener(this);
2.为监听器编写方法
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String text= lv.getItemAtPosition(position)+"";
Toast.makeText(this, "position="+position+"text="+text,
Toast.LENGTH_SHORT).show();
}
二.滚动事件监听
1.设置监听器:
lv.setOnScrollListener(this);
2.为监听器编写方法
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
switch (scrollState) {
case SCROLL_STATE_FLING:
Log.i("tag", "用户手指离开屏幕后,因惯性继续滑动");
Map<String,Object>map = new HashMap<String,Object>();
map.put("pic", R.drawable.ic_launcher);
map.put("text", "增加项");
dataList.add(map);
sim_Adapter.notifyDataSetChanged();
break;
case SCROLL_STATE_IDLE:
Log.i("tag","已经停止滑动");
break;
case SCROLL_STATE_TOUCH_SCROLL:
Log.i("tag", "手指未离开屏幕,屏幕继续滑动");
break;
}
}
代码理解:
Map<String,Object>map = new HashMap<String,Object>();
map.put("pic", R.drawable.ic_launcher);
map.put("text", "增加项");
dataList.add(map);
sim_Adapter.notifyDataSetChanged();
当用户大力滑动屏幕且手指离开屏幕后屏幕仍继续滑动的,新建立一个哈希表通过map.put为其赋值,并将新的哈希表添加在datalist末尾,通过notifyDataSetChanged()方法提醒页面组件重新载入资源
网友评论