[英]Background ListView becomes black when scrolling
我创建了一个特定的列表,它存在于以下元素中,以创建一个可滚动列表,其中每一行都包含左侧的图像和右侧的一些文本:
从“根”布局开始:
<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:scrollingCache="false"
像这样:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollingCache="false"
/>
你可以这样使用:
list.setCacheColorHint(Color.TRANSPARENT);
list.requestFocus(0);
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"/>
对于这个问题,我们有很多选择,您可以通过编程将背景设置为透明,例如
yourlistview.setCacheColorHint(Color.TRANSPARENT);
或通过 xml
android:cacheColorHint="@android:color/transparent"
在您的 xml 中在哪里使用Listview
集
android:cacheColorHint="@android:color/transparent"
我在listView
使用图像,它有时在 samsung s4 中变黑,甚至不滚动。 这是我在适配器中犯的一个愚蠢的错误。我只是将我的视图设置为 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);
}
}
这个问题有很多答案,但今天我意识到这个问题仍然缺少一个关键信息。
该问题有两种可能的解决方案,两种解决方案都有效,但应在不同情况下使用每种解决方案。
方法
当您的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
的动画和滚动会变慢。
android:cacheColorHint="@android:color/transparent"
以下对我有用:
myListView.setScrollingCacheEnabled(false);
android:cacheColorHint="#00000000"// setting transparent color
或者
不要设置listview
背景。
添加属性
android:cacheColorHint="#00000000" //transparent color
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.