禁用某些RecyclerView项的单击/触摸 [英] Disable click/touch for some of a RecyclerView's items

查看:679
本文介绍了禁用某些RecyclerView项的单击/触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以防止单击回收者视图中的特定项目?已经尝试将视图设置为不可点击且未在视图持有器构造函数中启用该视图,但仍然没有运气.当我在该项目的布局内触摸编辑文本时,它仍然是可单击的,并且将打开键盘.

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.

非常感谢!

这与所引用主题中提出的问题不同.我不希望禁用整个回收视图.只需从回收者"视图中禁用某些项目即可.我已经尝试了针对特定项目视图的参考主题中提供的解决方案,但该方法不起作用.

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.

推荐答案

完全阻止与单个项目中的任何内容进行交互的最简单方法可能是在其上方放置一个透明视图,以拦截所有触摸事件.为此,您可以将现有的itemView布局包装在FrameLayout中,然后在其顶部添加另一个视图:

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>

onCreateViewHolder()内部,您可以为叠加层分配无操作点击监听器:

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

这篇关于禁用某些RecyclerView项的单击/触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆