Android ListView中的多个单元格类型

时间:2010-10-12 19:31:14

标签: android listview

我要做的是为Title,Author和Cover这样的书显示一些数据,并在这些数据下面,让ListView包含一个可点击的章节列表。

我认为有一种方法是使用不同类型的单元格的ListView,其中第一个包含数据,其余的将是包含章节的简单单元格。 ListView绑定到一个自定义的CursorAdapter,它实现了newView(...)和bindView(...)方法,我不知道如何自定义以便根据单元格的位置返回不同类型的单元格。

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView title = (TextView) view.findViewById(R.id.title);
    title.setText(cursor.getString(cursor.getColumnIndex(Constants.TITLE)));
    ..
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.bookcell, parent, false);
    bindView(v, context, cursor);
    return v;
}

我该怎么做?

谢谢!

1 个答案:

答案 0 :(得分:5)

您可以通过以下方式执行此操作:

  1. 覆盖getViewTypeCount()并返回不同行布局的数量
  2. 覆盖getItemViewType()并返回基于0的行布局索引,以用于给定的position
  3. 调整newView()bindView()以根据位置使用正确的行类型(例如,newView()根据position对不同的行布局进行膨胀)
  4. 或者,您可以尝试使用我的MergeAdapter

    之类的东西来组装您的适配器