OnItemClickListener,OnScrollListener应用
OnItemClickListener:处理视图中单个条目的点击事件
OnScrollListener:监听滚动的变化,可以用于视图在滚动中加载数据
OnItemClickListener:实现AdapterView.OnItemClickListener接口,别忘记在MainActivity中调用listView.setOnItemClickListener(this);
1 private List<Map<String, Object>> getDataList() { 2 for (int i=0;i<20;i++){ 3 Map<String,Object> map=new HashMap<String,Object>(); 4 map.put("pic",R.mipmap.ic_launcher); 5 map.put("txt","慕课网"+i); 6 dataList.add(map); 7 } 8 return dataList; 9 } 10 11 @Override 12 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 13 String text=listView.getItemAtPosition(position)+"";/*获得文本信息,pisition从0开始计数*/ 14 Toast.makeText(this,"position="+position+"text="+text,Toast.LENGTH_SHORT).show(); 15 }
OnScrollListener:实现AbsListView.OnScrollListener接口,别忘记在MainActivity中调用listView.setOnScrollListener(this);
1 @Override 2 public void onScrollStateChanged(AbsListView view, int scrollState) { 3 switch (scrollState){ 4 case SCROLL_STATE_FLING: 5 Log.d("Main","用户在手指离开屏幕之前,由于用力滑一下,视图仍然以惯性继续滑动"); 6 Map<String,Object> map= new HashMap<String,Object>(); 7 map.put("pic",R.mipmap.ic_launcher); 8 map.put("txt","增加项"); 9 dataList.add(map); 10 simpleAdapter.notifyDataSetChanged();/*适配器通知ui线程进行刷新,数据刷新,可以动态的更新视图中所包含的数据*/ 11 break; 12 case SCROLL_STATE_IDLE: 13 Log.d("Main","视图停止滑动"); 14 break; 15 case SCROLL_STATE_TOUCH_SCROLL: 16 Log.d("Main","手指没有离开屏幕,视图正在滑动"); 17 break; 18 } 19 } 20 21 @Override 22 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 23 24 } 25 }