按钮单击StaggeredGrid中的行内部RecyclerView触发整个项目单击

时间:2015-06-12 05:29:16

标签: android android-recyclerview onitemclicklistener recycler-adapter

嗨我在RecyclerView行内有一个按钮(用于书签)。当我点击按钮时,它会触发该按钮点击,同时也会触发整行的项目。我有单独的代码来处理按钮和整个项目的点击。为什么行中的按钮会触发整个项目点击?

如何解决此问题?

以下是我在适配器中点击按钮的代码:

viewHolder.imgBookmark.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ...
    }
});

这是我点击RecyclerView项目的代码:

mGridView.addOnItemTouchListener(new RecyclerItemClickListener(
        getActivity(), mGridView,
        new RecyclerItemClickListener.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        ...
    }

    @Override
    public void onItemLongClick(View view, int position) {
        ...
    }
}));

RecyclerItemClickListener.java

public class RecyclerItemClickListener implements
    RecyclerView.OnItemTouchListener {
    public static interface OnItemClickListener {
        public void onItemClick(View view, int position);

        public void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mListener;
    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 childView = recyclerView.findChildViewUnder(
                        e.getX(), e.getY());

                if (childView != null && mListener != null) {
                    mListener.onItemLongClick(childView,
                            recyclerView.getChildPosition(childView));
                }
            }
        });
    }

    @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.getChildPosition(childView));
        }

        return false;
    }

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

1 个答案:

答案 0 :(得分:3)

为列表项添加触摸侦听器并使用它们ClickListeners不是一个好主意。
以下是设置RecyclerView

的itemClick侦听器的小示例
public static class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
public IMyViewHolderClicks mListener;

public ViewHolder(View itemLayoutView, IMyViewHolderClicks listener) {
    super(itemLayoutView);
    mListener = listener;
    itemLayoutView.setOnClickListener(this);
   }
}

更新:
   在列表项中添加按钮的监听器。

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //Set the data for the views.
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }