使 ListView 项目选择保持稳定
Make ListView Item Selection remain Stable
如何让ListView的Item Selection保持稳定?
<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"
/>
您必须在适配器中创建 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"
You need to keep track of selected item and accordingly change the background of the list row.
1.Change 所选项目在项目点击中的位置:
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
adapter.setSelectedItem(position);
}
2.Get 已选择项目并设置背景:
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;
}
}
希望对你有帮助ツ
我已经使用渐变来使列表选择保持稳定
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"
如何让ListView的Item Selection保持稳定?
<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"
/>
您必须在适配器中创建 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"
You need to keep track of selected item and accordingly change the background of the list row.
1.Change 所选项目在项目点击中的位置:
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
adapter.setSelectedItem(position);
}
2.Get 已选择项目并设置背景:
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;
}
}
希望对你有帮助ツ
我已经使用渐变来使列表选择保持稳定
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"