[英]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.