[英]How to access Recyclerview.ViewHolder params from outside of adapter?
I have recyclerview attached to SnapHelper. 我已将cyclerview附加到SnapHelper。 Then I made scrollListener for recyclerview and everytime the item is snapped to the center of the display, I will find that snapped view, then I will get its viewHolder instance by finViewHolderForAdapterPosition(). 然后,我为recyclerview制作了scrollListener,并且每次将该项目捕捉到显示的中心时,我都会找到该捕捉的视图,然后通过finViewHolderForAdapterPosition()获得其viewHolder实例。 Now what I want is to access custom variable created inside ViewHolder. 现在,我要访问的是在ViewHolder中创建的自定义变量。 Is there any way to access it? 有什么办法可以访问它吗? Basically this variable is set in onBindViewHolder() and it's containing custom object which stats are shown in this snapped view. 基本上,此变量是在onBindViewHolder()中设置的,它包含自定义对象,其统计信息显示在此快照视图中。 So I want to extract that json object from that view. 所以我想从那个视图中提取那个json对象。
//inside onBindViewHolder()
holder.addressEntity = it
I've tried this, but this variable is not detected. 我已经尝试过了,但是没有检测到此变量。 Only itemView (variable sent in constructor as parameter) is accessible. 只能访问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 inside adapter as inner class: 适配器内部的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)
}
You just need to cast the holder as your own, so 您只需要将所有者转换为自己的所有者,因此
val myHolder = (searchedHolder as ViewHolder)
//Access custom info with variable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.