[英]How to inflate custom view with XML layout with use of ViewBinding?
I have layout in XML:我在 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/hsv"
android:layout_width="match_parent"
android:layout_height="92dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#555555"
android:orientation="horizontal"
android:paddingHorizontal="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</HorizontalScrollView>
And extended HorizontalScrollView
as custom view definition:并将 Horizo HorizontalScrollView
扩展为自定义视图定义:
class TopBubblesWidget(context: Context, attrs: AttributeSet? = null) : HorizontalScrollView(context, attrs) {
private var binding: FragmentBiometricTopBubblesBinding = FragmentBiometricTopBubblesBinding.inflate(LayoutInflater.from(context))
private var data: List<BubblesWidget.Data>? = null
override fun onFinishInflate() {
super.onFinishInflate()
binding.rv.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
}
private fun initView(data: List<BubblesWidget.Data>) {
binding.rv.adapter = TopBubblesAdapter(data)
}
fun updateData(data: List<BubblesWidget.Data>) {
initView(data)
}
}
The problem is that TopBubblesWidget
is not inflated by the XML and I do not see the RecyclerView.问题是TopBubblesWidget
没有被 XML 膨胀,我没有看到 RecyclerView。
What am I doing wrong here?我在这里做错了什么?
I have a feeling this is what you are looking for.我有一种感觉,这就是您要找的东西。 This is a sample code -这是一个示例代码 -
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PartyViewHolder {
return PartyViewHolder(
PartyListItemBinding.inflate(LayoutInflater.from(parent.context)),
viewModel
)
}
private fun logOutAndExit() { val dialogBox = Dialog(requireContext()) val promptLogOutBinding = PromptLogOutBinding.inflate(layoutInflater)//declaration done here dialogBox.apply { setContentView(promptLogOutBinding.root) window...setLayout( ViewGroup,LayoutParams.MATCH_PARENT. ViewGroup.LayoutParams.WRAP_CONTENT ) setCancelable(false) show() }
in the top, my layout file is party_list_item
while, in the bottom example, my layout is prompt_log_out
在顶部,我的布局文件是party_list_item
,而在底部示例中,我的布局是prompt_log_out
and this -和这个 -
PartyListItemBinding.inflate(LayoutInflater.from(parent.context))
is how you inflate a custom layout是你如何膨胀自定义布局
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.