【问题标题】:Getting Black Screen while Scrolling in ListView in Android在Android的ListView中滚动时出现黑屏
【发布时间】:2012-01-10 20:29:21
【问题描述】:

我有一个列表视图,我在其中显示我得到的数据,它在模拟器中运行良好并且也可以滚动。但是当我在滚动时在设备上测试它时,它显示黑屏,滚动停止后它处于正常可读形式。我不明白为什么会这样。我还附上了示例代码。

第一个 XML 布局:-

<?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="fill_parent"
    android:orientation="vertical"
    android:padding="7dp" >

<TextView
    android:id="@+id/item_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:textColor="#000000"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="20dp" />

<TextView
    android:id="@+id/item_subtitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:padding="2dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="13dp" />

第二个 ListView 布局:-它也有滚动视图。

<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="#FFFFFF">   

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="No data"
    android:textColor="#000000" />

我使用此布局的代码:-

ListAdapter adapter = new SimpleAdapter(this, HomeMenuActivity.mylist , R.layout.listdata, 
            new String[] { "Name", "Vicinity" }, 
            new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    lv = getListView();
    lv.setTextFilterEnabled(true);

我有包含数据的静态数组列表“mylist”。如果有人知道为什么它在滚动时显示黑屏,请指导我。

谢谢 lv.setOnItemClickListener(this);

【问题讨论】:

    标签: android listview scrollbar


    【解决方案1】:

    您必须在列表视图中进行设置:

    android:cacheColorHint="#00000000"
    

    目标是禁用框架为提高滚动操作期间的绘图性能所做的优化。

    【讨论】:

      【解决方案2】:

      只需在列表视图中添加颜色提示属性即可。

                  <ListView android:layout_height="wrap_content"
                      android:layout_width="match_parent" android:cacheColorHint="@android:color/transparent" />
      

      【讨论】:

        【解决方案3】:

        在布局的列表视图标签中添加一个属性

        android:cacheColorHint="#00000000"
        

        之后它应该可以工作了。

        【讨论】:

          【解决方案4】:

          我完全同意上面列出的所有答案,但让我告诉你这个信息:

          ListView 默认具有透明/半透明背景,Android UI 工具包中的所有默认小部件也是如此。这意味着当 ListView 重绘其子项时,它必须将子项与窗口的背景混合。再一次,这需要从内存中进行代价高昂的回读,这在每秒进行数十次绘制时在滚动或投掷期间尤其痛苦。

          为了提高滚动操作期间的绘图性能,Android 框架会重用缓存颜色提示。设置此提示后,框架将列表的每个子项复制到填充了提示值的位图中(假设未关闭另一个优化,称为滚动缓存)。 ListView 然后直接在屏幕上对这些位图进行 blits,因为这些位图已知是不透明的,所以不需要混合。此外,由于默认的缓存颜色提示是 #191919,因此在滚动期间您会在每个项目后面看到深色背景。

          要解决此问题,您只需禁用缓存颜色提示优化(如果您使用非纯色背景),或者将提示设置为适当的纯色值。您可以使用 android:cacheColorHint 属性从代码(参见 setCacheColorHint(int))或最好从 XML 中执行此操作。要禁用优化,只需使用透明色#00000000。以下屏幕截图显示了在 XML 布局文件中设置了 android:cacheColorHint="#00000000" 的列表

          欲了解更多信息:Android ListView Optimization

          解决方案:

          您可以使用 android:cacheColorHint="@android:color/transparent"android:cacheColorHint="#00000000"

          【讨论】:

          • 非常感谢 paresh 提供详细信息。再次感谢您的帮助...
          • 优秀的答案!非常有用和详细;D
          • 优秀,技术丰富的答案。对开发人员、学生、研究人员等非常有用
          【解决方案5】:

          尝试此代码并根据您的要求设置颜色:

          String arr[]=new String[]{"a","bv"};
          ListView lv;    
          
          protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              lv=(ListView)findViewById(R.id.lv1);
              ArrayAdapterad=newArrayAdapter(getApplicationContext(),R.layout.check_it,R.id.txt_color,ar);
              lv.setAdapter(ad);
          }
          
          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
          <ListView
              android:layout_width="match_parent"
              android:layout_height="250dp"
              android:id="@+id/lv1"
              android:background="#000"
              android:scrollingCache="false"/>
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#fff"
              android:id="@+id/txt_color"/>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-04-26
            • 1970-01-01
            • 1970-01-01
            • 2015-11-17
            • 1970-01-01
            相关资源
            最近更新 更多