【问题标题】:How inflate a Linear layout with custom view by databinding如何通过数据绑定使用自定义视图膨胀线性布局
【发布时间】:2020-05-30 10:53:37
【问题描述】:

我只想制作一个带有一些 Vertical LinearLayout 的地图,其中每个都有一个 Horizo​​ntal LinearLayout 并且在每个布局中都想膨胀一个类似于下面的代码:

每个单元格.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android">
<data>
    <variable name="live" type="Integer" />
    <variable name="i" type="Integer" />
    <variable name="j" type="Integer" />
</data>

<ImageView
    android:id="@+id/itemView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    app:setI="@{i}"
    app:setJ="@{j}"/>

each_row.xml:

<?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"
    android:id="@+id/row_instance"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:orientation="horizontal">

</LinearLayout>

我将把这个 Horizo​​ntal LL 视图添加到我的主 Vertical LL 并创建地图

和 kotlin 代码:

            val cell = DataBindingUtil.inflate<EachCellBinding>(inflater , R.layout.each_cell , HorizontalLinearLayout.row_instance , true   )

但错误是:

Required DataBindingComponent is null in class EachCellBindingImpl. A BindingAdapter in com.example.gameoflife.SartFragment.StartFragment is not static and requires an object to use, retrieved from the DataBindingComponent. If you don't use an inflation method taking a DataBindingComponent, use DataBindingUtil.setDefaultComponent or make all BindingAdapter methods static.

【问题讨论】:

  • 您在哪里声明了方法 app:setI 和 app:setJ 的绑定适配器?

标签: android kotlin data-binding android-databinding


【解决方案1】:

如错误所示,您在com.example.gameoflife.SartFragment.StartFragment 中声明了绑定适配器方法。在类中声明它使其成为非静态的。但是绑定适配器方法应该始终是静态的。所以把类里面的方法放到类外面,它会变成静态的,你的错误就会得到解决。

【讨论】:

  • tnx ,我把它放在片段类上,是的,当把它放在外面时,我的问题就解决了
猜你喜欢
  • 2011-05-18
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多