Android的列表视图具有不同的布局的每一行? [英] Android Listview with different layout for each row?

查看:158
本文介绍了Android的列表视图具有不同的布局的每一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建列表视图中,我想对所有不同的行不同的布局。
那么,我怎么可以创建设置不同的布局不同的行自定义适配器。

I want to create Listview in which I want different layout for all different row. Then how can I create custom adapter for set different layout for different row.

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

感谢您提前。

推荐答案

您需要扩展适配器,并覆盖其 getView 方法。

You need to extend your Adapter, and override its getView method.

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resource;

    // Here you set ‘resource’ with the correct layout, for the row
    // given by the parameter ‘position.’
    //
    // E.g.:
    //
    // switch (someArray[position].type) {
    //   case SOME_TYPE_A: resource = R.layout.a; break;
    //   case SOME_TYPE_B: resource = R.layout.b; break;
    //   ...
    // }

    View rowView = inflater.inflate(resource, parent, false);

    // Here you initialize the contents of the newly created view.
    //
    // E.g.:
    // switch (resource) {
    //   case R.layout.a:
    //      TextView aA = (TextView) rowView.findViewById(R.id.aa);
    //      aA.setText("View 1");
    //      ...
    //      break;
    //   case R.layout.b:
    //      TextView bB = (TextView) rowView.findViewById(R.id.bb);
    //      bB.setText("View 2");
    //      ...
    //      break;
    //   ...
    // }

    return rowView;
}

有关适配器更多的例子,以及如何扩展它们,请参见下面的链接。

For more examples on adapters and how to extend them, see the links below.


  • <一个href=\"http://www.vogella.com/articles/AndroidListView/article.html#adapterown\">http://www.vogella.com/articles/AndroidListView/article.html#adapterown

<一个href=\"http://devtut.word$p$pss.com/2011/06/09/custom-arrayadapter-for-a-listview-android/\">http://devtut.word$p$pss.com/2011/06/09/custom-arrayadapter-for-a-listview-android/

这篇关于Android的列表视图具有不同的布局的每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆