ListView中使用ViewHolder,setBackground失效

情境:

在 Fragement 中使用了 listView,listView 中 也用了 viewHolder

當在程式中動態指定要更換某個 id 的 row 中的背景時

someView.setBackground();

發現每個 row 的背景開始亂跳

有些非指定的 row 竟然也會更換背景

後來一查

這是用view holder中常見的error

只要再將背景變回來即可….

[sourcecode language=”JAVA”]

//view holder處理

…..

//針對某列處理
if (要對TextViewB做動作的條件) {
holder.TextViewB.setBackground(要換的背景圖);
} else {
holder.TextViewB.setBackground(原來的背景圖);
}

[/sourcecode]

資料來源:
http://stackoverflow.com/questions/18986730/personalized-listview-error-on-set-background-color

另外

設背景圖後
如果要動態移除背景圖,可以使用下面方式

[sourcecode language=”JAVA”]
view.setBackgroundResource(0);

[/sourcecode]

 

資料來源:
http://stackoverflow.com/questions/6826564/android-remove-background-drawable-programmatically

7 關於 “ListView中使用ViewHolder,setBackground失效” 的評論

  1. adult

    What’s up friends, how is everything, and what you would
    like to say regarding this piece of writing, in my view its really amazing
    in support of me.

    回覆
  2. Poppy

    Annd wһɑt are the resᥙlts if yyou create a pig
    of yoᥙrself, you gain extra weight. Many people usualⅼү do not recognize
    the signbs and symptoms ⲟf diabetes
    еspecially ѡhenever you hɑve no membеr ߋf the family, ߋr friend thаt’s
    diabetic. Once уou becomе overweight, аnd then pack more kiilos or pounds, what’ѕ more,
    it contributes tⲟ an increase inide riskks օf developing different
    types of cancers, including hіgher likelihood оf developing colon cancer,
    gall bladder, kidney, prostate, post-menopausal ɑnd endometrial cancer.

    回覆
  3. Lelanddex

    Amazing things here. I’m very satisfied to look your post. Thanks so much and I am having a look ahead to contact you. Will you kindly drop me a e-mail?

    回覆
  4. สุขภาพ

    Fantastic beat ! I would like to apprentice while you amend your website, how
    could i subscribe for a blog web site? The account aided me a acceptable deal.
    I were tiny bit familiar of this your broadcast offered vivid clear idea

    回覆

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *