处理按钮单击RecyclerView

时间:2017-02-01 16:56:30

标签: android button android-recyclerview fragment

我正在尝试使用onClickListener for Button Inside我的RecyclerView行,我也有我的RecyclerView行的点击监听器,现在的问题是,如果我点击RecyclerView的一行上的Button,它还会触发RecyclerView的点击监听器排,顺便说一下,我正在使用片段。

这是RecyclerView touchListener的自定义类

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

private GestureDetector gestureDetector;
private ClickListener clickListener;

public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
    this.clickListener = clickListener;
    gestureDetector = 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 && clickListener != null) {
                clickListener.onLongClick(child, recyclerView.getChildPosition(child));

            }
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

    View child = rv.findChildViewUnder(e.getX(), e.getY());
    if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
        clickListener.onClick(child, rv.getChildPosition(child));

    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

public static  interface ClickListener {

    void onClick(View view, int position);
    void onLongClick(View view, int position);
}

}

这是ViewHolder for Buttons

 public static class RecycleViewHolder extends RecyclerView.ViewHolder{

    ImageButton BTN_B, BTN_A, BTN_C;

    public RecycleViewHolder(View view){

        super(view);

        ItemImage      = (ImageView) view.findViewById(R.id.item_item_image);

        BTN_A = (ImageButton) view.findViewById(R.id.button_a);
        BTN_B = (ImageButton) view.findViewById(R.id.button_b);
        BTN_C = (ImageButton) view.findViewById(R.id.button_c);

       BTN_B.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CTX,"B Button Clicked"+getAdapterPosition(),Toast.LENGTH_SHORT).show();
            }
        });

        BTN_A.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CTX,"A Button Clicked"+getAdapterPosition(),Toast.LENGTH_SHORT).show();
            }
        });

        BTN_C.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CTX,"C Button Clicked"+getAdapterPosition(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

不是为整个回收者视图实现点击事件,而是获取回收者视图的子布局的父元素,并为其添加点击事件,就像您为BTN_A,BTN_B,BTN_C,

添加的那样

例如,如果Recycler视图的子布局的父元素(子布局中的最顶层视图)是相对布局,则为relativelayout添加onclick事件