Android RecyclerView适配器不使用数据绑定更新数据
请帮我解决一个问题。 我有一个带有recyclerView的activity.xml文件,我设置了一个适配器,如:Android RecyclerView适配器不使用数据绑定更新数据,android,kotlin,android-recyclerview,android-databinding,Android,Kotlin,Android Recyclerview,Android Databinding,请帮我解决一个问题。 我有一个带有recyclerView的activity.xml文件,我设置了一个适配器,如: <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" an
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
android:visibility="@{viewModel.recyclerVisibility}"
app:adapter="@{viewModel.adapter}"
app:layout_constraintTop_toBottomOf="@id/title" />
要在xml中绑定适配器并访问它,我在viewModel中有如下方法:
@Bindable
fun getAdapter(): SomeAdapter {
if (adapter == null) {
adapter = SomeAdapter(emptyList())
}
return adapter as SomeAdapter
}
在我的适配器中,我有更新数据列表的方法:
fun setItems(someData: List<SomeData>) {
this.someData = someData
notifyDataSetChanged()
}
那么这里的问题是什么?因为我在UI上看到空适配器,所以我猜绑定工作不正常。这是一个愚蠢但不明显的错误,但问题已经解决: 我忘记为我的recyclerView添加布局管理器,如:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
在xml中,也可以通过编程实现。。
**它发生在:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"