android cancel the click event of the RecycleView nested in the item

Source of the problem:

        In our android adapter, a layer of RecycleView is often nested in the item, such as this...


        However, when we set the click event of the entire area, the RecycleView area (and the circular logo area) does not respond to the click event...

The code is this:

        xml code:

    <LinearLayout
        android:id="@+id/ll_item"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="vertical"
        >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rlv_item"
            android:layout_width="match_parent"
            android:layout_height="30dp"/>

    </LinearLayout>

        Code in adapter:

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.VH> {

    private Context context;

    public RecycleViewAdapter(Context context){
        this.context = context;
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VH(LayoutInflater.from(context).inflate(R.layout.layout_item,null));
    }

    @Override
    public void onBindViewHolder(VH holder, final int position) {
        
        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"clicked "+(position+1)+"th",Toast.LENGTH_SHORT).show();
            }
        });
        
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    static  class VH extends RecyclerView.ViewHolder{
        private LinearLayout ll_item;
        private RecyclerView rlv_item;

        public VH(View itemView) {
            super(itemView);
            ll_item = (LinearLayout) itemView.findViewById(R.id.ll_item);
            rlv_item = (RecyclerView) itemView.findViewById(R.id.rlv_item);
        }
    }

}

        In onBindViewHolder(), we want to set the click event of LinearLayout, but RecycleView will not execute the click event...

problem solved:

        In fact, the problem is that in the onTouchEvent function of RecycleView, the dispatchOnItemTouch function will be called first to do some work, but when we rewrite RecycleView's onTouchListener(), the OnItemTouchListener object we added will be called before RecycleView distributes the click, so we can do this ...

    @Override
    public void onBindViewHolder(final VH holder, final int position) {

        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"clicked "+(position+1)+"th",Toast.LENGTH_SHORT).show();
            }
        });

        holder.rlv_item.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return holder.ll_item.onTouchEvent(event);
            }
        });

    }

        ok, the problem has been solved temporarily, the whole area can be clicked, and respond to the click event, friends who encounter this problem can also try this method...

        

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324699937&siteId=291194637