正确在RecyclerView适配器中使用的上下文 [英] Correct context to use in RecyclerView adapter
问题描述
在RecyclerView
适配器中使用SharedPreference
时应该使用哪个context
?这很令人困惑,因为在默认设置中,val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
中的context
显示为未解析的引用.
Which context
should I be using when I use a SharedPreference
within a RecyclerView
adapter? This is confusing as when during the default one, context
in val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
appears as an unresolved reference.
class MyRVAdapter(private val myString: ArrayList<String>): RecyclerView.Adapter<MyRVAdapter.MyViewHolder>() {
private val typeA = 1
private val typeB = 2
override fun onCreateViewHolder(parent: ViewGroup, type: Int): MyViewHolder {
return when (type) {
typeA -> MyViewHolder(inflateHelper(R.layout.rv_type_a, parent))
typeB -> MyViewHolder(inflateHelper(R.layout.rv_type_b, parent))
else -> MyViewHolder(inflateHelper(R.layout.rv_type_a, parent))
}
}
override fun onBindViewHolder(viewHolder: MyViewHolder, position: Int) {
if (getItemViewType(position) == typeA) {
// Check preference (what's the correct context?)
val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
valueDestinationExpanded = mSharedPreferences.getBoolean("my_preference", true)
}
else if (getItemViewType(position) == typeB) {
}
}
private fun inflateHelper(resId: Int, parent: ViewGroup): View {
return LayoutInflater.from(parent.context).inflate(resId, parent, false)
}
override fun getItemViewType(position: Int): Int {
return if (position == 0) typeA
else typeB
}
}
片段
myTV不能为空
myTV must not be null
class MyFragment : androidx.fragment.app.Fragment() {
private lateinit var mRecyclerView: RecyclerView
private var valueShowTextView: Boolean = false
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.my_rv, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
val myTV = my_tv
mRecyclerView = v.mRecyclerViewSansToolbar
mRecyclerView.layoutManager = LinearLayoutManager(activity)
val mAdapter = MyRVAdapter(dataTVtext!!)
val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
valueDestinationExpanded = mSharedPreferences.getBoolean("preference_DestinationExpand", true)
if (valueShowTextView) {
myTV.visibility = View.VISIBLE
} else {
myTV.visibility = View.GONE
}
mRecyclerView.adapter = mAdapter
super.onActivityCreated(savedInstanceState)
}
}
推荐答案
共享首选项用于持久存储数据,并不完全适合在RecyclerView中使用,因为它的操作会降低应用程序的速度.请参阅开发人员文档,了解有关为什么不一定要使用它的更多信息.以这种方式以及最佳应用方式.
Shared Preferences is used for persisting data and isn't entirely suitable for use within a RecyclerView as its operations can slow down the application. See the developer documentation for more info on why it shouldn't necessarily be used in this way and where it is best applied.
在您的情况下,最好在适配器初始化之前的某个时间从SharedPreferences
检索数据,并在初始化期间传递数据.
In your case, it may be preferable to retrieve the data from SharedPreferences
at some point prior to the initialization of your Adapter and pass it during initialization.
假设性能不是很关键,则可以像以前一样继续进行操作,只需使用viewHolder.itemView.context
从其中一个视图中检索上下文.您还可以将上下文引用从活动传递给适配器.
Assuming performance is not critical, you can continue doing it as before and simply retrieve a context from one of your views with viewHolder.itemView.context
. You could also pass a context reference from your activity to the adapter.
这篇关于正确在RecyclerView适配器中使用的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!