Android listview所选背景不';我不能在2.3中工作
我正在尝试让我的应用程序在2.3中正常工作(在4.0+中工作正常),我遇到的一个问题是,在我的listview中,我无法更改所选项目的背景。我不确定我需要改变什么-有人知道吗 以下是listview本身:Android listview所选背景不';我不能在2.3中工作,android,listview,background,Android,Listview,Background,我正在尝试让我的应用程序在2.3中正常工作(在4.0+中工作正常),我遇到的一个问题是,在我的listview中,我无法更改所选项目的背景。我不确定我需要改变什么-有人知道吗 以下是listview本身: <ListView android:id="@+id/score_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_abo
<ListView
android:id="@+id/score_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/keyboard"
android:layout_below="@+id/sort_header"
android:choiceMode="singleChoice"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:listSelector="@drawable/selector" />
以下是在4.0+中工作的选择器(在2.3中没有颜色变化):
实际上,我并不需要所有的4个,但我想尝试所有的东西。
android:state\u activated
在API级别11中引入
请参阅此链接
更新
我正在我的应用程序中使用它(API级别8)
我也有同样的问题。我还没有找到一种在XML中实现这一点的方法,但我在代码中找到了一种解决方法。以下代码在支持API级别7的应用程序中进行了测试+ 首先,您需要稍微更改
列表视图的适配器:
public class ListViewAdapter extends BaseAdapter {
private int selectedItemPosition = -1;
// your code
public void selectItem(int i) {
selectedItemPosition = i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
// your code
if (i == selectedItemPosition) {
// set the desired background color
textView.setBackgroundColor(context.getResources().getColor(R.color.highlight));
}
else {
// set the default (not selected) background color to a transparent color (or any other)
textView.setBackgroundColor(Color.TRANSPARENT);
}
return view;
}
}
接下来,您必须通知适配器,OnItemClickListener
的onItemClickMethod
中的选择已更改:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// select the item
((ListViewAdapter) listview.getAdapter()).selectItem(position);
// force redraw of the listview (invalidating just "view" is not enough)
((ListView) parent).invalidateViews();
// your code
}
@覆盖
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//选择项目
((ListViewAdapter)listview.getAdapter())。选择项(位置);
//强制重新绘制listview(仅使“视图”无效是不够的)
((ListView)parent.invalidateViews();
//你的代码
}
应该是这样。现在,只要您不想更改所选项目,就可以使用onItemClick()
中使用的相同代码,即selectItem()
后跟invalidateViews()
。除了调用invalidateViews()
,还可以使用适配器的notifyDataSetChanged()
此外,还应向列表视图添加适当的listSelector,以避免单击项目时默认选择器短暂闪烁。然而,当整个视图的背景发生变化时,API 7和8上的列表选择器存在一个bug。您可以找到一种解决方法尝试在listviewandroid:cacheColorHint中设置属性=“@null
。触摸listview背景不会很亮。正如AwdKab响应中所述,API级别11中引入了android:state\u activated
。解决方案是为列表项布局的顶部视图
实现可检查的
界面,请参见my.那么我可以在API级别9上做什么?该列表中的任何内容都无法突出显示所选项目。当我单击时,这种情况会很快发生:1。整个列表将突出显示2。项目I单击取消高亮显示3。整个列表将取消高亮显示,因此不会高亮显示我单击的列表。如果我按住,整个列表会一直突出显示,直到我发布。我的体验与GrilledCheese完全一样
public class ListViewAdapter extends BaseAdapter {
private int selectedItemPosition = -1;
// your code
public void selectItem(int i) {
selectedItemPosition = i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
// your code
if (i == selectedItemPosition) {
// set the desired background color
textView.setBackgroundColor(context.getResources().getColor(R.color.highlight));
}
else {
// set the default (not selected) background color to a transparent color (or any other)
textView.setBackgroundColor(Color.TRANSPARENT);
}
return view;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// select the item
((ListViewAdapter) listview.getAdapter()).selectItem(position);
// force redraw of the listview (invalidating just "view" is not enough)
((ListView) parent).invalidateViews();
// your code
}