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:
- Overriding
getViewTypeCount()and returning the number of distinct row layouts - Overriding
getItemViewType()and returning a 0-based index of the row layout to be used for a givenposition - Adjusting
newView()andbindView()to use the right row type based on the position (e.g.,newView()inflates different row layouts based onposition)
Or, you can try assembling your adapter from pieces, using something like my MergeAdapter.
加载中,请稍侯......
精彩评论