Reputation: 251
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?
Upvotes: 2
Views: 510
Reputation: 63303
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 an onTouchListener()
if you don't have a custom class), look for the ACTION_POINTER_DOWN
event to tell you that a second finger came down, and call setSelected()
on the view to trigger a drawable you have set for the android:state_selected
in 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 the ACTION_PONTER_DOWN
event and return true
. This will teach the ListView
to 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 to super
in this case so you don't break all the existing logic ListView
uses for scrolling, etc.
Beware of cases where the existing AbsListView
implementation may get in your way here. For example, the current implementation of onIntercepTouchEvent()
looks for ACTION_POINTER_UP
in 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.
Upvotes: 1