RecyclerView.OnItemTouchListener不规则地调用OnItemClick和onLongItemClick

时间:2018-05-31 07:36:08

标签: android android-recyclerview

我一直试图在我的recyclerview中实现简单的点击和长按,但是当我长按一个项目时,onLongItemClick会在正确位置执行两次,而在位置-1执行一次。也可以在简单点击时执行onLongItemClick。这是我的代码。

recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {

            if (shareMedia) {
                if (isMultiSelect)
                    Toast.makeText(SearchActivity.this, "Click Index: " + position + "\nMultiselect: " + isMultiSelect, Toast.LENGTH_SHORT).show();
                    if (position > -1) {
                        multi_select(position);
                    } else
                        Toast.makeText(getApplicationContext(), "Tap and hold to share", Toast.LENGTH_SHORT).show();

            }else{
                //Another Implementation
            }
        }

        @Override
        public void onItemLongClick(View view, int position) {
            if (!isMultiSelect) {
                SelectedListData = new ArrayList<MediaListingModel>();
                isMultiSelect = true;

                if (mActionMode == null) {
                    mActionMode = startActionMode(mActionModeCallback);
                }
            }
            Toast.makeText(SearchActivity.this, "Long Click Index: " + position + "\nMultiselect: " + isMultiSelect, Toast.LENGTH_SHORT).show();
            if(position > -1) {
                multi_select(position);
            }

        }
    }));

RecyclerItemClickListener.java的代码是

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;

public interface OnItemClickListener {
    void onItemClick(View view, int position);

    void onItemLongClick(View view, int position);
}

private GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (child != null && mListener != null) {
                mListener.onItemLongClick(child, recyclerView.getChildAdapterPosition(child));
            }
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        return true;
    }
    return false;
}

@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }

@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}

}

1 个答案:

答案 0 :(得分:1)

错误可能在 RecyclerItemClickListener.java 中。尝试删除if语句中的return true;

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    }
    return false;
}

Working with recycler view - tutorial