-1

How to make ListView Item Selection remain stable ?

     <ListView
        android:id="@+id/list_slidermenu"        
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="8"       
        android:layout_gravity="start"
        android:scrollbars="none"
        android:fastScrollEnabled="true"
        android:choiceMode="singleChoice"
        />
3
  • where is your adapter code?
    – M D
    Jan 29, 2015 at 4:41
  • Do as MD told you and make a selector like this Jan 29, 2015 at 6:20
  • u need to handle else part in getView() like this : if (selectedIndex != -1 && selectedIndex == position) { v.setBackgroundResource(R.color.yourColor); }else{ v.setBackgroundResource(R.color.defaultColor); } Jan 29, 2015 at 6:51

3 Answers 3

2

You've to make getSelectedIndex() and setSelectedIndex() method in your adapter.

private int selectedIndex;

public int getSelectedIndex() {
    return selectedIndex;
}

public void setSelectedIndex(int index) {
    this.selectedIndex = index;

    // Re-draw the list by informing the view of the changes
    notifyDataSetChanged();
}

and then

@Override
public View getView(int position, View convertView, ViewGroup parent) {

     .......

    // Highlight the selected item in the list
    if (selectedIndex != -1 && selectedIndex == position) {
        v.setBackgroundResource(R.color.yourColor);
    }

    return v;
}

and used from onListItemClick(....)

 adapter.setSelectedIndex(position);

Another way as @Sun said

Used gradient to make list selection remain stable

gradient_bg.xml:-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
  android:startColor="#7ecce8"
  android:centerColor="#7ecce8"
  android:endColor="#7ecce8"
  />
</shape>

gradient_bg_hover.xml:-

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
  android:startColor="#7ecce8"
  android:centerColor="#7ecce8"
  android:endColor="#7ecce8"
  />
</shape>

list_selector.xml:-

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

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

<item android:state_selected="true"
 android:state_pressed="false"
    android:drawable="@drawable/gradient_bg_hover" />
 </selector>

finally in ListView, use listSelector property like below:

android:listSelector="@drawable/list_selector"
4
  • now selection remains stable but whenever i do tap on any other list item, its not refreshing list still getting old selected rows as well
    – Sun
    Jan 29, 2015 at 5:37
  • @Sun add adapter.setNotifyOnChange(true); in your Activity
    – M D
    Jan 29, 2015 at 5:41
  • @Sun Thnx. and your answer also useful to others that's why +1 up.
    – M D
    Jan 29, 2015 at 7:01
  • Let us continue this discussion in chat.
    – Sun
    Jan 29, 2015 at 7:05
2

I have used gradient to make list selection remain stable

gradient_bg.xml:-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#7ecce8"
      android:centerColor="#7ecce8"
      android:endColor="#7ecce8"
      />
</shape>

gradient_bg_hover.xml:-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#7ecce8"
      android:centerColor="#7ecce8"
      android:endColor="#7ecce8"
      />
</shape>

list_selector.xml:-

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

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

    <item android:state_selected="true"
     android:state_pressed="false"
        android:drawable="@drawable/gradient_bg_hover" />
</selector>

finally in ListView, use listSelector property like below:

android:listSelector="@drawable/list_selector"
3
  • 1
    @SweetWisherヅ i followed your link as well but that was not helping me in making selection stable, but yes that gave me an idea and that's why i ticked your answer as useful.. but as you know MD was the first who started helping me in this and i accepted his answer... anyways thank you so much
    – Sun
    Jan 29, 2015 at 7:14
  • 1
    Lolz... I din't ask you to accept mine.. happy coding Jan 29, 2015 at 7:31
  • 1
    @SweetWisherヅ great person .. good thoughts -- deserves salute :)
    – Sun
    Jan 29, 2015 at 7:32
1

You need to keep track of selected item and accordingly change the background of the list row.

1.Change the selected item's position in item click :

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
        adapter.setSelectedItem(position);
}

2.Get selected item and set background :

public class AudioAdapter extends ArrayAdapter<Audio> {
    Integer selected_position = -1;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Your Code

        if (position == selected_position) {
            // set selected color
            v.setBackgroundResource(R.color.yourSelectedColor);
        } else {
            // set default color
            v.setBackgroundResource(R.color.yourDefaultColor);
        }
    }

    public int getSelectedItem() {
        return selected_position;
    }

    public void setSelectedItem(int index) {
        this.selected_position = index;
    }
}

Hope it helps ツ

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.