Home > Net >  Why isn't layout inflater available in recycler view?
Why isn't layout inflater available in recycler view?

Time:06-12

i'm having trouble at recycler view. When i want to add layout inflater, android does not respond like this

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

class MyAdapter(var ctx: Context): RecyclerView.Adapter<RecyclerView.ViewHolder> (){


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var myRoot: layoutInflater
    }

enter image description here

But when in other activity, layout inflater appears by itself, like this one

enter image description here

What should i do with this?

CodePudding user response:

var myRoot: layoutInflater

This is a grammar error. layoutInflater is not a type of a parameter. This is not type, it's an object.

CodePudding user response:

You need to get layout inflater from context in adapter, like this:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) {
    return CustomViewHolder(parent.inflateView(R.layout.item_view))
}
    
private fun ViewGroup.inflateView(@LayoutRes res: Int): View {
    return LayoutInflater.from(context).inflate(res, this, false)
}
  • Related