赞
踩
如上图所示,在使用RecyclerView过程中会出现不同布局item的情况,可以通过定义不同的ViewType来实现。
此demo以书籍的分类及详细信息作为数据。
首先定义父类item:ParentItem,作为Adapter的数据源
private int type; // 指定item的类型
private CategoryItem categoryItem; // 分类item
private BookItem bookItem; // 书item
书籍的分类:CategoryItem
private String categoryName;
书的详细内容:BookItem
private String bookName;
private String desc;
首先需要重写RecyclerView.Adapter中的getItemViewType()方法,根据不同的position,返回对应布局item的ViewType供onCreateViewHolder()使用。
@Override
public int getItemViewType(int position) {
if (mList.size() == 0 || position < 0 || position >= mList.size()) {
return -1;
}
ParentItem parentItem = mList.get(position);
return parentItem.getType();
}
基类ViewHolder,供业务ViewHolder实现,使用抽象方法在Adapter和ViewHolder中传递数据
public abstract class BaseHolder extends RecyclerView.ViewHolder {
public BaseHolder(@NonNull View itemView) {
super(itemView);
}
public abstract void bindView(List list, Object bean);
}
书籍的分类:CategoryViewHolder
public CategoryViewHolder(@NonNull View itemView) {
super(itemView);
}
@Override
public void bindView(List list, Object bean) {
// 分类布局的控件展示
}
书的详细内容:BookViewHolder
public BookViewHolder(@NonNull View itemView) {
super(itemView);
}
@Override
public void bindView(List list, Object bean) {
// 书籍布局的控件展示
}
根据getItemViewType()中返回的ViewType,调用不同的ViewHolder
@NonNull @Override public BaseHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { // i来自getItemViewType的return结果,默认为0,需重写getItemViewType switch (i) { case CATEGORY_ITEM_TYPE: return new CategoryViewHolder(LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.layout_category_view_holder, viewGroup, false)); case BOOK_ITEM_TYPE: return new BookViewHolder(LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.layout_book_view_holder, viewGroup, false)); default: return new BaseHolder(new LinearLayout(viewGroup.getContext())) { @Override public void bindView(List list, Object bean) { } }; } } @Override public void onBindViewHolder(@NonNull BaseHolder baseHolder, int i) { baseHolder.bindView(mList, mList.get(i)); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/text_view_category_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="15dp" android:textStyle="bold" android:textColor="#333333" android:layout_marginTop="35dp" android:layout_marginBottom="10dp" android:textSize="17sp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="15dp" android:layout_marginTop="10dp" android:background="#ffffff" android:orientation="vertical"> <TextView android:id="@+id/text_view_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:textColor="#333333" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/text_view_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:textColor="#666666" android:textSize="13sp" /> </LinearLayout>
public class CategoryViewHolder extends BaseHolder { private TextView textView; public CategoryViewHolder(@NonNull View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.text_view_category_top); } @Override public void bindView(List list, Object bean) { if (bean instanceof ParentItem) { CategoryItem item = ((ParentItem) bean).getCategoryItem(); textView.setText(item.getCategoryName()); } } }
public class BookViewHolder extends BaseHolder { private TextView textViewName; private TextView textViewDesc; public BookViewHolder(@NonNull View itemView) { super(itemView); textViewName = (TextView) itemView.findViewById(R.id.text_view_name); textViewDesc = (TextView) itemView.findViewById(R.id.text_view_desc); } @Override public void bindView(List list, Object bean) { if (bean instanceof ParentItem) { BookItem bookItem = ((ParentItem) bean).getBookItem(); textViewName.setText(bookItem.getBookName()); textViewDesc.setText(bookItem.getDesc()); } } }
public class BookAdapter extends RecyclerView.Adapter<BaseHolder> { private List<ParentItem> mList = new ArrayList<>(); public static final int CATEGORY_ITEM_TYPE = 1; public static final int BOOK_ITEM_TYPE = 2; public BookAdapter(List<ParentItem> list) { this.mList = list; } @NonNull @Override public BaseHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { // i来自getItemViewType的return结果,默认为0,需重写getItemViewType switch (i) { case CATEGORY_ITEM_TYPE: return new CategoryViewHolder(LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.layout_category_view_holder, viewGroup, false)); case BOOK_ITEM_TYPE: return new BookViewHolder(LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.layout_book_view_holder, viewGroup, false)); default: return new BaseHolder(new LinearLayout(viewGroup.getContext())) { @Override public void bindView(List list, Object bean) { } }; } } @Override public void onBindViewHolder(@NonNull BaseHolder baseHolder, int i) { baseHolder.bindView(mList, mList.get(i)); } @Override public int getItemCount() { return mList.size(); } @Override public int getItemViewType(int position) { if (mList.size() == 0 || position < 0 || position >= mList.size()) { return -1; } ParentItem parentItem = mList.get(position); return parentItem.getType(); } }
public class MainActivity extends AppCompatActivity { private List<ParentItem> mDatas = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); getItems(); LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); // 指定布局方式为线性布局 BookAdapter adapter = new BookAdapter(mDatas); recyclerView.setAdapter(adapter); } /** * 组装item数据 */ private void getItems() { List<Category> categoryList = initBooks(); for (int i = 0; i < categoryList.size(); i++) { Category category = categoryList.get(i); ParentItem item1 = new ParentItem(); // 组装CategoryItem CategoryItem categoryItem = new CategoryItem(); categoryItem.setCategoryName(category.getName()); item1.setType(BookAdapter.CATEGORY_ITEM_TYPE); item1.setCategoryItem(categoryItem); mDatas.add(item1); // 组装BookItem List<Book> bookList = category.getBooks(); for (int j = 0; j < bookList.size(); j++) { ParentItem item2 = new ParentItem(); Book book = bookList.get(j); BookItem bookItem = new BookItem(); bookItem.setBookName(book.getName()); bookItem.setDesc(book.getDesc()); item2.setType(BookAdapter.BOOK_ITEM_TYPE); item2.setBookItem(bookItem); mDatas.add(item2); } } } /** * 组装书籍数据 */ private List<Category> initBooks() { List<Category> categoryList = new ArrayList<>(); // 第一个分类 Category category1 = new Category(); category1.setName("科幻书"); List<Book> bookList1 = new ArrayList<>(); Book book1 = new Book(); book1.setName("三体"); book1.setDesc("《三体》是刘慈欣创作的系列长篇科幻小说,由《三体》、《三体Ⅱ·黑暗森林》、《三体Ⅲ·死神永生》组成," + "第一部于2006年5月起在《科幻世界》杂志上连载,第二部于2008年5月首次出版,第三部则于2010年11月出版。"); bookList1.add(book1); Book book2 = new Book(); book2.setName("太空漫游四部曲"); book2.setDesc("科幻殿堂的永恒经典, 太空时代的不朽史诗全新修订精装典藏版阿瑟·克拉克,这个时代最伟大的科幻大师和太空预" + "言家“太空漫游”系列,世界科幻文学巅峰之作。"); bookList1.add(book2); category1.setBooks(bookList1); categoryList.add(category1); // 第二个分类 Category category2 = new Category(); category2.setName("小说"); List<Book> bookList2 = new ArrayList<>(); Book book3 = new Book(); book3.setName("围城"); book3.setDesc("围城》是钱钟书所著的长篇小说,是中国现代文学史上一部风格独特的讽刺小说。"); bookList2.add(book3); Book book4 = new Book(); book4.setName("我与地坛"); book4.setDesc("《我与地坛》在 2002年5月由人民文学出版社出版发行的图书,作者为史铁生。"); bookList2.add(book4); Book book5 = new Book(); book5.setName("雪国"); book5.setDesc("是日本作家川端康成创作的第一部中篇小说,也是他唯美主义代表之作,从1935年起以短篇的形式,分别以《暮景的镜》、《白昼的镜》等题名。"); bookList2.add(book5); category2.setBooks(bookList2); categoryList.add(category2); // 第三个分类 Category category3 = new Category(); category3.setName("技术书"); List<Book> bookList3 = new ArrayList<>(); Book book6 = new Book(); book6.setName("第一行代码"); book6.setDesc("被Android开发者誉为“Android学习第一书”"); bookList3.add(book6); category3.setBooks(bookList3); categoryList.add(category3); return categoryList; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。