Home > other >  How to inflate custom view with XML layout with use of ViewBinding?
How to inflate custom view with XML layout with use of ViewBinding?

Time:03-18

I have layout in 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:

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.

What am I doing wrong here?

CodePudding user response:

I would add a comment but not enough rep.

What does you recycler adapter look like?

CodePudding user response:

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

and this -

PartyListItemBinding.inflate(LayoutInflater.from(parent.context)) 

is how you inflate a custom layout

  • Related