Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight multiple items in a list view?

Hello guys I want to highlight multiple items in a list view.

So I tried SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); but it didn't help

I am using a custom adapter and extending BaseAdapter

I am using ListView and not AbsListView

I don't want to use CAB because it doesn't go well with the design of my app

I don't want to use the getView method of the adapter as well.

I don't want to use checkboxes either, I guess I will use a boolean for each item and pass it to getviews if I don't get a solution here, but that doesn't seem too elegant and neat. I believe there is a proper built-in way of doing it without using getview() of the adapter

I tried:

android:drawSelectorOnTop="false"
android:listSelector="@android:color/darker_gray"

in the xml but it is highlighting only one of the items and as soon as I click on another item, it highlights it instead...

So is there any proper way of doing it?

Here is how my app looks:

enter image description here

like image 857
Ankit Srivastava Avatar asked Apr 29 '14 13:04

Ankit Srivastava


2 Answers

You can make the same logic as CAB but avoid using CAB.

Your list item should have the FrameLayout at root like

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="?android:attr/activatedBackgroundIndicator">

....

Set onItemClickListener to change choice mode on long press

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        if (mInMultiChoiceMode) {
            // if already in multi choice - do nothing
            return false;
        }

        mInMultiChoiceMode = true;
        // set checked selected item and enter multi selection mode
        final AbsListView list = (AbsListView) arg0;
        list.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        list.setItemChecked(arg2, true);

        return true;
    }
});

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        if (mInMultiChoiceMode) {
            //exit multi choice mode if number of selected items is 0
            if (((AbsListView) arg0).getCheckedItemCount() == 0) {
                ((AbsListView) arg0).setChoiceMode(AbsListView.CHOICE_MODE_NONE);
                mInMultiChoiceMode = false;
            }
        } else {
            // do whatever you should as in normal non-multi item click
            System.out.println("CLICK");
        }
    }
});
like image 50
Yaroslav Mytkalyk Avatar answered Oct 17 '22 06:10

Yaroslav Mytkalyk


SngList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

It should be enough, but you have to use getView, to differentiate the selected and unselected state.

You can use isItemChecked() method to determine weather the item is selected or not, so you don't have to introduce a boolean variable for each items.

Edit:

Something like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ListView list = (ListView) parent;

    if(list.isItemChecked(position)){
        //...
    }
    else{
        //...
    }
like image 30
kupsef Avatar answered Oct 17 '22 07:10

kupsef