[CymChad/BaseRecyclerViewAdapterHelper]Recyclerview嵌套Recyclerview的外层Recyclerview的item点击事件问题

2024-08-20 234 views
4

img_20180130_101248

如图所示:列表是外层一个Recyclerview,切外层Recyclerview的每个item里面也包含一个Recyclerview(该Recyclerview的item不需要点击事件)。

红色框是一个整体的外层Recyclerview的一个item,当我使用adapter.setOnItemClickListener时只有点击蓝色框外面部分时才有效果,而点击蓝色框内部时不起作用,为什么?

回答

7

adapter.setOnItemClickListener((adapter, view, position) -> { Log.i("TAG", "setOnItemClickListener"); });

使用上面代码时点击蓝色框不起作用,只有点击蓝色框外部,红色框里面时才有效果。

而我使用 recyclerView.addOnItemTouchListener(new OnItemClickListener() { @Override public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) { LogUtils.i("TAG", "onSimpleItemClick"); } });时,点击中间蓝色框有效果,但是点击事件又执行了两次。怎么破?

5
  1. 嵌套点击事件, 内部提供了一个setNestChild() 属性,在父层Adapter内设置子recyclerview的id ,可参考Demo里面的嵌套事件的点击写法
8

@AllenCoder
image 写了个democe测试嵌套Recyclerview

 @Override
    protected void convert(BaseViewHolder helper, String item) {
        RecyclerView mRecyclerView = helper.getView(R.id.item_list);
        SecondAdapter adapter = new SecondAdapter();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(adapter);

        List<String> mListData = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            mListData.add("dd");
        }
        adapter.setNewData(mListData);

        helper.setNestView(R.id.item_list);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(mContext,position+"嵌套",Toast.LENGTH_SHORT).show();
            }
        });
    }

点击红框空白部分没有响应

1

请问楼主最后怎么解决的。。

8

同问

7

在item里面的Recyclerview上门覆盖一层透明的View,然后监听这层覆盖view的点击事件即可解决.

8

给子recycleView设置onTouch事件,并且设置子recycleView的点击监听事件:

  recyclerView.setOnTouchListener(new ChildTouchListener(this));
  recyclerView.setOnClickListener(v -> {
                //do.........
   });

  private class ChildTouchListener implements View.OnTouchListener{

    private float downX;
    private float downY;
    private float touchSlop;
    private View view;

    ChildTouchListener(View view){
        this.view = view;
        touchSlop = ViewConfiguration.get(view.getContext()).getScaledTouchSlop();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getActionMasked()){
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                if (Math.abs(event.getY() - downY) < touchSlop && Math.abs(event.getX() - downX) < touchSlop){
                    view.performClick();
                }
                break;
        }
        return false;
    }
}
3

/**

  • 解决Recyclerview 或者listView item嵌套Recyclerview
  • item的点击事件点击Recyclerview部分无效的问题
  • @Author zhoudq
  • @Time 2020/7/15 */ public class NoTouchRecyclerView extends RecyclerView { public NoTouchRecyclerView(Context context) { super(context); }

    public NoTouchRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); }

    public NoTouchRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }

    @Override public boolean onTouchEvent(MotionEvent e) { return false; }

    @Override public boolean onInterceptTouchEvent(MotionEvent e) { return true; }

    @Override public boolean dispatchTouchEvent(MotionEvent ev) { return super.dispatchTouchEvent(ev); }

}

0

//内层recyclerView设置属性:让外层recyclerView能接收到点击事件 recyclerView.suppressLayout(true); 内层添加该属性,亲测有效

5
  1. 嵌套点击事件, 内部提供了一个setNestChild() 属性,在父层Adapter内设置子recyclerview的id ,可参考Demo里面的嵌套事件的点击写法

4.0 版本是不是没有这个属性了?

4

//内层recyclerView设置属性:让外层recyclerView能接收到点击事件 recyclerView.suppressLayout(true); 内层添加该属性,亲测有效

亲测无效