Android 如何清除listview上的项目选择
我已将选择颜色设置为listview项Android 如何清除listview上的项目选择,android,listview,android-listview,Android,Listview,Android Listview,我已将选择颜色设置为listview项 <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:textColor="@drawable/te
<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:textColor="@drawable/text_selector"
android:textSize="16sp" >
有没有办法清除项目选择
编辑
在检查了不同的平台后,该解决方案仍然无法在一些使用安卓4.4.4(例如:Nexus 10)的设备上运行,但它可以在安卓4.3设备上正常运行
谢谢。试试这个:
v.clearChoices();
v.requestLayout();
编辑:
根据此链接,您还可以尝试:
v.clearChoices();
for (int i = 0; i < v.getCount(); i++)
v.setItemChecked(i, false);
v.post(new Runnable() {
@Override
public void run() {
v.setChoiceMode(ListView.CHOICE_MODE_NONE);
}
});
v.clearChoices();
对于(int i=0;i
因为上述解决方案在某些设备上仍然不起作用,所以我找到了解决此问题的解决方案
private View mSelectedView; // the view has been selected
// set text color when the list view get focus or not
mainListView.setOnFocusChangeListener(new OnFocusChangeListener() {
if(hasFocus) {
if(mSelectedView != null && mSelectedView instanceof TextView)
((TextView)mSelectedView).setTextColor(getResources().getColorStateList(R.drawable.text_selector));
} else {
mSelectedView = mainListView.getSelectedView();
if(mSelectedView != null && mSelectedView instanceof TextView)
((TextView)mSelectedView).setTextColor(getResources().getColor(R.color.default_color));
}
});
但当listview失去焦点时,最后一项的文本颜色将保持在选定状态
请从选择器可绘制xml中删除所选状态,并为其声明一些choiceMode。
我的ListView声明:
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:listSelector="@drawable/main_list_selector"
...
可能重复的
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:listSelector="@drawable/main_list_selector"
...
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/button_focused"/>
<item android:state_pressed="true" android:drawable="@drawable/button_checked_background"/>
<item android:state_activated="true" android:drawable="@drawable/button_checked_background"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<com.example.yourapp.ui.checkable.CheckableLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckedTextView
android:id="@+id/item_text_view"
...
android:background="@drawable/row_item background"/>
</com.example.yourapp.ui.checkable.CheckableLinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/button_checked_background"/>
<item android:drawable="@android:color/transparent"/>
</selector>
public class CheckableLinearLayout extends LinearLayout implements Checkable {
private boolean checked = false;
public CheckableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CheckableLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isChecked() {
return this.checked;
}
@Override
public void setChecked(boolean checked) {
this.checked = checked;
this.setSelected(this.checked);
final int childsCount = getChildCount();
for (int i = 0; i < childsCount; ++i) {
View child = getChildAt(i);
if (child instanceof Checkable) {
((Checkable) child).setChecked(checked);
}
}
}
@Override
public void toggle() {
setChecked(!this.checked);
}
}