I have a list view where the list items have the following background drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/transparent" />
<item android:state_pressed="true"
android:drawable="@drawable/collection_item_gradient" />
</selector>
When the list item is pressed, it applies the gradient. However, if the user then initiates a multitouch event (say by putting an additional finger down somewhere outside the list view) it is possible that the list item remains in the pressed state until another touch event is received by the list view.
Is there a way around this?
It depends somewhat on what you are looking to accomplish…
If you are looking to display a third background type when the user has two fingers down, there is no drawable state that you can reference directly to accomplish. You would have to co-op one of the existing states that is not often used in touch mode (such as the selected state), and implement a custom touch handler on the list item’s object to set that state. For example, implement
onTouchEvent()in each list item’s view (or add anonTouchListener()if you don’t have a custom class), look for theACTION_POINTER_DOWNevent to tell you that a second finger came down, and callsetSelected()on the view to trigger a drawable you have set for theandroid:state_selectedin your<selector>.If you just want the state to clear under this case, you can cancel the touch event when more than one finger is detected. In this case, you are looking at customizing the
ListView.onInterceptTouchEvent()method to look for theACTION_PONTER_DOWNevent and returntrue. This will teach theListViewto steal the touch back from it’s child item with a second finger, which will cancel the pressed state of the list item. Make sure to call back through tosuperin this case so you don’t break all the existing logicListViewuses for scrolling, etc.Beware of cases where the existing
AbsListViewimplementation may get in your way here. For example, the current implementation ofonIntercepTouchEvent()looks forACTION_POINTER_UPin order to switch the main touch finger from the first to the second. This behavior may cause strange artifacts in your customization if you aren’t aware of it.HTH.