简体   繁体   中英

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

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 with no luck. When I touch an edit text inside that item's layout it is still clickable and will open the keyboard.

Thanks very much in advance!

Edit: This is not the same problem as the one presented in the referenced topic. I do not wish to disable the whole recycle view. Just disable some items from the recycler view. I have already tried the solutions present in the referenced topic to the specific item view and it did not work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM