Android RecyclerView multi selection expand list items and change list items color
Hello Devs,
If you are previously working with ListView and recently shifted to RecyclerView then you maybe missing some great features like ItemClickListener
, MultipleItemSelection
, TouchRippleEffect
,EmptyView
and more.
But in RecyclerView we need to do this manually, so lets add MultiSelection functionality in RecyclerView
.
Lets start:
Description : So i created a boolean variable to check of multiple selection is active or not. and a List<Integer> selectedIds
containing values which are selected. In RecyclerView's onItemTouchListener
i used custom implementation of RecyclerView.OnItemTouchListener
, for handling click
and longClick
events.
On longClick we will check if multiSelection is false then set this to true.
On singleClick or LongClick check if currently selected item is in List<Integer> selectedIds
is list then remove that item from list, otherwise add that item in list and notify adapter for latest value. In adapter check if selectedItem match with current item in adapter then change backgroud or foreground color as per your choice.
in onBindViewHolder method we will check if our selected items list contain current item then change its color to desired color to show as selected item, otherwise set color to transparent to show this like unselected item.
<resources><color name="colorPrimary">#3F51B5</color><color name="colorPrimaryDark">#303F9F</color><color name="colorAccent">#FF4081</color><color name="colorActionMode">#009362</color><color name="colorControlActivated">#50FF4081</color></resources>
Quick Links
Legal Stuff
Social Media