2012-11-09 48 views
1

我重寫我的項目中getItemViewType()方法來表示我的列表中使用的項目其觀點,R.layout.listview_item_product_completeR.layout.listview_item_product_inprocess使用getItemViewType() - 我怎麼知道哪個佈局是0,哪個是1?

我知道這個功能必須小於可能的視圖數0和1之間返回一個值,在我的情況下0或1.

我怎麼知道哪個佈局是0,哪個是1?我認爲我創建的第一個佈局將爲0,後一個爲1,但我想返回一個變量,以便該值可以靈活地使用。...

即,

@Override 
public int getItemViewType(int position) { 
    // Define a way to determine which layout to use 
    if(//test for inprocess){ 
     return INPROCESS_TYPE_INDEX; 
    } else { 
     return COMPLETE_TYPE_INDEX; 
    } 
} 

我在哪裏可以參考什麼/定義COMPLETE_TYPE_INDEXINPROCESS_TYPE_INDEX值是多少?

+0

「我怎麼知道哪些佈局0,這是1?」這取決於你的數據,什麼區別「完整」或「進程中」的行? (我們看不到你的數據...) – Sam

+0

(我剛剛注意到你以前的問題發表了你的評論,安迪因默認通知你,因爲你評論了他的回答,但是我沒有用@ @ Sam標記,所以我沒有得到通知...) – Sam

+0

@Sam哦,是的,我明白了。我剛剛製作了這兩個變量名。我不知道如何定義它們。我做了一個listview並添加了兩個佈局,名稱如上。我如何在數組中保留它們的實用標識符? (感謝關於通過@回覆某人的提示):) – jamis0n

回答

2

我需要知道如何定義COMPLETE_TYPE_INDEX爲1或0。好像這樣一件小事!

真的,不要緊COMPLETE_TYPE_INDEX是否爲0和INPROCESS_TYPE_INDEX是1,或正相反。但是,你將它們定義爲類變量,在這種情況下,他們可以staticfinal還有:

public class MyAdapter ... { 
    private static final int COMPLETE_TYPE_INDEX = 0; 
    private static final int INPROCESS_TYPE_INDEX = 1; 
    private static final int NUMBER_OF_LAYOUTS = 2; 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     if (convertView == null) { 
      if(getItemViewType(position) == COMPLETE_TYPE_INDEX) 
       convertView = mInflater.inflate(R.layout.listview_item_product_complete, null); 
      else // must be INPROCESS_TYPE_INDEX 
       convertView = mInflater.inflate(R.layout.listview_item_product_inprocess, null); 

      // etc, etc... 
      // Depending on what is different in your layouts, 
      // you may need update your ViewHolder and more of getView() 
     } 

     // Load data that changes on each row, might need to check index type here too 
    } 
    @Override 
    public int getItemViewType(int position) { 
     Order thisOrder = (Order) myOrders.getOrderList().get(position); 

     if(thisOrder.getOrderStatus().equals("Complete")) return COMPLETE_TYPE_INDEX; 
     else return INCOMPLETE_TYPE_INDEX; 
    } 
    @Override 
    public int getViewTypeCount() { 
     return NUMBER_OF_LAYOUTS; 
    } 
} 
+0

但是對於操作系統,不是'R.layout.listview_item_product_inprocess'特別對應於0或1?同樣用'R.layout.listview_item_product_complete'?我想我需要找出哪些索引在陣列中擁有這些佈局對象的每個佈局。 – jamis0n

+0

通過重寫'getViewTypeCount()'和'getItemViewType()'這就是你需要做的事情,回收者將處理剩下的工作。 – Sam

+0

好吧,我想我明白你在說什麼......所以理論上,我可以在getView()函數中實現getItemViewType()後面的邏輯。但操作系統也可能使用這個功能,所以我需要定義它。所以在這個函數中,我怎麼能''測試inprocess'塊?我需要在'位置'檢查有問題的視圖屬性,對嗎? – jamis0n

1

getView方法

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

if(getItemViewType(position) == INPROCESS_TYPE_INDEX){ 

//inflate a layout file 
convertView = inflater.inflate(R.layout.R.layout.listview_item_product_inprocess); 
} 

else{ 
{ 

//inflate a layout file 
convertView = inflater.inflate(R.layout.listview_item_product_complete); 

} 

return convertView; 
+0

是的,那是我打算做的。我只是不知道如何定義INPROCESS_TYPE_INDEX。我只是爲這個例子製作了這些變量,但我需要正確地將它們賦值爲0或1。 – jamis0n

相關問題