微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ViewBinding 不适用于 RecyclerView 项目中包含的布局

如何解决ViewBinding 不适用于 RecyclerView 项目中包含的布局

这是我的 RecyclerView 项目布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"><!--
    android:background="@color/home_item_bg"-->

    <include
        android:id="@+id/layoutSectionHeader"
        layout="@layout/section_header_rv_item_home"/>

    <androidx.cardview.widget.CardView
        style="@style/Widget.MaterialComponents.CardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        app:cardElevation="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:paddingStart="16dp"
            android:paddingTop="8dp"
            android:paddingEnd="16dp"
            android:paddingBottom="8dp">

            <TextView
                android:id="@+id/tvMarketAndTerm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                tools:text="NASDAQ - Medium Term" />

            <com.github.mikephil.charting.charts.PieChart
                android:id="@+id/pieChart"
                android:layout_width="match_parent"
                android:layout_height="@dimen/pie_chart_height" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

这是section_header_rv_item_home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/llSectionHeader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingStart="16dp"
    android:paddingTop="12dp"
    android:background="?selectableItemBackground"
    android:paddingEnd="16dp"
    android:paddingBottom="12dp"><!--
    android:background="@drawable/ripple_white_light_grey"-->

    <TextView
        android:id="@+id/tvProductName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLines="1"
        android:textAllCaps="false"
        android:textColor="@color/gray"
        android:textSize="20sp"
        android:textStyle="bold"
        tools:text="@string/stock_exchange_barometer" />

    <TextView
        android:id="@+id/tvSeeAll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/see_all"
        android:textColor="@color/colorAccent"
        android:textSize="13sp"
        android:textStyle="bold"
        android:visibility="gone"
        tools:visibility="visible" />
</LinearLayout>

这是如何访问包含布局中的视图

inner class BarometerVH(private val rvBinding: RvItemHomeSeBarometerBinding) :
            RecyclerView.ViewHolder(rvBinding.root) {
    fun bind(homeItem: HomeItem) {
        rvBinding.layoutSectionHeader.tvProductName.text = homeItem.title
    }
}

我没有编译时错误,但在运行应用程序时出现此异常

java.lang.NullPointerException: Missing required view with ID: com.example.appname:id/layoutSectionHeader

在 Activity 或 Fragment 中包含布局对我有用,但在 Recycler View 中使用时不起作用。

我尝试了本论坛中针对类似异常提供的多种解决方案,但没有一个适用于回收站视图,而不是关于在回收站视图中使用

编辑 1:我添加了 RecyclerView Adapter 中的重写方法

override fun onCreateViewHolder(
     parent: ViewGroup,viewType: Int
) = MarketCommentaryVH(RvItemHomeMarketCommentaryBinding.bind(parent))

override fun onBindViewHolder(
     holder: RecyclerView.ViewHolder,position: Int
) {
    holder.bind(homeItemList[position])
}

解决方法

RecyclerView 通过创建和重用一堆 ViewHolder 来工作,这些 onCreateViewHolder 包含视图布局以显示项目的详细信息。您在 ViewHolder 方法中设置这些,您可以:

  • 为项目增加布局
  • 将膨胀的视图传递给 RvItemHomeMarketCommentaryBinding.bind(parent) 构造函数

你实际上并没有夸大布局,你正在做

RvItemHomeMarketCommentaryBinding.inflate(LayoutInflater.from(context),parent,false)

代替

parent

(您需要在膨胀 ViewHolder 的布局时传入 bind,以便它可以使用正确的尺寸等进行布局)


Binding 组件上的 layoutSectionHeader 函数采用已经膨胀的视图层次结构,并尝试使用它知道的 ID 查找所有组件。例如,它会尝试查找 binding.layoutSectionHeader,以便将 parent 分配给该视图。

问题是,在您传入的 RecyclerView 布局中不存在它,因为那只是 ViewHolder 的布局,它不包含 {{1} 中的内容}} 布局。所以 bind 不起作用 - 视图不在那里绑定。您需要改为调用 inflate

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。