[英]How to deselect a row of a Recycler View
I'm trying to deselect the selected rows of a Recycler View.我正在尝试取消选择 Recycler View 的选定行。
I've been using manager.findViewByPosition(position);
我一直在使用manager.findViewByPosition(position);
to get the views in recycler view, but the problem is this method returns only the views that are appearing on the screen.获取回收站视图中的视图,但问题是此方法仅返回出现在屏幕上的视图。 I want to get recycler views that are offscreen also.我也想获得屏幕外的回收站视图。
Is there any other way to solve this this problem?有没有其他方法可以解决这个问题?
This is a simple way to achieve what you are trying to do, there are several ways to do this but I hope this quick example helps you get started.这是实现您想要做的事情的简单方法,有几种方法可以做到这一点,但我希望这个快速示例可以帮助您入门。
If you struggle to understand the logic, I would ask you to post your recycler code so I can help you better.如果您难以理解逻辑,我会要求您发布您的回收站代码,以便我可以更好地帮助您。
class MyAwesomeRecycler : RecyclerView.Adapter<MyAwesomeRecycler.ViewHolder>() {
val myAwesomeStrings = arrayOf("...")
val selectedPositions = arrayListOf<Int>()
class ViewHolder(var view: View): RecyclerView.ViewHolder(view){
//TODO
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
TODO("Not yet implemented")
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.view.setOnLongClickListener {
if(selectedPositions.contains(position)) {
selectedPositions.remove(position)
} else {
selectedPositions.add(position)
}
true
}
if(selectedPositions.contains(position)){
holder.someView.text = "I'm selected"
} else {
holder.someView.text = "I'm not selected"
}
}
override fun getItemCount(): Int {
return myAwesomeStrings.size
}
}
selectedPositions
array will contain the index of the rows that the user selected. selectedPositions
数组将包含用户选择的行的索引。
Let me know if you don't understand the logic behind this.如果你不明白这背后的逻辑,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.