2013-04-03 104 views
5

我想让我的应用程序在2.3中正常工作(它在4.0+中工作正常),我遇到的一个问题是在我的列表视图中我无法获得所选项目的背景将更改。我不知道我需要改变 - 有人知道吗?Android的listview选定的背景不起作用在2.3

这里的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" /> 

下面是在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> 

我其实并不需要的所有4那些,但我想尝试一切。

回答

0

android:state_activated介绍了在API级别11

请参阅此链接Drawable Resources

更新

我在我的应用程序使用(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> 
+0

那么我可以在API级别9上做什么?该列表中没有任何内容用于突出显示所选项目。 – GrilledCheese 2013-04-03 22:29:33

+0

我更新了我的答案。 – AwadKab 2013-04-03 23:20:38

+0

当我点击,出现这种情况真的很快: 1.整个列表被高亮显示 2.项目我点击unhighlights 3.整个列表unhighlights 所以也没有突出的一个,我点击。如果我按住,整个列表将保持突出显示,直到我释放。 – GrilledCheese 2013-04-03 23:31:39

7

我有完全相同的问题。我还没有找到如何在XML中做到这一点的方法,但我已经找到了解决方法。下面的代码是在应用程序,支持7+

API层面第一次测试,您需要更改适配器为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; 
    } 
} 

接下来,您必须通知选择在改变适配器您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 
} 

这应该是它。现在,只要您不想更改所选项目,就可以使用与onItemClick()中使用的代码相同的代码。 selectItem(),然后是invalidateViews()。也可以使用适配器的notifyDataSetChanged(),而不是调用invalidateViews()

此外,您还应该添加一个适当的listSelector到列表视图,以避免点击该项目时默认选择器的短暂闪烁。然而,当整个视图的背景被改变时,API 7和8上的列表选择器存在一个错误。您可以找到解决方法here

1

尝试在列表视图android:cacheColorHint="@null中设置属性。列表视图背景不会被触摸。

+0

完全一样,对于我的问题是一个很好的解决方案。谢谢。 – j2emanue 2015-12-04 15:43:54

0

由于是写在AwdKab响应,android:state_activated API等级11的解决方案是引入实现Checkable接口列表的顶部View项目布局,看到我my post here

相关问题