listview 滑动监听
2016-01-25 09:31
471 查看
// listview touch and scroll listener boolean isTabShow = true; float downY = 0; boolean isActionDown = false; private int mListViewFirstItem = -1; //listView中第一项的在屏幕中的位置 private int mScreenY = 0; private int screenHeight = 0; private int lastDirection = 0; // 0 向上滑动 1 向下滑动 View.OnTouchListener listViewOnTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: isActionDown = true; downY = event.getY(); Log.d("onTouchListener", "downY:" + downY); break; case MotionEvent.ACTION_MOVE: if (!isActionDown) { // 当为false时就触发了ACTION_MOVE,第一个action需要当成ACTION_DOWN处理 isActionDown = true; downY = event.getY(); Log.d("onTouchListener", "downY:" + downY + " no action down"); } else { float currentY = event.getY(); Log.d("onTouchListener", "downY:" + downY + " currentY::" + currentY + " currentY - downY:" + (currentY - downY)); if (currentY - downY < -20 && isTabShow) { // 向上,隐藏 isTabShow = false; Log.d("onTouchListener", "downY:" + downY + " currentY::" + currentY + " currentY - downY:" + (currentY - downY) + " hide"); } else if (currentY - downY > 20 && !isTabShow && mListViewFirstItem == 0) { isTabShow = true; Log.d("onTouchListener", "downY:" + downY + " currentY::" + currentY + " currentY - downY:" + (currentY - downY) + " show"); } } break; case MotionEvent.ACTION_UP: isActionDown = false;// isActionDown重置 break; default: break; } return false; } }; AbsListView.OnScrollListener listScrollListener = new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if(mListViewFirstItem==-1) { return; } if (SCROLL_STATE_IDLE==scrollState ) { // if(Math.abs(mScreenY)-(screenHeight*3)/5>0) // { // listRes.smoothScrollToPosition(mListViewFirstItem + 1); // // }else{ // listRes.smoothScrollToPosition(mListViewFirstItem); // } // if(listRes.getFirstVisiblePosition() == 0 && lastDirection == 1) { // mMPHBarChart.setVisibility(View.VISIBLE); // } } } @Override public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) { if(listRes.getAdapter()==null || listRes.getChildCount()==0) { return; } View visibleFirstChild = (View) listRes.getChildAt(firstVisibleItem); if(visibleFirstChild==null) { return; } int[] location = new int[2]; visibleFirstChild.getLocationOnScreen(location); Log.d("zzq", "first="+firstVisibleItem+" , screenY="+location[1]+",deltY="+(Math.abs(mScreenY)-(screenHeight*3)/5)); if(firstVisibleItem!=mListViewFirstItem) { if(firstVisibleItem>mListViewFirstItem) { Log.d("zzq", "between item 向上滑动 hide mpchart"); // mMPHBarChart.setVisibility(View.GONE); }else{ Log.d("zzq", "向下滑动"); } }else{ if(mScreenY>location[1]) { Log.d("zzq", "->finger in item 向上滑动 hide mpchart"); //mMPHBarChart.setVisibility(View.GONE); } else if(mScreenY<location[1]) { Log.d("zzq", "->向下滑动"); } } if(firstVisibleItem == 0) { // mMPHBarChart.setVisibility(View.VISIBLE); Log.d("zzq", "first visible ==0 show mpchart"); } mListViewFirstItem = firstVisibleItem; mScreenY = location[1]; } };
相关文章推荐
- oracle-备份方案之(EXP+任务计划)
- UltraWebToolbar+UltraWebGrid使用实例
- python废话二:学习python2还是python3
- 如何获取wifi名称(SSID)
- 设计模式读书笔记-----中介者模式
- 设计模式之:外观模式(Facade Pattern)
- android 字体TypeFace设置.
- 源码推荐(01.25):微博cell自动布局,三行代码集成设置界面LGSettingView
- 日经春秋 20160125
- 天声人語 20160125 琴奨菊遅咲きの賜杯
- 有限差分法Eluer算法(求解常微分方程)
- IntelliJ IDEA IDEtalk
- JetBrains C++ IDE 推荐
- 如何解决Connect超时导致的阻塞问题
- 设计模式读书笔记-----解释器模式
- iOS配置多语言
- 支持动态Web开发的9个有用&免费工具
- 微博项目代码勘误
- PHP 文件操作类(创建文件并写入) 生成日志
- CodeForces 557B Pasha and Tea