2016-11-29 18 views
1

我有一個GridView。它總是兩列。Android GridView,回收兩個不同的單元格?

對於僅位於頂部的前兩個單元格,我有不同的單元格。

在Apdater我做到這一點...

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

    if (position<2) { 
     // just return a new header cell - no need to try to be efficient, 
     // there are only ever two of the header cells 
     convertView = LayoutInflater.from(c).inflate(R.layout.cell_header, null); 
     return convertView; 
     // you're completely done 
    } 

    // from here, we want only a normal cell... 

    if (convertView == null) { 
     // just make a new one 
     convertView = LayoutInflater.from(c).inflate(R.layout.d.cell_normal, null); 
    } 

    // if you get to here, there's a chance it's giving us a header cell to recycle, 
    // if so get rid of it 

    int id = convertView.getId(); 
    if (id == R.id.id_cell_header) { 
     Log.d("DEV", "We got a header cell in recycling - dump it"); 
     convertView = LayoutInflater.from(c).inflate(R.layout.cell_normal, null); 
    } 

    ... populate the normal cell in the usual way 

    return convertView; 
    } 

這個偉大的工程。注意我只是不回收標題單元格。沒有問題,因爲只有兩個。

但是如果你想要一個GridView和兩個完全不同的單元格呢? (想象一下GridView與每種類型的說50,所有混合在一起。)

我的意思是,兩者都是相同的大小,但他們是完全不同的,兩個不同的XML文件,完全不同的佈局?

你如何「同時回收」?

這是怎麼回事?

+3

http://stackoverflow.com/a/24717323/115145 – CommonsWare

+0

@CommonsWare - 完全宏偉的,謝謝! – Fattie

回答

1

因爲不想回答我的問題,但

與感謝總是驚人CommonsWare ...

這裏正是你如何做到這一點:

@Override 
public int getViewTypeCount() { 
    // we have two different types of cells, so return that number 
    return 2; 
} 

@Override 
public int getItemViewType(int position) { 
    if (..position should be a header-type cell..) 
     return 1;  // 1 means to us "header type" 
    else 
     return 0;  // 0 means to us "normal type" 
    // note the 0,1 are "our own" arbitrary index. 
    // you actually don't have to use that index again: you're done. 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     if (..position should be a header-type cell..) 
      convertView = LayoutInflater.from(c).inflate(R.layout.cell_header, null); 
     else 
      convertView = LayoutInflater.from(c).inflate(R.layout.cell_normal, null); 
    } 

    if (..position should be a header-type cell..) { 
     // .. populate the header type of cell .. 
     // .. it WILL BE a header type cell .. 
    } 
    else { 
     // .. populate the normal type of cell .. 
     // .. it WILL BE a normal type cell .. 
    } 

    return convertView; 
} 

這是一個情況下, 「好Android」...美麗,可愛的東西。

相關問題