2

我的 Android 应用程序中有一个两种颜色ListView的 s(一种颜色用于奇数元素,一种用于偶数元素)。它在 Froyo 上运行良好。但是在 Jelly Bean 模拟器上,滚动过程中会出现一些伪影。某些元素的背景变为黑色。是的,我知道带有透明缓存颜色提示的解决方案!但只有当我以这种方式设置背景时它才有效:

bindView()适配器方法中:

// ...
view.setBackgroundResource(
        cursor.getPosition() % 2 == 0 ? R.color.list_item_bg1: R.color.list_item_bg2);

但是这种方法不适合我,因为我想突出显示元素,然后用户点击它。所以我StateListDrawable为此目的使用:

mOddColorDrawable = new ColorDrawable(
        context.getResources().getColor(R.color.list_item_bg2));
mEvenColorDrawable = new ColorDrawable(
        context.getResources().getColor(R.color.list_item_bg1));
mSelector = new ColorDrawable(
    context.getResources().getColor(R.color.list_item_selector));

public void bindView(View view, Context context, Cursor cursor) {
    // ...
    setBackground(cursor.getPosition % 2 != 0, view);
}

public void setBackground(boolean isOdd, View listItem) {
    StateListDrawable dr = new StateListDrawable();
    Drawable drColor = isOdd ? mOddColorDrawable : mEvenColorDrawable;
    dr.addState(new int[] { android.R.attr.state_pressed }, mSelectorDrawable);
    dr.addState(new int[] { -android.R.attr.state_pressed }, drColor);
    listItem.setBackgroundDrawable(bg);
}

因此,即使我为ListView. 我花了几天时间调查这个问题,但无法克服它。所以,你是我最后的希望,StackOverflow!

概括:

  1. 我想让 ListView 有两种不同的元素颜色。
  2. 我想要具有自定义颜色的项目选择器。
  3. 如果我使用 StateListDrawable,setCacheColorHint(transparent) 没有帮助。
  4. 在 Froyo 上一切正常,但在 Jelly Bean 上却不行。
4

7 回答 7

5

为了使列表项的备用背景颜色。在你的适配器类中写下这行代码

public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.listitem, null);

            if (position % 2 == 0) {
                v.setBackgroundResource(R.color.firstcolor);
            } else {
                v.setBackgroundResource(R.color.second color);
            } 
         ...............
         .........

您好,请通过我的 Android 博客获得完整的帮助,希望您能找到您正在寻找的答案。刚才我已经从我身边完成并进行了测试,它对我来说工作正常。在底部,您将获得一个链接,以便您可以下载完整的源代码。

http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html

如果它完全满足您的要求,请告诉我。

于 2013-03-06T09:14:41.347 回答
2

在列表视图中设置 cacheColorHint="#00000000"。

有关更多信息.. 阅读这篇文章Getting Black Screen while Scrolling in ListView in Android

于 2013-03-06T05:51:17.360 回答
1

如果您使用的是动态列表视图,那么为了避免黑屏

ListView listview=new ListView();
listview.setCacheColorHint(0);

如果您想在单击任何显示为黄色的项目时制作透明的列表项,还有一件事。为了避免这种情况

listview.setSelector(new ColorDrawable(0x0));

并且用于布局//用于在选择或单击列表项时避免黄色

android:listSelector="#00000000"

谢谢,

于 2013-03-06T06:05:48.003 回答
0

setSelector(new ColorDrawable(0x0)); Is used to avoid yellow color while clicking listitem. If you want some hover effect to ListItem so use selctor in listiem.

listiemselector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@color/white" />
<item android:state_pressed="true" 
    android:drawable="@color/selected" />
<item android:state_selected="true"
 android:state_pressed="false" 
    android:drawable="@color/selected" />
</selector>

and use it in your listitem parent Layout.

android:background="@drawable/listiemselector"
于 2013-03-06T09:02:41.957 回答
0

从列表中删除项目时,我遇到了相同的列表变黑问题。今天我通过删除listView的“android:background”属性解决了这个问题。它工作正常。

于 2013-06-13T04:29:59.157 回答
0

您好,请查看本教程以获取更多信息。

在 ListView 标签上添加一个属性

 android:cacheColorHint="#00000000"// setting as a transparent color
于 2013-03-06T05:58:59.343 回答
0

请使用 mListView.setFadingEdgeLength(0); 其中 mListView 是 ListView 对象引用。它工作完美。

于 2013-12-04T09:32:04.543 回答