开发者

Multiple Cell Types in Android ListView

What I'm trying to do is to show some data for a book like Title, Author, and a Cover, and beneath these data, to have a ListView containing a list of clickable chapters.

One way I've thought would be to have a ListView with different type of cells, where the first would contain the data, and the rest of them would be simple cells containing the chapters. The ListView binds to a custom CursorAdapter that im开发者_如何学Goplements the newView(...) and bindView(...) methods, which I don't know how to customise in order to return different types of cells based on the cell's position.

@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;
}

How should I do that?

Thanks!


You can do this by:

  1. Overriding getViewTypeCount() and returning the number of distinct row layouts
  2. Overriding getItemViewType() and returning a 0-based index of the row layout to be used for a given position
  3. Adjusting newView() and bindView() to use the right row type based on the position (e.g., newView() inflates different row layouts based on position)

Or, you can try assembling your adapter from pieces, using something like my MergeAdapter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜