listview 滑动监听

  // 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];

        }
    };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值