我一直在制作一个与Android中的ListViews一起使用的应用程序,我不能这样做,以便所选(chacked)项具有不同的背景.我正在使用CHOICE_MODE_SINGLE.这是我的代码到目前为止的样子:
我使用的ListView :(
在layout.xml内)
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:listSelector="@drawable/selector_test" >
</ListView>
Run Code Online (Sandbox Code Playgroud)
我在适配器中使用的TextView布局:
(listItem.xml)
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textStyle="bold" />
Run Code Online (Sandbox Code Playgroud)
这是我添加适配器的地方:
mListAdapter = new ArrayAdapter<String>(this, R.layout.listItem, mList);
mListView = (ListView) findViewById(R.id.listView);
mListView.setAdapter(mAuthorAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
String selected = mList.get(position);
// ...
mListView.setItemChecked(position, true);
}
});
Run Code Online (Sandbox Code Playgroud)
我确定在点击时检查了正确的项目,因为当我调用getCheckedItemPosition()时,它会返回正确的值.
现在,我试图突出检查项目的两件事:
选择器drawable:
(selector_test.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="@android:integer/config_longAnimTime">
<item android:state_checked="true"><shape>
<solid android:color="@color/red" /> …Run Code Online (Sandbox Code Playgroud)