2012-01-31 26 views
1

我知道這個問題被問了好幾次,但沒有答案讓我找到解決方案。這是我的問題:我創建了一個約50個項目的ListView。他們中幾乎沒有一個需要有一個白色背景,而另一個有黃色背景。要做到這一點,我創建了兩個佈局feed1.xml和feed2.xml:ListView中具有不同背景色的項目

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@color/yellow" 
    > 
    <ImageView 
     android:id="@+id/ReadUnread" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_toLeftOf="@+id/FeedItem" 
     android:src="@drawable/readunread" 
     android:background="@color/yellow" 
    > 
    </ImageView> 
    <TextView 
     android:id="@+id/FeedItem" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:textSize="12sp" 
     android:textColor="#000" 
     android:textStyle="bold" 
     android:background="@color/yellow"> 
    </TextView> 
</LinearLayout> 

第二個是完全不同的背景色我改爲「@顏色/白」一樣。

其創建ListView的是這樣的(這是當然的精簡版)的源代碼:

public class ItemsListActivity extends ListActivity implements Runnable { 

    static final int FEED_RESULT = 0; 
    private ArrayList<Feed> feeds; 
    private FeedAdapter m_feedAdapter; 
    public ListView lv; 

    private Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      createListAdapter(); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    public void createListAdapter() 
    { 
     m_feedAdapter = new FeedAdapter(this, R.layout.feed, feeds); 
     setListAdapter(m_feedAdapter); 

     lv = getListView(); 
     lv.setTextFilterEnabled(true); 
     lv.setCacheColorHint(0); 
     lv.setDividerHeight(2); 
     lv.setScrollingCacheEnabled(false); 


     lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       Intent intent = new Intent(ItemsListActivity.this, 
          ReaderActivity.class); 
       Bundle bundle = new Bundle(); 
       bundle.putInt("id", feeds.get(position).getId()); 
       intent.putExtras(bundle); 
       startActivityForResult(intent, FEED_RESULT); 
      } 
     }); 
    } 

    @Override 
    public void run() { 
     Bundle bundle = this.getIntent().getExtras(); 
     handler.sendEmptyMessage(0); 
    } 

    private Boolean getFeeds() { 
     feeds = new ArrayList<Feed>(); 
    } 

    private class FeedAdapter extends ArrayAdapter<Feed> { 

     private ArrayList<Feed> feeds; 

     public FeedAdapter(Context context, int textViewResourceId, 
       ArrayList<Feed> feeds) { 
      super(context, textViewResourceId, feeds); 
      this.feeds = feeds; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      Feed f = feeds.get(position); 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       if (f.getWhite() == 0) 
        v = vi.inflate(R.layout.feed1, null); 
       else 
        v = vi.inflate(R.layout.feed2, null); 
      } 
      if (f != null) { 
       TextView tt = (TextView) v.findViewById(R.id.FeedItem); 
       ImageView im = (ImageView) v.findViewById(R.id.ReadUnread); 
       if (tt != null) { 
        tt.setText(Html.fromHtml("<b>"+f.getSubject()+"</b>"+"<small> >>"+f.getAuthor()+"</small>")); 
        tt.setTag(f.getId()); 
       } 
      } 
      return v; 
     } 
    } 
} 

當我運行我的應用程序(在模擬器或設備上),這似乎是巨大的直到我滾動。當我滾動列表時,很少有黃色的東西變成白色,然後當我再次滾動時,它們又變成了黃色。白色的物品完全一樣。它似乎是完全隨機的......背景顏色被交換和再次交換...我嘗試了很多東西(比如setCacheColorHint()或setScrollingCacheEnabled()),但似乎沒有任何工作。你有什麼主意嗎?

Thx尋求幫助。

解決方案

好了,問題解決了!

兩個重要的事情要修改:

  1. 由於回收的,這顯然不是創建兩個佈局是一個好主意。最好的解決方案是隻創建一個佈局並在getView()函數中設置背景顏色。
  2. 通過這樣做,有必要設置所有情況下的背景顏色!即使設置了默認背景顏色(在我的情況下爲「黃色」)。

所以我getView權實現()函數變爲:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     Feed f = feeds.get(position); 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.feed1, null); 
     } 
     if (f != null) { 
      TextView tt = (TextView) v.findViewById(R.id.FeedItem); 
      ImageView im = (ImageView) v.findViewById(R.id.ReadUnread); 
      if (tt != null) { 
       tt.setText(Html.fromHtml("<b>"+f.getSubject()+"</b>"+"<small> >>"+f.getAuthor()+"</small>")); 
       tt.setTag(f.getId()); 
       if (f.getWhite == 0) { 
         im.setBackgroundResource(R.color.white); 
         tt.setBackgroundResource(R.color.white); 
       } 
       else { 
         im.setBackgroundResource(R.color.yellow); 
         tt.setBackgroundResource(R.color.yellow); 
       } 
      } 
     } 
     return v; 
    } 
+0

提示:不要創建2個佈局,只需創建一個,然後在適配器中以編程方式分配顏色。 – st0le 2012-01-31 10:40:00

+0

謝謝!它現在有效! – user1175828 2012-01-31 14:03:46

回答