Highlight the selected image in the HorizantalListView in android, and it will remain selected until another image is clicked.

I work with HorizantalListView, but when I knocked on any element of the list, I want to show it selected and it should remain selected until another image is clicked, as possible, please help me.

enter image description here

+4
source share
3 answers

Make it a toggle button instead of an image. And then create a selector in xml with the different images you want in each matching state. (I'm not sure if this can also be done using imageView, why I told you to use the toggle button).

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/> <item android:state_pressed="false" android:drawable="@drawable/button_rested"/> <item android:state_enabled="false" android:drawable="@drawable/button_disabled"/> </selector> 

on the bar

 <ToggleButton android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@null" android:gravity="center_horizontal" android:src="@drawable/button_selector"/> 
+4
source

Here's how I did it - my images came from a cursorAdapter, so

1.) A function is written in the adapter

 /*** * To change the colour of list item selected * * @param position */ public void setSelected(int position) { selectedPosition = position; } 

2.) In a BindView for a class that inherits a cursor adapter

 // Change the background color if (x == selectedPosition) { holder.title.setBackgroundResource(R.color.red); } 

If your images are fixed, you can simply use the selector in your xml layout

+1
source

You can do this using XML using selectors or programmatically using onItemClickListener (..)

One incomplete example:

item_selector.xml

 <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /> <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/list_selector_background_transition" /> 

And instead of ImageViews, use ImageButtons instead.

 <ImageButton android:id="@+id/icon" android:layout_width="50px" android:layout_height="wrap_content" android:src="@drawable/item_selector"/> 
+1
source

All Articles