在 Android 上的 ListView 中突出显示所选项目 [英] Highlight selected item in ListView on Android
问题描述
我一直在制作一个与 Android 中的 ListViews 一起使用的应用程序,但我无法制作它以使所选(修改)的项目具有不同的背景.我正在使用 CHOICE_MODE_SINGLE.这是我的代码到目前为止的样子:
I have been making an application that works with ListViews in Android, and I can't make it so that the selected (chacked) item has a different background. I'm using CHOICE_MODE_SINGLE. This is how my code looks so far:
我使用的ListView:
(在 layout.xml 内)
The ListView that I use:
(inside 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>
<小时>
我在适配器中使用的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" />
<小时>
这是我添加适配器的地方:
This is where I add the adapter:
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);
}
});
我确定点击时检查了正确的项目,因为当我调用 getCheckedItemPosition() 时,它返回正确的值.
I'm sure that the proper item is checked on click, because when I call getCheckedItemPosition(), it returns the proper value.
现在,为了突出显示选中的项目,我尝试了两件事:
选择器可绘制:
(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" />
</shape></item>
<item android:state_selected="true"><shape>
<solid android:color="@color/blue" />
</shape></item>
<item android:state_pressed="true"><shape>
<solid android:color="@color/green" />
</shape></item>
</selector>
我将它添加到 .xml 中:
I add it to .xml with:
android:listSelector="@drawable/selector_test"
<小时>
背景可绘制:
(background_test.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/red" android:state_checked="true"/>
<item android:drawable="@color/blue" android:state_selected="true"/>
<item android:drawable="@color/green"/>
</selector>
我将它添加到 .xml 中:
I add it to .xml with:
android:background="@drawable/background_test"
<小时>
我已经尝试将选择器和背景添加到 listView.xml 和 listItem.xml,但唯一改变的是默认背景颜色,以及按下(或按住)项目时选择器的颜色.
android:state_checked="true" 和 android:state_selected="true" 似乎什么都不做.
如果选择了视图,我可以通过覆盖 ArrayAdapter 中的 getView() 方法并在其中调用 setBackgroundColor() 来更改背景,它确实更改了背景,但也完全摆脱了选择器.另外,我真的不喜欢仅仅为了改变一行代码而覆盖类,特别是如果可以用不同的方式实现同样的事情.
所以我要问的是,有没有办法通过向 ListView 添加选择器或可绘制背景来突出显示 ListView 中的选中项,而我只是做错了,或者我是否必须以其他方式使其工作.
提前致谢!:)
推荐答案
在活动的开始处添加这一行
add this line in onStart of your activity
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
其中 lv 是 listView 的实例
where lv is instance of listView
然后覆盖此方法并向其添加以下行.
then override this method and add the following lines to it.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Set the item as checked to be highlighted
lv.setItemChecked(position, true);
v.setBackgroundColor(Color.BLUE);
conversationAdapter.notifyDataSetChanged();
}
然后在自定义适配器的 getView 方法中将上一个选定项目的背景颜色改回正常
and then change the color of previous selected item's background back to normal in getView method of your custom adapter
这篇关于在 Android 上的 ListView 中突出显示所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!