Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think a better approach is to tell the <code>GridView</code> that you wish to support selecting (checking) the items:</p> <pre><code>gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE); </code></pre> <p>and then make sure that <strong>items in GridView implement <a href="http://developer.android.com/reference/android/widget/Checkable.html" rel="noreferrer">Checkable</a> interface</strong>. That means that the items can be either <code>Checkbox</code>, <code>ToggleButton</code> and so on or you can add the <code>Checkable</code> support yourself - for example make RelativeLayout checkable. (See the example below.)</p> <p>In contrast to the other answer most of the work is taken care of by the GridView itself - no <code>onClickListener</code> is needed. Instead of storing the state yourself, just call <code>gridView.getCheckedItemIds()</code> or similar method.</p> <hr> <p>To make <code>RelativeLayout</code> (or anything) checkable make a subclass of it:</p> <pre><code>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); } } </code></pre> <p>Notice that the method <code>onCreateDrawableState</code> 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.</p> <p>Then use the <code>CheckableRelativeLayout</code> as the top view for items in the GridView:</p> <pre><code>&lt;foo.bar.CheckableRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/my_awesome_background" ... more stuff &gt; ... content of the relative layout &lt;/com.test.CheckableRelativeLayout&gt; </code></pre> <p>And define how the background changes when the item is checked in <code>res/drawable/my_awesome_background.xml</code>:</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:state_checked="true" &gt; &lt;!-- This applies when the item is checked. --&gt; &lt;shape android:shape="rectangle" &gt; &lt;solid android:color="#A8DFF4" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item&gt; &lt;!-- This applies when the item is not checked. --&gt; &lt;shape android:shape="rectangle" &gt; &lt;solid android:color="#EFEFEF" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;/selector&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload