我有一个ListView的自定义布局,托管在导航抽屉中。这是适配器的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="100dp" >
<ImageView
android:id="@+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:duplicateParentState="true"
android:src="@drawable/nav_drawer_icon_states" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/icon"
android:duplicateParentState="true"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@drawable/nav_drawer_text_states" />
</RelativeLayout>
在我的/res/drawable
文件夹中,我有nav_drawer_text_states.xml
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/blue"/>
<item android:state_enabled="true" android:state_focused="true" android:state_window_focused="true" android:color="@color/white"/>
<item android:color="@color/gray"/>
</selector>
并且已经使用简单的选择器进行了测试:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/blue" />
<item android:state_selected="true" android:color="@color/white" />
<item android:state_checked="true" android:color="@color/white" />
<item android:color="@color/gray" />
</selector>
当我选择ListView:
项时,按下的和默认颜色可以工作,但是中间状态<ListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:paddingTop="?attr/actionBarSize" />
代码:
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
不会将所选文本更改为白色。
感谢任何帮助。
我将nav_drawer_text_states.xml
更改为:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/pink"/>
<item android:state_activated="true" android:color="@android:color/white"/>
<item android:color="@color/second_row_playlists"/>
</selector>
它工作正常,但state_activated =“true”是API11 +,我的应用程序是API8 +。 任何帮助表示赞赏。
答案 0 :(得分:0)
<强>&LT;咆哮&gt; 不幸的是,在Honeycomb之前的版本中Android非常糟糕,市场不想让这些版本死掉。的&LT; / rant&gt;
我通过手工完成适配器中的所有操作来解决。如果有人还在乎,这就是解决方案:
public class NavigationAdapter extends ArrayAdapter<String> {
private int mSelectedPosition;
public NavigationAdapter(Context context, String[] items) {
super(context, R.layout.cell_navigation_drawer, items);
this.mSelectedPosition = 0;
}
public void setItemChecked(int position) {
mSelectedPosition = position;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
...
}
...
Resources res = getContext().getResources();
if (position == mSelectedPosition) {
textView.setTextColor(res.getColor(R.color.white));
} else {
textView.setTextColor(res.getColor(R.color.gray));
}
return convertView;
}
}
并在点击时更改了方法:
public void setItemChecked(int position, boolean value) {
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
mMyAdapter.setItemChecked(mCurrentSelectedPosition);
}