本文共 2278 字,大约阅读时间需要 7 分钟。
我创建了一个特定的List,它存在于以下元素之外,用于创建一个可滚动列表,每行包含左侧的Image和右侧的一些文本:
首先是“根”布局:
< android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" android:divider="#C8C8C8" android:background="#C8C8C8"/>
然后在ListView中我放置以下“行”项:
只要屏幕静态显示(没有移动)就会正确显示,但是当我开始滚动列表时,行项目的背景(代码中显示的“图标”)将是正确显示,但“根”布局的背景将变成完全黑色......当滚动停止时,背景将会大部分时间恢复其颜色......当我测试时,我还在该根中添加了一个TextView
具有相同背景的元素,当滚动列表时,这个将保留它的颜色...任何想法为什么会发生这种情况,以及如何解决这个问题?
你可以像这样使用:
list.setCacheColorHint(Color.TRANSPARENT);list.requestFocus(0);
在布局文件中使用此行非常简单:
android:scrollingCache="false"
像这样:
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"
这个问题有很多答案,但今天我意识到这个问题仍然缺少一个重要的信息。
这个问题有两种可能的解决方案 ,两种方法都有效,但每种方法都应该用于不同的情况。
方法
当ListView
具有纯色背景时,请使用android:cacheColorHint
。
- @android:color/transparent
当ListView
将(复杂) 图像作为背景时,请使用android:scrollingCache
。
- false
当ListView
具有纯色背景时,两种方法都可以工作,因此不仅cacheColorHint
可以工作。 但是不建议将scrolingCache
方法用于纯色背景,因为它会关闭用于平滑动画和滚动ListView的优化方法。
注意注意: scrolingCache
设置为false并不一定意味着ListView
的动画,滚动会变慢。
android:cacheColorHint="@android:color/transparent"
android:cacheColorHint="#00000000"// setting transparent color
要么
不要设置listview
背景。
在ListView
标记上添加属性
android:cacheColorHint="#00000000" // setting transparent color
有关详细信息,请查看
在你的xml中使用Listview
设置
android:cacheColorHint="@android:color/transparent"
我在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); } }
以下对我有用:
myListView.setScrollingCacheEnabled(false);
转载地址:http://zzcnb.baihongyu.com/