解決ListView捲動時背景變黑的狀況

ListView Scroll

發生的情況在於將 ListView 放置在 LinearLayout 中(或其他類似情況),LinearLayout 有設定背景顏色(非黑色)或圖片,當 ListView 在做捲動的動作時,會短暫變成黑底,LinearLayout 中設定的顏色或圖片會看不到,直到 ListView 的捲動停止時,才會又恢復。
解決方法很簡單,只要把 ListView 的 cacheColorHint 屬性設為 #00000000 即可,用意在於設定為透明色,如此就解決捲動時變黑底的狀況。
<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/background">
  <ListView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:cacheColorHint="#00000000"
    android:id="@+id/listAll">
  </ListView>
</LinearLayout>
大概的界面設定會是這樣,LinearLayout 設定背景,ListView把cacheColorHint 設為透明,就這樣。
本文網址:http://blog.tonycube.com/2011/10/listview.html
Tony Blog 撰寫,請勿全文複製,轉載時請註明出處及連結,謝謝 😀

2 則留言

  1. 你好,

    想請教一下, 我正在寫一個程式. 在SQLite上有加,減,改record功能的.咁我設計上是用listview顯示SQLite內的record. 之後, 在item上長按就可出另一個menu做加, 減, 改的動作. 但我就搞不好如果在長按時能拿到item的ID? 能否請教一下呢? 謝謝

    回覆刪除
    回覆
    1. 實作 onItemLongClick 方法(https://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html)
      透過第 3 個參數 position 為索引,就能取得該索引的資料

      刪除

留言小提醒:
1.回覆時間通常在晚上,如果太忙可能要等幾天。
2.請先瀏覽一下其他人的留言,也許有人問過同樣的問題。
3.程式碼請先將它編碼後再貼上。(線上編碼:http://bit.ly/1DL6yog)
4.文字請加上標點符號及斷行,難以閱讀者恕難回覆。
5.感謝您的留言,您的問題也可能幫助到其他有相同問題的人。