Android gridview keep item selected

I think a better approach is to tell the GridView that you wish to support selecting (checking) the items:

gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);

and then make sure that items in GridView implement Checkable interface. That means that the items can be either Checkbox, ToggleButton and so on or you can add the Checkable support yourself – for example make RelativeLayout checkable. (See the example below.)

In contrast to the other answer most of the work is taken care of by the GridView itself – no onClickListener is needed. Instead of storing the state yourself, just call gridView.getCheckedItemIds() or similar method.


To make RelativeLayout (or anything) checkable make a subclass of it:

public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
    private boolean checked = false;
    private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };

    public CheckableRelativeLayout(Context context) {
        super(context);
    }

    public CheckableRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CheckableRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
         if (isChecked())
             mergeDrawableStates(drawableState, CHECKED_STATE_SET);
         return drawableState;
    }

    @Override
    public boolean isChecked() {
        return checked;
    }

    @Override
    public void setChecked(boolean _checked) {
        checked = _checked;
        refreshDrawableState();
    }

    @Override
    public void toggle() {
        setChecked(!checked);
    }

}

Notice that the method onCreateDrawableState updates the visual style. You don’t have to do it this way, you can for example directly change the background in the setChange method.

Then use the CheckableRelativeLayout as the top view for items in the GridView:

<foo.bar.CheckableRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/my_awesome_background"
    ... more stuff
    >
        ...  content of the relative layout
</com.test.CheckableRelativeLayout>

And define how the background changes when the item is checked in res/drawable/my_awesome_background.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
     <item android:state_checked="true" >
        <!-- This applies when the item is checked. -->
         <shape android:shape="rectangle"  >
             <solid android:color="#A8DFF4" />
         </shape>
     </item>

    <item>
        <!-- This applies when the item is not checked. -->
        <shape android:shape="rectangle"  >
             <solid android:color="#EFEFEF" />
         </shape>
     </item>
</selector>

Leave a Comment