listview 滑动监听

来源:互联网 发布:le48r31数据 编辑:程序博客网 时间:2024/04/28 00:01
  // 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];        }    };
0 0
原创粉丝点击