zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    java 代码规范 sun 公司
    软引用、弱引用、虚引用
    socket
    httpURLConnection、URL、httpClient、httpPost、httpGet
    android service aidl 理解
    Python2.7-codecs
    Python2.7-textwrap
    Python2.7-StringIO和cStringIO
    Python2.7-difflib
    Python2.7-struct模块
  • 原文地址:https://www.cnblogs.com/spring87/p/5427430.html
Copyright © 2011-2022 走看看