Android ListView onTouch滑动

时间:2015-05-18 05:00:58

标签: android

我正在尝试制作一个像滑动一样的收件箱,以便在列表视图中选择多个项目。我能够在主视图下显示视图,并且还能够拖动视图。问题是试图让onTouch和onItemClickListener一起工作。

1:如果我在ACTION_DOWN期间在switch case中返回true,则onItemClickListener停止工作。如果我返回false,那么我无法拖动视图,但onItemClickListener正在工作。

2:在拖动视图时,listView仍然可以滚动,这使得onTouch转到ACTION_CANCLE

我将onTouchListener附加到适配器的getView()方法中的视图,如下所示:

root.setOnTouchListener(new MySwipeListener);

    @Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            return false;
        }
        case MotionEvent.ACTION_MOVE: {
            upX = event.getX();
            float deltaX = downX - upX;
            if (Math.abs(deltaX) > MIN_LOCK_DISTANCE && listView != null && !motionInterceptDisallowed) {
                listView.onInterceptTouchEvent(event);
                listView.requestDisallowInterceptTouchEvent(true);
                motionInterceptDisallowed = true;
                listView.requestDisallowInterceptTouchEvent(true);
            }
            if (deltaX > 0) {
                holder.viewLeft.setVisibility(View.GONE);
            } else {
                // if first swiped left and then swiped right
                holder.viewLeft.setVisibility(View.VISIBLE);
            }
            //holder.mainView.setTranslationX(-(int)deltaX);
            if(deltaX < 0) {
                holder.mainView.setTranslationX(-(int) deltaX);
            }
            return true;
        }
        case MotionEvent.ACTION_UP: {
            upX = event.getX();
            float deltaX = upX - downX;
            if (Math.abs(deltaX) > MIN_DISTANCE) {
                holder.mainView.setTranslationX(200);
            } else {
                holder.mainView.setTranslationX(0);
            }
            if (listView != null) {
                listView.requestDisallowInterceptTouchEvent(false);
                motionInterceptDisallowed = false;
            }
            holder.viewLeft.setVisibility(View.VISIBLE);
            return false;
        }
        case MotionEvent.ACTION_CANCEL:{
            holder.viewLeft.setVisibility(View.VISIBLE);
            holder.mainView.setTranslationX(0);
            return false;
        }

    }
    return false;

}

1 个答案:

答案 0 :(得分:1)

请使用如下的GestureDetector,

{{1}}