Android ListView中的多个单元格类型

geo*_*b89 2 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;
}
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

谢谢!

Com*_*are 5

你可以这样做:

  1. 覆盖getViewTypeCount()并返回不同行布局的数量
  2. 覆盖getItemViewType()并返回要用于给定的行布局的从0开始的索引position
  3. 根据位置调整newView()bindView()使用右行类型(例如,newView()根据不同的行布局膨胀position)

或者,您可以尝试使用像我这样的东西来组装适配器MergeAdapter.