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

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

Patricia Arquette
Release: 2024-11-07 06:02:02
Original
381 people have browsed it

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>
Copy after login

Solution

To ensure the selected item in cli_lista remains highlighted:

  1. Set the Choice Mode programmatically:

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

    android:listSelector="#666666"
    Copy after login

    or programmatically:

    listView.setSelector(getResources().getDrawable(R.drawable.selector_background));
    Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template