How to keep the selected item color fixed

Asked

Viewed 1,016 times

4

Adding the Istener to the Listview through the method setOnItemClickListener, every time you click on one of your items, it momentarily changes color. Is there any way to keep this color fixed on the item that is clicked ?

  • Are you referencing the click effect? @Vitormendanha

  • Is wearing a custom view to your list item?

  • Are you using Array adapter?

  • I’m using a class that extends Basedapter to build the Listview items. The "setOnItemClickListener" method executes it in the Mainactivity class right after the "setAdapter" method":

  • <code>listview.setAdapter(new Listviewadapter(this, arrayList)); listview.setOnItemClickListener(new Adapterview.Onitemclicklistener() { @Override public void onItemClick(Adapterview<?> Parent, View view, int position, long id) { Toast.makeText(Mainactivity.this, "List " + position, Toast.LENGTH_SHORT).show(); } });<code/>

1 answer

2


1 - Add the attribute android:choiceMode à Listview,

to allow selecting only one line:

android:choiceMode="singleChoice"

to select more than one line:

android:choiceMode="multipleChoice"

2 - Add the following attribute to layout of list item:

android:background="?android:attr/activatedBackgroundIndicator"

If you want customize the appearance of the selection create, in the folder res/drawable, one selector to that end:

xml listselector.

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

   <item android:drawable="@android:color/holo_green_light" android:state_pressed="true"/>
   <item android:drawable="@android:color/holo_purple" android:state_selected="true"/>
   <item android:drawable="@android:color/holo_purple" android:state_activated="true"/>

</selector>

Change the colors to your liking!

The attribute background of the list item should be declared as:

android:background="@drawable/listselector"

EDIT after comments

How are you using more than one color for the backgroud of the Listview lines, it is necessary to define a Selector for each of them:

To set the default background color is created in the Selector an item without "State", this item has to be the last of the Selector:

listselector_par.xml

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

   <item android:drawable="@android:color/holo_green_light" android:state_pressed="true"/>
   <item android:drawable="@android:color/holo_purple" android:state_selected="true"/>
   <item android:drawable="@android:color/holo_purple" android:state_activated="true"/>

    <!-- cor do fundo quando não seleccionada (cor por defeito) -->
    <item android:drawable="@android:color/holo_green_dark"/>
</selector>

Create another Selector, listselector_impar.xml, another color indicated for the background.

In the Adapter attribute to background its Selector as the line is even or odd.

Note:
If you don’t like the animation effect delete the line:

android:exitFadeDuration="@android:integer/config_mediumAnimTime"

or change the value config_mediumAnimTime

  • ramaral, now I came up with the following problem: If the click is long through the method "setOnItemLongClickListener" the color is not fixed in the selected item again. You know the solution to this problem?

  • 1

    Declare a variable like this: final Boolean vemDoLongClick = false;. Change return true for return false and in the previous line do vemDoLongClick = true; at the beginning of the method onItemClick() take the test like this if(vemDoLongClick){vemDoLongClick = false; return;}

  • ramaral, your logic worked perfectly! Again a big thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.