Home  >  Article  >  Java  >  How to Maintain Highlight Persistence for Selected Items in Android ListView?

How to Maintain Highlight Persistence for Selected Items in Android ListView?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-07 06:02:02294browse

How to Maintain Highlight Persistence for Selected Items in Android ListView?

Android ListView Selected Item Highlight Persistence

Issue

In an Android application with two ListViews, the goal is to keep the selected item in one ListView highlighted while displaying the details of the selected item in the other ListView.

XML Structure

<ListView
    android:id="@+id/cli_lista"
    android:choiceMode="singleChoice"
    android:listSelector="#666666"
    ...>
</ListView>

<ListView
    android:id="@+id/cli_lista_det"
    ...>
</ListView>

Solution

To ensure the selected item in cli_lista remains highlighted:

  1. Set the Choice Mode programmatically:

    listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
  2. Specify the background color for selected items:

    android:listSelector="#666666"

    or programmatically:

    listView.setSelector(getResources().getDrawable(R.drawable.selector_background));

The above is the detailed content of How to Maintain Highlight Persistence for Selected Items in Android ListView?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn