Android ListView Permanent Background

My question is: Is it possible to apply a paintable resource to a TextView in a ListView that forces it to retain its background color after the user lifts his finger F TextView?

Consider:

  • User selects an item in a ListView
  • List item highlights
  • User removes his finger from the object.
  • The item is no longer highlighted.

My question is, can I let the object remain selected in step 4? I am currently using a list of states designed for my list items. I tried

android:state_focused="true"

android:state_selected="true"

android:state_check="true"

Thank you in advance:)

Edit

solved. I achieved this through java code. For those who wish, I did the following:

An integer variable "previous" has been declared to preserve the previous index, starting from 0 for the first element, and force it to be selected through setBackground ().

Then, in the onItemClickListener for the list, I simply clear the selection of the previous item, select the current one and set the “previous” to the index of the currently selected item.

I don’t know how I did not think about it before xD. I usually have a good understanding of problem solving.

Happens to all of us at some point, I think: P

+4
source share
1 answer

Although your approach will work, these are additional pieces of code that I would like to avoid. To achieve this, follow these steps:

Make list selection mode in singleChoiceMode.

Make this selector

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

     <item android: drawable = "@ color / color_light_blue_for_selected_item" android: state_checked = "true" />

 </selector>

Set this as the background of the list items.

And it should work.

+1
source

Source: https://habr.com/ru/post/1447197/


All Articles