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
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);