Когда я нажимаю на элементы, ничего не происходит, я пытаюсь изменить цвет, отобразить количество выбранных элементов, отобразить журналы. Ничего не происходит
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
}
}
Мой адаптер
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)
}
}
Спасибо
Решение : Трекер работает Iдобавил setOnClickListener на мой взгляд, как предлагает Khaled Qasem
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