gpt4 book ai didi

Android GridView,回收两个不同的单元格?

转载 作者:行者123 更新时间:2023-11-30 00:52:03 24 4
gpt4 key购买 nike

我有一个 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 文件,完全不同的布局?

如何“同时回收两者”?

这是怎么回事?

最佳答案

不想回答我自己的问题,但是

感谢一如既往的 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;
}

这是“Good Android”的案例……漂亮、可爱的东西。

关于Android GridView,回收两个不同的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40872326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com