RecyclerView不同类型Item的展示
代码如下:
public class AccessoiresAdapter extends RecyclerView.Adapter { final int VIEW_TYPE_ACCESSORY = 0; final int VIEW_TYPE_ACCESSORY_SPECIAL_OFFER = 1; List<Accessory> items; @Override public int getItemViewType(int position) { Accessory accessory = items.get(postion); if (accessory.hasSpecialOffer()){ return VIEW_TYPE_ACCESSORY_SPECIAL_OFFER; } else { return VIEW_TYPE_ACCESSORY; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (VIEW_TYPE_ACCESSORY_SPECIAL_OFFER == viewType){ return new SpecialOfferAccessoryViewHolder(inflater.inflate(R.layout.item_accessory_offer, parent)); } else { return new AccessoryViewHolder (inflater.inflate(R.layout.item_accessory)): } } ... }
public class HomeAdapter extends AccessoriesAdapter { final int VIEW_TYP_NEWS_TEASER = 2; @Override public int getItemViewType(int position) { if (items.get(position) instanceof NewsTeaser){ return VIEW_TYP_NEWS_TEASER; } else { // accessories and special offers return super.getItemViewType(position); } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (VIEW_TYP_NEWS_TEASER == viewType){ return new NewsTeaserItem( inflater.inflate(R.layout.item_news_teaser, parent)); } else { // accessories and special offers return super.onCreateViewHolder(parent, viewType); } } ... }
From:http://hannesdorfmann.com/android/adapter-delegates
时刻怀有一颗虔诚之心,乐于分享。知识才更有意义。