Android列表视图选择的背景在2.3中不起作用

Gri*_*ese 5 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_above="@+id/keyboard"
    android:layout_below="@+id/sort_header"
    android:choiceMode="singleChoice"
    android:divider="#CCCCCC"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/selector" />
Run Code Online (Sandbox Code Playgroud)

这是在4.0+中工作的选择器(在2.3中没有颜色变化):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/highlight"/>
    <item android:state_pressed="true" android:drawable="@color/highlight"/>
    <item android:state_activated="true" android:drawable="@color/highlight"/>
    <item android:state_selected="true" android:drawable="@color/highlight"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

我实际上并不需要所有4个,但我想尝试一切.

sta*_*tiv 7

我有完全相同的问题.我还没有找到如何在XML中执行此操作的方法,但我在代码中找到了一种解决方法.以下代码在支持API级别7的应用程序中进行了测试

首先,您需要更改适配器ListView:

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

接下来,您必须通知选择在你改变了适配器OnItemClickListeneronItemClickMethod:

@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
}
Run Code Online (Sandbox Code Playgroud)

那应该是它.现在,无论何时您不想更改所选项目,您都可以使用相同的代码onItemClick(),即.selectItem()接着是invalidateViews().也可以使用invalidateViews()适配器,而不是调用notifyDataSetChanged().

此外,您还应该在列表视图中添加适当的listSelector,以避免在单击项目时短暂闪烁默认选择器.但是,当整个视图的背景发生变化时,API 7和8上的列表选择器存在错误.你可以在这里找到一个解决方法


Awa*_*Kab 0

android:state_activated在 API 级别 11 中引入。

请参阅此链接可绘制资源

更新

我在我的应用程序中使用它(API 级别 8)

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:state_focused="false"
    android:drawable="@color/normal"/> 

<item android:state_pressed="true"
        android:drawable="@color/highlight"/>

<item android:state_selected="true"
     android:state_pressed="false"
     android:drawable="@color/highlight"/>

<item android:state_focused="true"
     android:state_pressed="false"
     android:drawable="@color/highlight"/>
</selector>
Run Code Online (Sandbox Code Playgroud)