Note that there are some explanatory texts on larger screens.

plurals
  1. POGridView with CustomLayout Items and State
    primarykey
    data
    text
    <p>I'm working on a layout that needs to display a dynamic list of items. Each item has a defined UUID, a Name(banner text), an Image URL and a boolean to indicate if it's selected - I've also created a POJO to hold these. I created a custom layout to put the items in a grid view, two icons with banner text on it. It looks like this:</p> <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;RelativeLayout android:id="@+id/ItemLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;ImageView android:id="@+id/ItemImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:background="@android:color/transparent" /&gt; &lt;ImageView android:id="@+id/ItemOverlayImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:background="@android:color/transparent" /&gt; &lt;TextView android:id="@+id/ItemTextBanner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="#BF000000" android:gravity="center" android:textStyle="bold" /&gt; &lt;/RelativeLayout&gt; &lt;/FrameLayout&gt; </code></pre> <p>I've created the Custom Adapter and the activity as well and I'm having no issue getting all items read in, the image's downloaded and encoded to bitmaps and the text banner being set. The problem is I want the "selected" state and image to change if the item is clicked.</p> <p>I know I can add the <code>GridView.setOnItemOnClickListener</code> and inside I know which item as been selected as well as the parent view it comes from, but I'm not sure how to get it's "selected" state. <a href="https://stackoverflow.com/questions/11326089/android-gridview-keep-item-selected">This</a> link uses state from a static list in the Adapter and is what I've modeled after. I added the following method and called it from the <code>GridView.setOnItemOnClickListener</code>.</p> <pre><code>public void itemSelected(View parent, int position) { if(items.get(position).isSelected()) { ImageView interestSelected = (ImageView)parent.findViewById(R.id.ItemOverlayImage); interestSelected.setImageResource(0); } else { ImageView interestSelected = (ImageView)parent.findViewById(R.id.ItemOverlayImage); interestSelected.setImageResource(R.drawable.overlay); } } </code></pre> <p>The first problem is obvious, I'm not saving the state to my items array, but that is an easy fix. The real problem is only the first item is getting the overlay. So how do I alter the <code>R.id.ItemOverlayImage</code> for the item in the GridView selected and not just the first one? Or am I going about this totally wrong?</p>
    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.
 

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