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

标签 android android-gridview

我有一个 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/

相关文章:

android - 使用 GridView 的自定义适配器

android - 查找也使用我的 Android 应用程序的用户的 Facebook 好友?

java - 访问 obb 文件时出错

android - 如何在 GridView 布局中将 TextView 添加到 ImageView 中?

android - 如何在 android gridview 单元格中设置 colspan

android - 未在 GridView 上单击项目

java - 将对象从 Fragment 的 GridView 传递到另一个 Fragment 的 ListView

java - Android - 在WebView中显示本地镜像

android - 将 NavigationView 背景更改为圆形在按下时不起作用

android - 如何使用 ButterKnife 库将文本设置为多个 TextView