Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android gridview keep item selected

Tags:

I have a GridView with multiple items, but the items must be kept selected once the the onClickListener is called.How can i achive this?

I'v already tried v.setSelected(true) but it doesnt seem to work.

gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            // Toast.makeText(Project.this, "Red" + position,
            // Toast.LENGTH_SHORT).show(); //position = al catelea element
            v.setPressed(true);
            if (bp == 2) {
                if (position == 0) {
                Square.setSex(R.drawable.girl_body2v);
                Square2.setHair(R.drawable.girl_hair_01v);
                SquareAccesories.setAcc(R.drawable.girl_accessories_01v);
                SquareEyes.setEyes(R.drawable.eyes_1v);
                SquareLips.setLips(R.drawable.lip_1v);
                Square3.setDress(R.drawable.girl_tops_01v);
                SquareShoes.setShoes(R.drawable.girl_shoes_01v);
                SquarePants.setPants(R.drawable.girl_bottom_01v);
                setS(2);

This is a small part of the code for the onClickListener because i have lots of cases.

like image 461
Dorin Rusu Avatar asked Jul 04 '12 09:07

Dorin Rusu


1 Answers

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>
like image 139
Strix Avatar answered Oct 31 '22 04:10

Strix