2014-03-01 21 views
-2

我知道,像方法:有多種佈局使用列表視圖

@Override 
public int getItemViewType(int position) { 
// Define a way to determine which layout to use, here it's just evens and odds. 
return position % 2; 
} 

@Override 
public int getViewTypeCount() { 
return 2; // Count of different layouts 
} 

是有益的,當我們要使用多個佈局與單個適配器。 但是,這些方法根據列表視圖中行的「位置」確定要使用的佈局。但是,就我而言,要使用的佈局取決於要在行中顯示的數據類型。所以,對於行位置,我不能知道要使用哪種佈局。 請幫忙!

回答

0

是的!在這裏我找到了解決方案。我只需要使用我的數據結構來找到正確的行。

public int getItemViewType(int position) { 

//we have an image so we are using an ImageRow 
if (animals.get(position).getImageId() != null) return 0; 

//we don't have an image so we are using a Description Row 
else return 1; 
} 
2

您可以在適配器中的對象內使用類型(或其他檢查)。 喜歡的東西:

protected static final int TYPE_ROW = 0; 
protected static final int TYPE_HEADER = 1; 

@Override 
public int getItemViewType(int position) { 
    if (getItem(position).isHeader()) 
     return TYPE_HEADER; 
    return TYPE_ROW; 
} 

然後你就可以在getView()一起使用,例如方法:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    MyRow item = getItem(position); 
    int viewType = this.getItemViewType(position); 
    switch (viewType) { 
     case TYPE_HEADER: 
      // 
      break; 
     case TYPE_ROW: 
      // 
      break; 
    } 
    ///.... 
}