我想要做的是显示书籍的一些数据,例如标题、作者和封面,并在这些数据下方有一个包含可点击章节列表的 ListView。
我想到的一种方法是使用具有不同类型单元格的 ListView,其中第一个单元格包含数据,其余单元格是包含章节的简单单元格。 ListView 绑定(bind)到实现 newView(...) 和 bindView(...) 方法的自定义 CursorAdapter,我不知道如何自定义这些方法以便根据单元格的位置返回不同类型的单元格。
@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;
}
我该怎么做?
谢谢!
最佳答案
您可以通过以下方式执行此操作:
- 重写
getViewTypeCount()
并返回不同行布局的数量 - 重写
getItemViewType()
并返回用于给定位置
的行布局的从 0 开始的索引 - 调整
newView()
和bindView()
以根据位置使用正确的行类型(例如,newView()
会膨胀不同的行类型)基于位置
的行布局)
或者,您可以尝试使用我的 MergeAdapter
之类的东西组装适配器。 .
关于android - Android ListView 中的多种单元格类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3918316/