1. 程式人生 > >高仿各大商城首頁---使用分型別的RecyclerView來實現

高仿各大商城首頁---使用分型別的RecyclerView來實現

**正所謂,一入商城深似海~ 商城類的App,確實是有許多東西值得學習,但是隻要略微斟酌一下,你又會發現,它們之間存在著許多不謀而合的相似,也就是所謂的雷同~既然如此,讓我們也來接下地氣,先從一個簡單的首頁做起吧~** 源部落格http://blog.csdn.net/cjm2484836553/article/details/53363233 實現的效果如下圖:

這裡寫圖片描述

1.大布局就是一個簡單的RecyclerView:也可以通過新增多個header實現

4.這裡我僅以四種類型為例

/** 
    * 4種類型 
    */  
   /** 
    * 型別1:黑色星期五--使用banner實現 
    */  
   public static final int BLACK_5_BANNER0 = 0;  
   /** 
    *型別2:今日新品--使用GridView實現 
    */  
   public static final int TODAY_NEW_GV1 = 1;  
   /** 
    * 型別3:品牌福利--使用ImageView實現 
    */  
   public static final int PIN_PAI_IV2=2;  

   /** 
    * 型別4:搭配趨勢--使用RecyclerView實現 
    */  
   public static final int DAPEIQS_GV3 =3;  


   /** 
    * 當前型別 
    */  
   public int currentType = BLACK_5_BANNER0;  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

5.下面就來一一實現這四種類型

@Override  
    public int getItemViewType(int position) {  
        switch (position) {  
            case BLACK_5_BANNER0:  
                currentType = BLACK_5_BANNER0;  
                break;  
            case TODAY_NEW_GV1:  
                currentType = TODAY_NEW_GV1;  
                break;  
            case PIN_PAI_IV2:  
                currentType = PIN_PAI_IV2;  
                break;  
            case DAPEIQS_GV3:  
                currentType = DAPEIQS_GV3;  
                break;  
        }  
        return currentType;  
    }  


@Override  
   public int getItemCount() {  
       //四種類型 有幾種型別就寫幾
       return 4;  
   }  
  /**
  * 不同的型別建立不同的佈局  使用不同的holder
  */
   @Override  
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
       if (viewType == BLACK_5_BANNER0) {  
           return new BBNViewHolder(mContext, mLayoutInflater.inflate(R.layout.banner_viewpager, null));  
       } else if (viewType == TODAY_NEW_GV1) {  
           return new TODAYViewHolder(mContext, mLayoutInflater.inflate(R.layout.gv_channel, null));  
       } else if (viewType == PIN_PAI_IV2) {  
           return new PINPAIViewHolder(mContext, mLayoutInflater.inflate(R.layout.iv_pinpai, null));  
       } else if (viewType == DAPEIQS_GV3) {  
           //佈局:垂直線性,TextView+RecyclerView  
           return new DaPeiViewHolder(mContext, mLayoutInflater.inflate(R.layout.dapeiqs_rv, null));  
       }  
       return null;  
   }  

   @Override  
   public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {  
       if (getItemViewType(position) == BLACK_5_BANNER0) {  
           BBNViewHolder bbnViewHolder = (BBNViewHolder) holder;  
           List<WomenBean.WomenData.ModuleBean.DataBean> module0data = moduleBeanList.get(0).getData();  
           bbnViewHolder.setData(module0data);  
       } else if (getItemViewType(position) == TODAY_NEW_GV1) {  
           TODAYViewHolder todayViewHolder = (TODAYViewHolder) holder;  
           List<WomenBean.WomenData.ModuleBean.DataBean> module1data = moduleBeanList.get(1).getData();  
           todayViewHolder.setData(module1data);  
       } else if (getItemViewType(position) == PIN_PAI_IV2) {  
           PINPAIViewHolder pinpaiViewHolder = (PINPAIViewHolder) holder;  
           List<WomenBean.WomenData.ModuleBean.DataBean> pinpai2data = moduleBeanList.get(2).getData();  
           pinpaiViewHolder.setData(pinpai2data);  
       } else if (getItemViewType(position) == DAPEIQS_GV3) {  
           DaPeiViewHolder dapeiViewHolder = (DaPeiViewHolder) holder;  
           List<WomenBean.WomenData.ModuleBean.DataBean> dapeiqs6data = moduleBeanList.get(6).getData();  
           dapeiViewHolder.setData(dapeiqs6data);  
       }  
   }  
  /**
  *  這裡只寫了一種holder (有幾種型別就需要新增幾個holder)
  */
   class DaPeiViewHolder extends RecyclerView.ViewHolder {  
       private final Context mContext;  
       private RecyclerView dapeiqs_rv;  

       public DaPeiViewHolder(Context mContext, View itemView) {  
           super(itemView);  
           this.mContext = mContext;  
           dapeiqs_rv = (RecyclerView) itemView.findViewById(R.id.dapeiqs_rv);  
       }  

       public void setData(List<WomenBean.WomenData.ModuleBean.DataBean> dapeiqs6data) {  
           //1.已有資料  
           //2.設定介面卡  
           DaPeiQSRecycleViewAdapter adapter = new DaPeiQSRecycleViewAdapter(mContext, dapeiqs6data);  
           dapeiqs_rv.setAdapter(adapter);  

           //recycleView不僅要設定介面卡還要設定佈局管理者,否則圖片不顯示  
           LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);  
           dapeiqs_rv.setLayoutManager(manager);  
       }  
   }  

--------------------- 本文來自 kklxb 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/qq_22491765/article/details/56008981?utm_source=copy