Question

What I want to achieve:

  • A list item is highlighted when the user is pressing it
  • When the user stops pressing it, the list item:
    • remains highlighted, if it was unselected before the pressing
    • loses the highlight, if it was selected before the pressing
  • Long clicks behave the same way as the user stopping the pressing (changing the background of the item depending on its previous state)
  • Scrolling the list, without pressing any specific item, should not highlight any item

Details:

  • For what I read, I think that behaviour could be achieved using list selectors and the state android:state_activated, but this state was introduced in API level 11. The solution I am looking for has to work in API level 10
  • I think that solutions relying on click (onItemClick, onClick...) will not work, because the click is triggered after the user stops the pressing, not when he starts it (like the pressed state does). Changing the highlight of an item using android:state_pressed is not persistent (it will change back after the press is finished) and changing it in android:state_pressed and making it persistent on click will produce a flicker
  • A good example of app that achieves that in API level 10 is Tasks. Just click or long click on items in the list to see the desired behaviour

So, anybody has already solved that? Any idea on how the Tasks app does it?

Was it helpful?

Solution 2

I finally solved that issue implementing my own list (PressListView) extending ListView. PressListView has a callback to notify the user when an item on the list has been pressed.

Knowing when the items are pressed (which was the most difficult part), the only thing left is handling in a proper way the backgrounds of the item, which you can do in your own Adapter using selectors.

For those interested on seeing this fully working, take a look at my demo project.

You can launch the CAB performing a long click in an item, and once in CAB you can test the highlighting of items, both with touches and D-pad/trackpad.

Although my solution works as I wanted, it is still slower than the list in the Tasks app, when changing the state of the items. If you try to select two items really fast, most of the times it will not select one of the items on my example, but it will in Tasks. If someone knows what it can be, I would be extremely grateful!

OTHER TIPS

You probably want to set a OnTouchListener(in the getView method) on the row View. That way you'll see the MotionEvent for the first touch(the MotionEvent.ACTION_DOWN action) and can set the selection:

    private OnTouchListener mTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                v.setBackgroundColor(Color.RED); // set the selection
            }
            // don't forget to store this new status so the adapter will see
            // it. Otherwise you'll have some problems when the list will be
            // scrolled.
            return false;
        }
    };

Implement ActionMode on your ListView (see second link below). In ActionMode, the ListView keeps track of the item checked state automatically when the user clicks on an item. When you are using an adapter for your ListView, you set the background of an item based on the checked state:

@Override
public void bindView(final View view, final Context context, final Cursor cursor)
{
    int pos = cursor.getPosition();

    boolean selected = ((SessionsActivity)context).listView.isItemChecked(pos);
    if(!selected)
        view.setBackgroundResource(R.drawable.list_selector);
    else
        view.setBackgroundResource(R.drawable.list_selector_active);
...

AND, you also need to invalidate the ListView, after each item click:

 private AbsListView.MultiChoiceModeListener multiChoiceModeListener = new AbsListView.MultiChoiceModeListener()
    {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position,
                                              long id, boolean checked)
        {
            // Here you can do something when items are selected/de-selected,
            // such as update the title in the CAB
            listView.invalidateViews();
        }

See https://stackoverflow.com/a/50639298/2477937 and https://medium.com/over-engineering/using-androids-actionmode-e903181f2ee3

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top