使ListView项目选择保持稳定

时间:2015-01-29 04:38:59

标签: android listview

如何使ListView项目选择保持稳定?

     <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 个答案:

答案 0 :(得分:2)

您必须在适配器中设置getSelectedIndex()setSelectedIndex()方法。

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();
}

然后

@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;
}

并使用onListItemClick(....)

 adapter.setSelectedIndex(position);

另一种方式@Sun说

使用渐变使列表选择保持稳定

<强> 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>

最后在ListView中,使用listSelector属性,如下所示:

android:listSelector="@drawable/list_selector"

答案 1 :(得分:2)

我使用渐变来使列表选择保持稳定

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>

最后在ListView中,使用listSelector属性,如下所示:

android:listSelector="@drawable/list_selector"

答案 2 :(得分:1)

  

您需要跟踪所选项目并相应地更改列表行的背景。

1.在项目点击中更改所选项目的位置:

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

2.获取所选项目并设置背景:

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;
    }
}

希望它有助于ツ