Android开发时,最常用的控件之一就是ListView了,而使用ListView的同时,必然需要对它设置监听器,常用的监听器有这么几个
1.OnItemClickListener
2.OnTouchListener
3.OnScrollListener
1监听器在点击该项目的子项时触发,2监听器在触碰到ListView时触发,3监听器在ListView滚动时触发,
下面几个例子分别表述了具体处理的办法
- class ItemClickEvent implements OnItemClickListener {
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- // TODO Auto-generated method stub
- // arg1.setVisibility(View.GONE);
- // 对子项处理的办法
- // BtnCheck(0);
- System.out.println(arg2);
- }
- }
- class ItemTouchEvent implements OnTouchListener {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- // System.out.println("触摸..");
- if (event.getAction() == MotionEvent.ACTION_MOVE) {
- // System.out.println("MOVE"); //接触到ListView移动时
- BtnCheck(0);
- } else if (event.getAction() == MotionEvent.ACTION_UP) {
- // System.out.println("up"); //离开ListView时
- BtnCheck(currentPage);
- } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
- // System.out.println("down"); //接触到ListView时
- BtnCheck(0);
- }
- return false;
- }
- }
- class LvScrollEvent implements OnScrollListener {
- @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 OnScrollListener.SCROLL_STATE_IDLE: //
- BtnCheck(currentPage);
- // mBusy = false;
- System.out.println("停止...");
- break;
- case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
- BtnCheck(0);
- // mBusy = true;
- System.out.println("正在滑动...");
- break;
- case OnScrollListener.SCROLL_STATE_FLING:
- BtnCheck(0);
- // mBusy = true;
- System.out.println("开始滚动...");
- break;
- }
- }
- }
有的时候,我们需要对ListView滚动做一个相应的监听事件,例如:要实现如下图通讯录的功能:
思路为:首先呢,中间那个"路"字为一个TextView,它与ListView采用相对布局的布局风格,当我们滑动ListView的时候
控制其显示,当ListView停止的时候就让其消失。
首先呢,ListView的滚动有三种状态
第一是静止状态,SCROLL_STATE_IDLE
第二是手指滚动状态,SCROLL_STATE_TOUCH_SCROLL
第三是手指不动了,但是屏幕还在滚动状态。SCROLL_STATE_FLING
下面是ListView滚动时的响应代码:
- <span style="font-size:18px">//响应ListView的滚动状态</span>
- <span style="font-size:18px">getListView().setOnScrollListener(new OnScrollListener() {
- <span style="white-space:pre"> </span>int listViewScrollState;
- /**
- * ListView的状态改变时触发
- * @param view
- * @param scrollState
- */
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- <span style="white-space:pre"> </span>listViewScrollState = scrollState;
- switch(scrollState){
- case OnScrollListener.SCROLL_STATE_IDLE://空闲状态
- <span style="white-space:pre"> </span>break;
- case OnScrollListener.SCROLL_STATE_FLING://滚动状态
- break;
- case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://触摸后滚动
- break;
- }
- }
- /**
- * 正在滚动
- * firstVisibleItem第一个Item的位置
- * visibleItemCount 可见的Item的数量
- * totalItemCount item的总数
- */
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
- //控制显示和隐藏的代码...
- }
- });</span>