Android GridView,回收两个不同的单元格?
Android GridView, recycle two different cells?
我有一个 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;
}
效果很好。请注意,我只是不回收 header 电池。没问题,因为只有两个。
但是如果您想要一个包含两个完全不同的单元格的 GridView 怎么办? (想象一个 GridView,每种类型有 50 个,全部混合在一起。)
我的意思是,两者大小相同,但完全不同,两个不同的 xml 文件,完全不同的布局?
你好"recycle both simultaneously"吗?
这是怎么回事?
不想回答我自己的问题,但是
感谢一如既往的 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”的案例......漂亮、可爱的东西。
我有一个 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;
}
效果很好。请注意,我只是不回收 header 电池。没问题,因为只有两个。
但是如果您想要一个包含两个完全不同的单元格的 GridView 怎么办? (想象一个 GridView,每种类型有 50 个,全部混合在一起。)
我的意思是,两者大小相同,但完全不同,两个不同的 xml 文件,完全不同的布局?
你好"recycle both simultaneously"吗?
这是怎么回事?
不想回答我自己的问题,但是
感谢一如既往的 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”的案例......漂亮、可爱的东西。