当我单击项目时,没有任何反应 我尝试更改颜色,以显示所选元素的数量,以显示日志 什么都没发生
MyViewHolder
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val name: TextView = view.list_item_name
val phone: TextView = view.list_item_phone
// More code here
fun getItemDetails(): ItemDetailsLookup.ItemDetails<Long> =
object : ItemDetailsLookup.ItemDetails<Long>() {
override fun getSelectionKey(): Long? = itemId
override fun getPosition(): Int = adapterPosition
}
}
我的ItemDetailsLookup
class MuLookup(private val rv: RecyclerView) : ItemDetailsLookup<Long>() {
override fun getItemDetails(event: MotionEvent): ItemDetails<Long>? {
val view = rv.findChildViewUnder(event.x, event.y)
if (view != null) {
return (rv.getChildViewHolder(view) as MyViewHolder)
.getItemDetails()
}
return null
}
}
我的适配器
class PersonAdapter(
private val listItems: List<Person>,
private val context: Context
) : RecyclerView.Adapter<MyViewHolder>() {
init {
setHasStableIds(true)
}
private var tracker: SelectionTracker<Long>? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(
LayoutInflater.from(context)
.inflate(R.layout.list_item_jf, parent, false)
)
}
override fun getItemCount(): Int {
return listItems.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.name.text = listItems[position].name
holder.phone.text = listItems[position].phone
val parent = holder.name.parent as LinearLayout
if (tracker!!.isSelected(position.toLong())) {
parent.background = ColorDrawable(
Color.parseColor("#80deea")
)
} else {
Timber.i("zeze ${tracker!!.selection}")
// Reset color to white if not selected
parent.background = ColorDrawable(Color.WHITE)
}
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
fun setTracker(tracker: SelectionTracker<Long>?) {
this.tracker = tracker
}
}
我的活动
class MainActivity : AppCompatActivity() {
private var tracker: SelectionTracker<Long>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState != null)
tracker?.onRestoreInstanceState(savedInstanceState)
val myList = listOf(
Person("Alice", "555-0111"),
Person("Bob", "555-0119"),
Person("Carol", "555-0141"),
Person("Dan", "555-0155"),
Person("Eric", "555-0180"),
Person("Craig", "555-0145")
)
my_rv.layoutManager = LinearLayoutManager(this)
my_rv.setHasFixedSize(true)
val adapter = PersonAdapter(myList, this)
my_rv.adapter = adapter
tracker = SelectionTracker.Builder<Long>(
"selection-1",
my_rv,
StableIdKeyProvider(my_rv),
MuLookup(my_rv),
StorageStrategy.createLongStorage()
).withSelectionPredicate(
SelectionPredicates.createSelectAnything()
).build()
tracker?.addObserver(
object : SelectionTracker.SelectionObserver<Long>() {
override fun onSelectionChanged() {
val nItems: Int? = tracker?.selection?.size()
if (nItems != null && nItems > 0) {
// Change title and color of action bar
title = "$nItems items selected"
supportActionBar?.setBackgroundDrawable(
ColorDrawable(Color.parseColor("#ef6c00"))
)
} else {
// Reset color and title to default values
title = "RVSelection"
supportActionBar?.setBackgroundDrawable(
ColorDrawable(Color.parseColor("#126c00"))
)
}
}
})
adapter.setTracker(tracker)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
tracker?.onSaveInstanceState(outState)
}
}
谢谢
解决方案:Tracker的工作原理是像Khaled Qasem建议一样在视图上添加了setOnClickListener
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.name.text = listItems[position].name
holder.phone.text = listItems[position].phone
val parent = holder.name.parent as LinearLayout
holder.name.setOnClickListener {
Timber.i("zeze ici")
if (tracker!!.isSelected(position.toLong())) {
Timber.i("zeze la")
parent.background = ColorDrawable(
Color.parseColor("#80deea")
)
} else {
Timber.i("zeze ${tracker!!.selection}")
// Reset color to white if not selected
parent.background = ColorDrawable(Color.WHITE)
}
}
}
颜色变化和项目数显示在supportActionBar
答案 0 :(得分:1)
代替此操作,您只需要在创建视图持有者时添加一个Click Listener:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val myViewHolder = MyViewHolder(
LayoutInflater.from(context)
.inflate(R.layout.list_item_jf, parent, false)
)
myViewHolder.setOnClickListener {
// Add what you what you need here
}
return myViewHolder
}
答案 1 :(得分:0)
在我的情况下,解决方案是告诉用户长按第一个选择:
长按可进行第一个设置,然后点击其他设置进行多次 选择
我的实现方式有所不同,但是我还观察到SelectionTracker从我的RecyclerView中什么也没选择。我从某些SelectionTracker example code中无法理解它应该如何工作。该示例显示了ViewHolder回收后如何设置高光:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val number = list[position]
tracker?.let {
holder.bind(number, it.isSelected(position.toLong()))
}
}
fun bind(value: Int, isActivated: Boolean = false) {
text.text = value.toString()
itemView.isActivated = isActivated
}
但是当用户点击该行时,isActivated
应该如何设置为true?
答案是用户不应该点击;用户应该长按:
要开始选择项目,我们必须先长按任意一个项目以激活多重选择模式。
答案 2 :(得分:0)
问题在于,只有当最初选择一项时,才激活Recyclerview选择,然后您只需轻按即可选择其他项。
如果您始终需要用户选择一个项目或选择一个项目,则可以使用selectionTracker
最初从recyclerview中选择一个项目:
selectionTracker.select(someItemId)
现在,您只需单击列表即可从列表中选择其他项目。