RecyclerView 内item点击失效

 

 例如

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/content_one"
    android:layout_width="match_parent"
    android:layout_height="@dimen/normal_100dp"
    android:paddingTop="@dimen/normal_0dp"
    android:paddingRight="@dimen/normal_24dp"

    >
    <TextView
        android:id="@+id/tv_location"
        android:layout_width="@dimen/normal_480sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/normal_16dp"
        android:maxLines="2"
        android:inputType="text"
        android:textColor="@color/color_222222"
        android:textSize="@dimen/normal_24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/view_01"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="广东省深圳市宝安区留仙二路庭威工业园二栋三楼"

        />
</androidx.constraintlayout.widget.ConstraintLayout>

item布局如下,当点击后无反应。需要把android:inputType=“text”去掉,就可以点击了!

原因:

官方是这样说明的:

Attribute android:inputType should not be used with <TextView>: Change element type to <EditText> ? less... (Ctrl+F1)

Using a <TextView> to input text is generally an error, you should be using <EditText> instead. EditText is a subclass of TextView, and some of the editing support is provided by TextView, so it's possible to set some input-related properties on a TextView. However, using a TextView along with input attributes is usually a cut & paste error. To input text you should be using <EditText>.

This check also checks subclasses of TextView, such as Button and CheckBox, since these have the same issue: they should not be used with editable attributes.

Issue id: TextViewEdits

结合以上说明,android:inputType的属性只对EditText有效,我认为EditText本身有抢夺焦点的作用,给TextView设置android:inputType的属性后可能也会导致焦点抢夺,导致点击“Text”文本并不会触发条目点击事件,而是被自身消耗掉了。也可以理解为点击事件被屏蔽掉了。

这里,要感谢下我的朋友margintop,他后来发现:如果已经设置了android:inputType的属性,再设置两个属性android:clickable和android:longClickable,这两个属性都设置成false,也可以达到预期的效果。也就是说android:inputType改变了那两个属性的默认值,把false改成了true。

 

 

RecyclerView嵌套RecyclerView的情况下,子RecyclerView的binding失效通常是由于以下几个原因导致的: 1. **视图复用问题**:RecyclerView的视图复用机制可能导致子RecyclerView的binding失效。因为在视图复用时,子RecyclerView的视图可能会被重新绑定,但数据却没有及时更新。 2. **数据绑定问题**:子RecyclerView的adapter可能在数据变化时没有及时刷新,导致binding失效。 3. **生命周期问题**:父RecyclerView的adapter和子RecyclerView的adapter的生命周期管理不当,可能导致子RecyclerView的binding失效。 为了解决这个问题,可以采取以下几种方法: 1. **确保数据更新**:确保在父RecyclerView的adapter中,当数据变化时,子RecyclerView的adapter也能及时更新数据。 2. **正确设置LayoutManager**:在子RecyclerView的onBindViewHolder方法中,确保正确设置LayoutManager和adapter。 3. **避免重复绑定**:在子RecyclerView的onBindViewHolder方法中,避免重复绑定ViewHolder。 以下是一个示例代码,展示了如何在父RecyclerView的adapter中正确设置子RecyclerView: ```java public class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.ParentViewHolder> { private List<ParentItem> parentItems; public ParentAdapter(List<ParentItem> parentItems) { this.parentItems = parentItems; } @Override public ParentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.parent_item_layout, parent, false); return new ParentViewHolder(view); } @Override public void onBindViewHolder(ParentViewHolder holder, int position) { ParentItem parentItem = parentItems.get(position); holder.childRecyclerView.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext(), LinearLayoutManager.HORIZONTAL, false)); ChildAdapter childAdapter = new ChildAdapter(parentItem.getChildItems()); holder.childRecyclerView.setAdapter(childAdapter); } @Override public int getItemCount() { return parentItems.size(); } public static class ParentViewHolder extends RecyclerView.ViewHolder { RecyclerView childRecyclerView; public ParentViewHolder(View itemView) { super(itemView); childRecyclerView = itemView.findViewById(R.id.child_recycler_view); } } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值