简体   繁体   English

禁用某些RecyclerView项的单击/触摸

[英]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: 为此,您可以将现有的itemView布局包装在FrameLayout然后在其之上添加另一个视图:

<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: onCreateViewHolder()内部,您可以将无操作点击侦听器分配给叠加层:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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