【问题标题】:Background ListView becomes black when scrolling滚动时背景ListView变黑
【发布时间】:2011-02-19 10:56:39
【问题描述】:

我创建了一个特定的列表,它存在于以下元素中,以创建一个可滚动的列表,其中每一行都包含左侧的图像和右侧的一些文本:

从“根”布局开始:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="#C8C8C8"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        android:divider="#C8C8C8"
        android:background="#C8C8C8"/>
</LinearLayout>

然后在 ListView 中放置以下“行”项:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/bg_row"
>
    <ImageView
        android:layout_width="wrap_content"
        android:paddingLeft="10px"
        android:paddingRight="15px"
        android:paddingTop="5px"
        android:paddingBottom="5px"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_image"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5px"
        android:paddingBottom="5px"
        android:textSize="16sp"
        android:textColor="#000000"
        android:layout_gravity="center"
        android:maxHeight="50px"/>
</LinearLayout>

只要屏幕静态显示(如没有移动),它就会正确显示,但是当我开始滚动列表时,行项的背景(代码中显示的“图标” ) 将正确显示,但“根”布局的背景将变为完全黑色......当滚动停止时,大多数情况下,背景将恢复其颜色...... 当我测试时,我还在那个具有相同背景的根元素中添加了一个TextView,当列表滚动时,这个会保留它的颜色...... 知道为什么会发生这种情况,以及如何解决这个问题吗?

【问题讨论】:

    标签: android listview


    【解决方案1】:

    ListView 标签上添加一个属性

    android:cacheColorHint="#00000000" // setting transparent color
    

    更多详情请查看this blog

    【讨论】:

    • this.getListView().setCacheColorHint(0);从布局我无法设置颜色。但我设法用上面的代码做到了。我在列表活动的“onCreate”中使用了此代码。希望这些信息也有帮助。
    • android:cacheColorHint="#0000" 如果您害怕零,也可以使用。
    • 如果你害怕数字,也可以android:cacheColorHint="@android:color/transparent" :)
    • @android:color/transparent 可能有一个额外的好处,即设置了一个位。有一个错误,如果您在某些手机上传递 0,透明度将不起作用。所以诀窍是设置 1 位。
    【解决方案2】:

    非常简单,只需在布局文件中使用这一行即可:

    android:scrollingCache="false"
    

    像这样:

    <ListView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollingCache="false"
    />
    

    【讨论】:

    【解决方案3】:

    你可以这样使用:

    list.setCacheColorHint(Color.TRANSPARENT);
    list.requestFocus(0);
    

    【讨论】:

      【解决方案4】:

      android:id="@android:id/list"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"
      android:drawSelectorOnTop="false"
      android:divider="#C8C8C8"
      android:background="#C8C8C8"
      android:cacheColorHint="#00000000"/>
      

      【讨论】:

        【解决方案5】:

        对于这个问题我们有很多选择,你可以像这样通过编程将背景设置为透明

        yourlistview.setCacheColorHint(Color.TRANSPARENT); 
        

        或通过xml

        android:cacheColorHint="@android:color/transparent"
        

        【讨论】:

          【解决方案6】:

          在你的xml中使用Listview设置

              android:cacheColorHint="@android:color/transparent"
          

          【讨论】:

            【解决方案7】:

            我在 listView 中使用图像,有时在三星 s4 中它会变黑,甚至无法滚动。这是我在适配器中犯的一个愚蠢的错误。我只是将我的 view 设置为 null 来解决这个问题

             @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        Holder holder;
                        convertView = null; // convert view should be null
                        if (convertView == null) {
                            holder = new Holder();
                            convertView = inflater1.inflate(R.layout.listview_draftreport_item, null);
                         } 
                    }
            

            【讨论】:

              【解决方案8】:

              这个问题有很多答案,但今天我意识到这个问题仍然缺少关键信息。

              对于这个问题有两种可能的解决方案,它们都有效,但每种都应该在不同的情况下使用。


              方法

              当您的ListView 具有纯色 颜色背景时,请使用android:cacheColorHint

              <item name="android:cacheColorHint">@android:color/transparent</item>
              

              当您的ListView 有一个(复杂的)图像作为背景时,使用android:scrollingCache

              <item name="android:scrollingCache">false</item>
              

              注意

              当您的ListView 具有纯色背景时,两种方法都可以使用,因此不仅cacheColorHint 可以使用。但不建议将scrolingCache 方法用于纯色背景,因为它会关闭用于平滑动画和滚动 ListView 的优化方法。

              注意事项:scrolingCache设置为false并不一定意味着ListView的动画和滚动会变慢。

              【讨论】:

                【解决方案9】:
                android:cacheColorHint="@android:color/transparent"
                

                【讨论】:

                  【解决方案10】:

                  以下内容对我有用:

                  myListView.setScrollingCacheEnabled(false);
                  

                  【讨论】:

                    【解决方案11】:

                    android:cacheColorHint="#00000000"// setting transparent color

                    不要设置listview的背景。

                    【讨论】:

                      【解决方案12】:

                      添加属性

                      android:cacheColorHint="#00000000" //transparent color

                      【讨论】:

                        猜你喜欢
                        • 2012-07-03
                        • 1970-01-01
                        • 1970-01-01
                        • 2011-02-02
                        • 1970-01-01
                        • 1970-01-01
                        • 2017-05-22
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多