When I click on the items nothing happens I try to change the color, to display the number of selected elements, to display logs Nothing is happening
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
}
}
My 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
}
}
My adapter
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
}
}
My activity
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)
}
}
Thank you
Solution: Tracker works I added a setOnClickListener on my view like Khaled Qasem suggest
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)
}
}
}
Color change et number of items is displayed in supportActionBar
The problem is that Recyclerview selection is activated only when one item has initially been selected, then you can select other items by simply tapping them.
If you will always need the user to select an item, or to have an item selected, you can use the selectionTracker
to initially select one item from the recyclerview:
selectionTracker.select(someItemId)
Now, you can simply select other items from the list by single tapping them.
In my case, the solution was to tell the user to long-press the first selection:
Long-press for first setting, then tap other settings for multiple selection
My implementation was different but I also observed that the SelectionTracker selected nothing from my RecyclerView. I could not understand from some SelectionTracker example code how it was supposed to work. The example showed how the highlight could be set when the a ViewHolder got recycled:
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
}
But how is isActivated
supposed to be set to true when the user taps the row?
The answer is the user is not supposed to tap; the user is supposed to long-press:
To start selecting items we have to activate first the multi selection mode by long pressing on any item.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With