Android中的ListView 可以上下滑动,并且上下活动可以分页加载数据,这一节我们看下ListView的滚动事件.
ListView的滚动事件主要通过setOnScrollListener监听器来实现,主要包括两个方法onScroll和onScrollStateChanged方法,onScrollStateChanged主要用来监听滚动状态
在滑动过程中向ListView添加数据 ,ListView滚动的时候主要有三个状态,SCROLL_STATE_TOUCH_SCROLL ,SCROLL_STATE_FLING,SCROLL_STATE_IDLE
lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3) { TextView tname= (TextView)arg1.findViewById(R.id.tvname);//名称 TextView tmodel= (TextView)arg1.findViewById(R.id.tvmodel);//规格 TextView tprice= (TextView)arg1.findViewById(R.id.tvprice);//单价 TextView tcode= (TextView)arg1.findViewById(R.id.tvcode);//编码 Toast.makeText(getApplicationContext(),"当前商品 名称:"+tname.getText()+",编码:"+tcode.getText(),30).show(); } }); lv.setOnScrollListener(new OnScrollListener(){ @Override public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void onScrollStateChanged(AbsListView arg0, int arg1) { // TODO Auto-generated method stub 滚动状态监听器 if(arg1==AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { //手指没有离开屏幕,正在滑动 Toast.makeText(getApplicationContext(),"手指没有离开屏幕,正在滑动",30).show(); Map<String,Object> map=new HashMap<String,Object>(); map.put("code","编码:10"); map.put("name","名称:Ipad"); map.put("price","价格:22"); map.put("model","单位:22"); mapList.add(map); //adp.notifyDataSetChanged(); SimpleAdapter sAdapter = (SimpleAdapter)lv.getAdapter(); sAdapter.notifyDataSetChanged(); } else if(arg1==AbsListView.OnScrollListener.SCROLL_STATE_FLING) { //手指离开屏幕之前,用力滑动了一下 Toast.makeText(getApplicationContext(),"手指离开屏幕之前,用力滑动了一下",30).show(); } else if(arg1==AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { //停止滑动 Toast.makeText(getApplicationContext(),"停止滑动",30).show(); } } });
时间: 2024-11-17 20:31:08