prabu
prabu

Reputation: 727

How to change the listview's item color and keep it longer in android?

I'm working with Customized list-view which is in Slidepanel layout.

Here i'm trying to change the color of that listview's each ITEM when the particular item is being clicked. It is working fine, but here what i need is, i want to keep that changed color to be displayed untill i click the another item from the listview.

How to do this?Suggestions please.

Thanks for your precious time!...

Note : From the below code, i've set the list_row_selector.xml as the background for that customized listview.

list_row_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<!-- Selected --> 
 <item 
android:state_focused="true" 
android:state_selected="false" 
android:drawable="@drawable/focused"/> 

<!-- Pressed -->
<item 
android:state_selected="true" 
android:state_focused="false"
 android:drawable="@drawable/selected" /> 

</selector>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<drawable name="focused">#ff5500</drawable>
<drawable name="selected">#FF00FF</drawable>
</resources>

Upvotes: 0

Views: 89

Answers (1)

Shivam Verma
Shivam Verma

Reputation: 8023

Inside your adapter, create an instance variable int selectedPosition = -1;

Then inside the getView() method check if selectedPosition == position of that element, set the list_row_selector as the background else, set a normal background.

Also, update the value of selectedPosition whenever an item is clicked and call notifyDataSetChanged() in the adapter to refresh the list.

Upvotes: 2

Related Questions