0

I wanted to create a ListView that cannot scroll and I found a great deal of help from discussions here and here. If I understood correctly the trick is to ignore all MotionEvent.ACTION_MOVE events.

This works perfectly, the only issue I have is, my objects in ListView change their background based on their state.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/button_selected" android:state_pressed="true" />
  <item android:drawable="@drawable/button_selected" android:state_focused="true" />
</selector>

Now there is a scenario where user wants to press an item in ListView, but then changes his mind and moves his finger out of the view. The item he was pressing on still stays selected. I tried clearing their pressed state when handling Move event, but I guess I have to considering something else as well.

if (actionMasked == MotionEvent.ACTION_MOVE) {
    setPressed(false);
    return true;
}
Community
  • 1
  • 1
  • Are you sure you want to handle the pressed status, not checked? – H.A.H. Mar 07 '17 at 09:20
  • Yes, maybe I didn't state it clearly enough. There is a scenario when user holds on the ListView item and then changes his mind and moves his finger out of the ListView. Now the item he was holding down on is still stays pressed. I don't want for it to stay pressed. – aestheticfish Mar 07 '17 at 09:43
  • In the example you gave, they call invalidate. – H.A.H. Mar 07 '17 at 12:01
  • That's true, they call setPressed(false) and invalidate(), but its called for event ACTION_UP. I tried both of them in event ACTION_MOVE, but the result did not change. – aestheticfish Mar 07 '17 at 12:43
  • Yes. but in your selector you lack that row for state_pressed="false". Try adding one, and together with invalidating it. – H.A.H. Mar 07 '17 at 12:53
  • I added a new drawable with state_pressed="false" and return invalidate() to event move. The result is still the same. – aestheticfish Mar 07 '17 at 13:39
  • I have activated true, pressed true, pressed false in selector, in that order. Also, when I click on item, It lights, then when I move out and release it darkens. I used onTouchListener to disable scrolling. – H.A.H. Mar 07 '17 at 13:44
  • You are right, I tried this and it solved my problem. Thank you. – aestheticfish Mar 08 '17 at 08:54

1 Answers1

0

You can use listViewItems.setChoiceMode(ListView.CHOICE_MODE_SINGLE); or set android:choiceMode="singleChoice" inside ListView in xml.

pawar
  • 70
  • 2
  • 11
  • This isn't the issue. I have only one item selected at all times, the issue is that the item stays selected even if we move our finger out of the view. I believe I stated my issue wrongly in the post, so I changed it. Should be more clear what my issue is now. – aestheticfish Mar 07 '17 at 09:46
  • I think you forgot to handle android:state_pressed="false" inside your tag. You have to provide a state when you move your finger away i.e. – pawar Mar 07 '17 at 10:17
  • I don't think this would make much of a differences since this implementation works if I don't override ListView's move events. I will still give it a try once I get home. – aestheticfish Mar 07 '17 at 11:09