简体   繁体   English

如何使用 ViewBinding 使用 XML 布局扩展自定义视图?

[英]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
    )
}

and if you want to inflate a custom view like a prompt, here's a sample code - 如果你想像提示一样膨胀自定义视图,这里有一个示例代码 -
 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.

相关问题 如何从自定义视图 java class 膨胀布局? 不是来自 XML - How to inflate layout from custom view java class? NOT from XML 如何将xml布局充气到子视图中 - How to inflate xml-layout into child View 如何将 Android ViewBinding 用于从现有布局膨胀的自定义视图 - How to use Android ViewBinding for custom view which inflates from existing layout 如何通过数据绑定使用自定义视图膨胀线性布局 - How inflate a Linear layout with custom view by databinding 如何在XML中扩展自定义视图? - How do I Inflate a Custom View in XML? 如何在没有父级的情况下将xml膨胀为自定义视图 - How to inflate xml as Custom View without a parent 当从“其他”布局 XML (即,不是相应的片段 XML)中引用视图 ID 时,如何使用 ViewBinding? - How to use ViewBinding when referencing view IDs from "other" layout XML i.e, not the corresponding Fragment XML? 如何在布局中使用视图绑定和视图模型? - how to use viewbinding with viewmodel in layout? Android Studio-如何膨胀包含使用百分比参数的视图的xml布局 - Android Studio - How to inflate an xml layout containing a view that use percentage params Android:如何使用XML进行自定义视图布局 - Android: how to use XML for custom view layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM