Skip to content Skip to sidebar Skip to footer

Unresolved Reference: Layout_inflater_service Inside Onbindviewholder

Inside OnBindViewHolder of my RecyclerView Adapter, I got multiple items. One of those (ITEM_TRATAMENTOS) got a setOnClickListener which has the purpose of create an LinearLayout w

Solution 1:

An alternative solution is to get the LayoutInflater from parentLinearLayout ViewGroup.

Example

parentLinearLayout?.apply { 
    val inflater = LayoutInflater.from(context) // context is now available in the receiver scopeval rowView = inflater.inflate(R.layout.used_products_field, this, false)
    addView(rowView) // Add the view to the last position
}

Also, be aware of the consequences of adding too many views without recycling them. Probably you'd need another RecyclerView if the number is big enough.

Solution 2:

Step #1: Add a LayoutInflater parameter to the constructor of your RecyclerView.Adapter subclass.

Step #2: When you create the RecyclerView.Adapter, pass in a LayoutInflater, obtained from getLayoutInflater() on your activity.

For example, here is a RecyclerView.Adapter named ColorAdapter:

/*
  Copyright (c) 2018 CommonsWare, LLC

  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  Covered in detail in the book _Elements of Android Jetpack_

  https://commonsware.com/Jetpack
*/package com.commonsware.jetpack.sampler.recyclerview

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter

classColorAdapter(privateval inflater: LayoutInflater) :
  ListAdapter<Int, ColorViewHolder>(ColorDiffer) {

  overridefunonCreateViewHolder(
    parent: ViewGroup,
    viewType: Int
  ): ColorViewHolder {
    return ColorViewHolder(inflater.inflate(R.layout.row, parent, false))
  }

  overridefunonBindViewHolder(holder: ColorViewHolder, position: Int) {
    holder.bindTo(getItem(position))
  }

  privateobject ColorDiffer : DiffUtil.ItemCallback<Int>() {
    overridefunareItemsTheSame(oldColor: Int, newColor: Int): Boolean {
      return oldColor == newColor
    }

    overridefunareContentsTheSame(oldColor: Int, newColor: Int): Boolean {
      return areItemsTheSame(oldColor, newColor)
    }
  }
}

Notice that my ColorAdapter constructor has private val inflater: LayoutInflater as a parameter.

Here is the activity that uses that ColorAdapter:

/*
  Copyright (c) 2018 CommonsWare, LLC

  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  Covered in detail in the book _Elements of Android Jetpack_

  https://commonsware.com/Jetpack
*/package com.commonsware.jetpack.sampler.recyclerview

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

classMainActivity : AppCompatActivity() {
  privateval random = Random()

  overridefunonCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    items.apply {
      layoutManager = LinearLayoutManager(this@MainActivity)
      addItemDecoration(
        DividerItemDecoration(this@MainActivity, DividerItemDecoration.HORIZONTAL)
      )
      adapter = ColorAdapter(layoutInflater).apply {
        submitList(buildItems())
      }
    }
  }

  privatefunbuildItems() = generateSequence { random.nextInt() }
    .take(25)
    .toList()
}

Since you and I are both writing in Kotlin, the getLayoutInflater() call turns into a reference to the layoutInflater property on the activity. So, when I create my ColorAdapter, I use ColorAdapter(layoutInflater), to pass in an instance of LayoutInflater.

Post a Comment for "Unresolved Reference: Layout_inflater_service Inside Onbindviewholder"