gpt4 book ai didi

android - 如何从适配器外部访问 Recyclerview.ViewHolder 参数?

转载 作者:行者123 更新时间:2023-12-02 21:28:28 25 4
gpt4 key购买 nike

我已将 recyclerview 附加到 SnapHelper。然后我为recyclerview制作了scrollListener,每次该项目被捕捉到显示器的中心时,我都会找到捕捉到的 View ,然后我将通过finViewHolderForAdapterPosition()获取其viewHolder实例。现在我想要的是访问 ViewHolder 中创建的自定义变量。有什么办法可以访问它吗?基本上这个变量是在 onBindViewHolder() 中设置的,它包含自定义对象,其统计信息显示在这个快照 View 中。所以我想从该 View 中提取该 json 对象。

//inside onBindViewHolder()
holder.addressEntity = it

我已经尝试过,但未检测到此变量。仅可访问 itemView(作为参数在构造函数中发送的变量)。

addressRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
val centerView = snapHelper.findSnapView(addressRecyclerView.layoutManager)
centerView!!.setBackgroundResource(R.drawable.payment_button_active)
lastSnappedView = centerView
val childViewHolder: RecyclerView.ViewHolder = recyclerView!!.findViewHolderForAdapterPosition(viewManager.getPosition(centerView))
val selectedViewAddress: Address = childViewHolder.addressEntity //this doesnt work -- selectedViewAddress is red
AndroidAssets.getInstance(context!!).deliveryAddress = selectedViewAddress

} else if (lastSnappedView != null){
lastSnappedView!!.setBackgroundResource(R.drawable.payment_button_inactive)
lastSnappedView = null
}
}
})

ViewHolder 位于适配器内部作为内部类:

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

//address item views
var addressEntity: Address? = null
val cityName: TextView = itemView.findViewById(R.id.cityName)
val streetName: TextView = itemView.findViewById(R.id.streetName)
val addressIcon: ImageView = itemView.findViewById(R.id.addressIcon)
}

最佳答案

你只需要把持有者变成你自己的,所以

val myHolder = (searchedHolder as ViewHolder)
//Access custom info with variable

关于android - 如何从适配器外部访问 Recyclerview.ViewHolder 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51479761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com