未解决的参考:onBindViewHolder中的LAYOUT_INFLATER_SERVICE

时间:2018-12-30 21:46:43

标签: android kotlin adapter layout-inflater

OnBindViewHolder RecyclerView的{​​{1}}里面,我有多个物品。其中的一个(ITEM_TRATAMENTOS)得到了一个setOnClickListener,目的是当我单击按钮(add_field_btn)时创建一个Adapter。问题在于LinearLayout的唯一参数尚未解析( Context.LAYOUT_INFLATER_SERVICE )。

getSystemService中它可以正常工作,但在OnBindViewHolder中却并非如此。

ViewPagers

预期结果是创建新行,如果它在正常活动中,它将起作用。

2 个答案:

答案 0 :(得分:0)

步骤1:将apply参数添加到import numpy as np import pandas as np df = pd.DataFrame() df['a'] = np.zeros(10) # or get data from somewhere else p = 2/7 df.a.apply(lambda x: np.nan if np.random.rand() < p else np.random.rand()) 子类的构造函数中。

步骤2:创建LayoutInflater时,请传递您从RecyclerView.Adapter获得的RecyclerView.Adapter上的活动信息。

例如,这是一个名为LayoutInflater的{​​{1}}:

getLayoutInflater()

请注意,我的RecyclerView.Adapter构造函数将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 class ColorAdapter(private val inflater: LayoutInflater) : ListAdapter<Int, ColorViewHolder>(ColorDiffer) { override fun onCreateViewHolder( parent: ViewGroup, viewType: Int ): ColorViewHolder { return ColorViewHolder(inflater.inflate(R.layout.row, parent, false)) } override fun onBindViewHolder(holder: ColorViewHolder, position: Int) { holder.bindTo(getItem(position)) } private object ColorDiffer : DiffUtil.ItemCallback<Int>() { override fun areItemsTheSame(oldColor: Int, newColor: Int): Boolean { return oldColor == newColor } override fun areContentsTheSame(oldColor: Int, newColor: Int): Boolean { return areItemsTheSame(oldColor, newColor) } } } 的活动:

ColorAdapter

由于您和我都在用Kotlin写作,因此private val inflater: LayoutInflater调用变成了对活动上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.* class MainActivity : AppCompatActivity() { private val random = Random() override fun onCreate(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()) } } } private fun buildItems() = generateSequence { random.nextInt() } .take(25) .toList() } 时,我使用getLayoutInflater()来传递layoutInflater的实例。

答案 1 :(得分:0)

另一种解决方案是从LayoutInflater ViewGroup获取parentLinearLayout

示例

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

此外,请注意添加过多视图而不回收视图的后果。如果数量足够大,可能还需要另一个RecyclerView