1

I have a ListView with 10 Items. So when I click on the first Item and scroll to the end, the highlight of the first Item removed.

Some code:

my ListView

<ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/listView"
        >
</ListView>

simplerow

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000000"
android:focusable="false"
android:background="@drawable/selecter"
>

selecter

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_selected="true"
    android:drawable="@drawable/gray">
</item>

<item
    android:state_pressed="true"
    android:drawable="@drawable/blue" >
    </item>
</selector>

Activity

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    view.setSelected(true);
    ...
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
MuratAbi
  • 11
  • 1
  • 1
    Show more code. The code you showed is fine. The bug must originate from somewhere else. – barq Feb 16 '15 at 22:26
  • possible duplicate of [Android - Keep ListView's item highlighted once one has been clicked](http://stackoverflow.com/questions/9281000/android-keep-listviews-item-highlighted-once-one-has-been-clicked) – Jared Burrows Feb 16 '15 at 22:26
  • You should wrap up with some global list of booleans,say 'isSelected', of same size as that of listview's number of rows, and set corresponding index true if that row is selected. Now, in getView, if(isSelected(position)), then only view.setSelected(true), else view.setSelected(false). – Abhinav Puri Feb 16 '15 at 22:31
  • When you scroll , your views are rendered again . you must keep selected state in somewhere relative to position or id and in getView method of your list adapter , you must set selected true or false acccording to position or id – magirtopcu Feb 16 '15 at 22:32

0 Answers0