Android使用分类型RecyclerView仿各大商城首页

  

* *正所谓,一入商城深似海~
  商城类的应用,确实是有许多东西值得学习,但是只要略微斟酌一下,你又会发现,它们之间存在着许多不谋而合的相似,也就是所谓的雷同~既然如此,让我们也来接下地气,先从一个简单的首页做起吧~ * *

  

实现的效果如下图:

  

 Android使用分类型RecyclerView仿各大商城首页

  

<强> 1。大布局就是一个简单的RecyclerView:
  

  

也可以通过添加多个头实现

  

4。这里我仅以四种类型为例

     /* *   * 4种类型   *//* *   *类型1:黑色星期五——使用横幅实现   */公共静态最终int BLACK_5_BANNER0=0;/* *   *类型2:今日新品——使用显示数据表格实现   */公共静态最终int TODAY_NEW_GV1=1;/* *   *类型3:品牌福利——使用ImageView实现   */公共静态最终int PIN_PAI_IV2=2;/* *   *类型4:搭配趋势,使用RecyclerView实现   */公共静态最终int DAPEIQS_GV3=3;/* *   *当前类型   */公共int currentType=BLACK_5_BANNER0;      之前      

写构造器并传入参数,完善getItemCount()和getItemType()方法

     /* *   *数据对象   */私人最终上下文mContext;   私人最终ListmoduleBeanList;//以后用它来初始化布的局   私人最终LayoutInflater mLayoutInflater;//构造器   公共HomeRecycleViewAdapter3(上下文mContext ListmoduleBeanList) {   这一点。mContext=mContext;   这一点。moduleBeanList=moduleBeanList;//以后用它来初始化布的局   mLayoutInflater=LayoutInflater.from (mContext);   }         @Override   公共int getItemCount () {//以后完成后改为4,现在只完成第一种类型暂时写1   返回1;   }      @Override   公共int getItemViewType (int位置){   开关(位置){   案例BLACK_5_BANNER0:   currentType=BLACK_5_BANNER0;   打破;   案例TODAY_NEW_GV1:   currentType=TODAY_NEW_GV1;   打破;   案例PIN_PAI_IV2:   currentType=PIN_PAI_IV2;   打破;   案例DAPEIQS_GV3:   currentType=DAPEIQS_GV3;   打破;   }   返回currentType;   }      之前      

5。下面就来一一实现这四种类型
  

  

5.1设置第1种类型——黑色星期五(使用的是横幅)的适配器

        @Override   公共int getItemCount () {//以后完成后改为4,现在只完成第一种类型暂时写1   返回1;   }      @Override   公共RecyclerView.ViewHolder>   公开课TodayGVAdapter延伸BaseAdapter {      私人最终上下文mContext;   私人最终Listmodule1data;      公共TodayGVAdapter(上下文mContext Listmodule1data) {   这一点。mContext=mContext;   这一点。module1data=https://www.yisu.com/zixun/module1data;   }      @Override   公共int getCount () {   返回module1data==零?0:module1data.size ();   }      @Override   公共对象getItem (int i) {   返回null;   }      @Override   公共长getItemId (int i) {   返回0;   }      @Override   公众视线getView (int位置、视图convertView ViewGroup ViewGroup) {   ViewHolder持有人;   如果(convertView==null) {//项目的布局:垂直线性,ImagView + TextView   convertView=视图。充气(mContext出来。item_channel, null);   持有人=new ViewHolder ();   持有人。iv_channel=(ImageView) convertView.findViewById (R.id.iv_channel);   持有人。tv_channel=(TextView) convertView.findViewById (R.id.tv_channel);   convertView.setTag(持有人);   其他}{   持有人=(ViewHolder) convertView.getTag ();   }//装配数据   WomenBean.WomenData.ModuleBean。DataBean datasig=module1data.get(位置);//使用滑翔加载图片   Glide.with (mContext) .load (datasig.getImg ()) .into (holder.iv_channel);//设置文本   holder.tv_channel.setText (datasig.getTitle ());      返回convertView;   }      公共静态类ViewHolder {   公共ImageView iv_channel;   公共TextView tv_channel;   }   }      之前      

5.3第3种类型——品牌福利——(直接使用ImagView)

        @Override   公共int getItemCount () {//以后完成后改为4,现在只完成第3种类型暂时写3   返回3;   }      @Override   公共RecyclerView.ViewHolder>   @Override   公共int getItemCount () {//四种类型都已经完成,返回4   返回4;   }      @Override   公共RecyclerView.ViewHolder>   公共类DaPeiQSRecycleViewAdapter RecyclerView延伸。适配器{   私人最终上下文mContext;   私人最终List

Android使用分类型RecyclerView仿各大商城首页