Android 滚动时背景 ListView 变黑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2833057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Background ListView becomes black when scrolling
提问by TiGer
I have created a specific List which exists out of the following elements to create a scrollable list with every row containing a Image on the left side and some text on the right side:
我创建了一个特定的列表,它存在于以下元素中,以创建一个可滚动列表,其中每一行都包含左侧的图像和右侧的一些文本:
To begin with a "root" layout :
从“根”布局开始:
<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>
and then within the ListView I place the following "row" item :
然后在 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>
As long as the screen is shown statically (as in no movement) it will be shown correctly, but when I start scrolling through the list the background of the row-item (an "icon" as can be shown in the code) will be shown correctly but the background of the "root" layout will become completely black... when the scrolling stops the background will, most of the times, get back its color...
As I test I also added a TextView
in that root-element with the same background, this one will detain it's color when the List is scrolled...
Any idea why this is happening, and how to solve this?
只要屏幕静态显示(如没有移动),它就会正确显示,但是当我开始滚动列表时,行项目的背景(可以在代码中显示的“图标”)将是正确显示,但“根”布局的背景将变成完全黑色......当滚动停止时,背景在大多数情况下会恢复其颜色......在我测试时,我还在TextView
该根元素中添加了一个具有相同的背景,当滚动列表时,这个将保留它的颜色......知道为什么会发生这种情况,以及如何解决这个问题?
回答by Praveen
回答by Mahesh
It's very simple just use this line in your layout file :
非常简单,只需在您的布局文件中使用这一行:
android:scrollingCache="false"
like this:
像这样:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollingCache="false"
/>
回答by nikki
you can use like this:
你可以这样使用:
list.setCacheColorHint(Color.TRANSPARENT);
list.requestFocus(0);
回答by AnDx
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"/>
回答by Sampath Kumar
We have plenty of options for this problem, you can set the background as transparent through programming like
对于这个问题我们有很多选择,您可以通过编程将背景设置为透明
yourlistview.setCacheColorHint(Color.TRANSPARENT);
or through xml
或通过 xml
android:cacheColorHint="@android:color/transparent"
回答by jeet parmar
In your xml where to use Listview
set
在您的 xml 中使用Listview
set
android:cacheColorHint="@android:color/transparent"
回答by Ahmad Arslan
I am using images in listView
and it turns black sometimes in samsung s4 not even scrolling. It was a stupid mistake which I have done in the adapter.I just put my view to nullto fix this issue
我正在使用图像,listView
它有时在三星 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);
}
}
回答by Rolf ツ
There are allot of answers to this question but today I realized this question is still missing a critical piece of information.
这个问题有很多答案,但今天我意识到这个问题仍然缺少一个关键信息。
There are two possible solutionsfor the problem, both work but each should be used in different situations.
该问题有两种可能的解决方案,两种解决方案都有效,但应在不同情况下使用每种解决方案。
Methods
方法
Use android:cacheColorHint
when your ListView
has a solidcolor background.
使用android:cacheColorHint
时,你ListView
有一个坚实的彩色背景。
<item name="android:cacheColorHint">@android:color/transparent</item>
Use android:scrollingCache
when your ListView
has a (complex) imageas background.
使用android:scrollingCache
时,你ListView
有一个(复)图像作为背景。
<item name="android:scrollingCache">false</item>
Note
笔记
When your ListView
has a solid color background both methods will work, so not only the cacheColorHint
will work. But it's not recommended to use the scrolingCache
method for solid color backgrounds since it turns off an optimization method used for smooth animating and scrolling the ListView.
当您ListView
有纯色背景时,两种方法都可以使用,因此不仅cacheColorHint
可以使用。但是不建议scrolingCache
对纯色背景使用该方法,因为它关闭了用于平滑动画和滚动 ListView 的优化方法。
Note for the note:scrolingCache
set to false does not necessarily mean the ListView
's animations and scrolling will become slow.
注意事项:scrolingCache
设置为 false 并不一定意味着ListView
的动画和滚动会变慢。
回答by muditagarwal88
android:cacheColorHint="@android:color/transparent"
回答by Lazy Ninja
The following worked for me:
以下对我有用:
myListView.setScrollingCacheEnabled(false);