Is there a way to keep a GridView item selected even when a different element in the activity is tapped?

I am writing an Android application in Kotlin in which a grid of items with a count associated to each item is displayed. When an item on the grid is selected, I can tap one of two buttons at the bottom of the screen, one to increment the count of the selected item by one, and one to decrement it by one.

I’ve used a list selector to keep the item I have selected highlighted, but it is invariably un-highlighted whenever I tap one of those buttons. however, the grid item is still selected, because its value still updates with the new incremented/decremented value whenever I tap one of those two buttons.

I’ve tried to change drawSelectorOnTop to false and use a variable that stores the value of the current position to set the color back through code, but it seems to reset whenever I call notifyDataSetChanged() on my adapter.

How do I keep the grid item I have tapped highlighted until another is selected? Is there an XML attribute I should be using to ensure it doesn’t disappear when something outside the grid is tapped? Any tips or answers related to solving this programmatically are fine if given in Java as well.

I’ve looked at a lot of answers about things like this but I haven’t found anything that works. My bad if there is a post out there with the answer I seek – in that case, a link would be appreciated. Thanks in advance! Relevant code is provided below.

Here’s the RelativeLayout for each individual grid element:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_margin="10dp"     android:background="@drawable/border_layout"     android:contextClickable="false"     android:defaultFocusHighlightEnabled="true"     android:orientation="horizontal">      <TextView         android:id="@+id/itemCount"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignTop="@+id/itemImage"         android:layout_marginStart="5dp"         android:layout_marginTop="38dp"         android:layout_marginBottom="5dp"         android:gravity="end|top"         android:text="@string/default_count_value"         android:textColor="#000000"         android:textSize="18sp" />       <TextView         android:id="@+id/itemName"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/itemImage"         android:layout_alignStart="@+id/itemImage"         android:layout_marginLeft="10dp"         android:layout_marginRight="10dp"         android:text="@string/kotlin_title"         android:textColor="#000000"         android:textSize="18sp" />      <ImageView         android:id="@+id/itemImage"         android:layout_width="100dp"         android:layout_height="100dp"         android:layout_margin="20dp"         android:layout_toEndOf="@+id/itemCount"         android:contentDescription="@string/entry_image_description" /> </RelativeLayout> 

Here’s the GridView itself:

    <GridView         android:id="@+id/gridView"         android:layout_width="match_parent"         android:choiceMode="singleChoice"         android:listSelector="@drawable/list_selector"         android:layout_height="match_parent"         android:layout_above="@+id/ButtonLayout"         android:background="?android:attr/activatedBackgroundIndicator"         android:gravity="center_vertical"         android:horizontalSpacing="10dp"         android:numColumns="2"         android:padding="5dp"         android:stretchMode="columnWidth"         android:verticalSpacing="10dp"> 

Here’s the list selector:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:drawable="@color/colorGrey" android:state_selected="true"/>     <item android:drawable="@color/colorGrey" android:state_activated="true"/> <!-- I have attempted to figure this out with this line omitted. -->     <item android:drawable="@color/colorGrey"/> </selector> 

This function is called whenever a count value is incremented or decremented. This is the only operation performed in the activity that changes an element of the view:

    private fun updateCountDisplay() {         val gridView = this.findViewById(R.id.gridView) as GridView         val itemView = gridView.getChildAt(selectedPosition)  // Obtain view of the selected position         itemView.setBackgroundResource(R.color.colorGrey) // I thought this might maintain the background color, but I was mistaken.         val editText = itemView.findViewById(R.id.itemCount) as TextView         val text = currentProject.itemList[selectedPosition].itemCount // Get item count value         editText.text = text.toString() // Set item count value         notifyValueChange() // If I add code to change the color after this, it stays highlighted, but it also doesn't un-highlight when I select a new position, so everything that is tapped stays highlighted :(     } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.