I'm using a custom SimpleOnGestureListener to detect long press and single tap up and I'm implementing that on my ListView which has a selector on it.
Now, I am starting a contextual action mode when an item in the list is long pressed and after that for each call to singleTapUp I'm adding the item index to a set of selected items. I've also implemented toggle logic using the set which is quite rudimentary.
While toggling, I also check if that index is mapped to a boolean value in the Sparse Boolean Array I get from calling
getCheckedItemPositions()
on the ListView. If the index is not in selected items I add it and call
listView.setItemChecked(index, true)
otherwise I remove it from the set and call
listView.setItemChecked(index, false)
Now my problem is the first item that starts the Action Mode callback when long pressed doesn't hold on to its activated background whereas all the subsequent items I click after that do.
I also observed that if I don't call
setItemChecked()
explicitly then I have to long press every item for it to retain the activated background and single clicking items doesn't change the background.
Can someone help me figure out a clean approach to doing what I want to achieve? Any help will be much appreciated.
Here's the selector I'm using --->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true"
android:drawable="@drawable/blue_highlight"/>
<item android:state_activated="false"
android:drawable="@drawable/plank_idle"/>
</selector>
Okay, finally after a lot of head scratching turns out to be a case of bad design so I thought I'll post the answer in case anyone else is accidentally doing this wrong.
I should never have used SimpleOnGestureListener for detecting such basic events as onSingleTapUp() and onLongPress() whose alternatives are found in the AdapterView class which are OnItemClickListener() and OnItemLongClickListener() respectively.
Using these listeners the states are handled effectively by the AdapterView and the problem never arises.