Disable click/touch for some of a RecyclerView's items

前端 未结 1 1576
遥遥无期
遥遥无期 2021-01-29 07:29

Is there a way to prevent clicking in a specific item of a recycler view? Already tried to set the view as not clickable and not enabled in the view holder constructor but still

相关标签:
1条回答
  • 2021-01-29 08:10

    Probably the easiest way to completely block interaction with anything inside a single item is to put a transparent view over it that intercepts all touch events. You'd do this by wrapping your existing itemView layout in a FrameLayout and adding another view on top of that:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <!-- your itemView content here -->
    
        <View
            android:id="@+id/overlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </FrameLayout>
    

    Inside onCreateViewHolder(), you can assign a no-op click listener to the overlay:

    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.itemview, parent, false);
        MyViewHolder holder = new MyViewHolder(itemView);
    
        holder.overlay.setOnClickListener(v -> {});
    
        return holder;
    }
    

    Now, when you want to disable clicks, you can call

    holder.overlay.setVisibility(View.VISIBLE);
    

    and when you want to disable them, you can call

    holder.overlay.setVisibility(View.GONE);
    
    0 讨论(0)
提交回复
热议问题