Android 电商 四种条目 加轮播图效果

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements HotAdapter.HotClicklinsenter,StyleAdapter.StyleClicklinsenter,
        LiveAdapter.LiveClicklinsenter{

    private Context context;
    private HomeBean homeBean;
    private List<BannerBean.ResultBean> bannerbean;
    private final int BANNER = 0;
    private final int HOT = 1;
    private final int STYLE = 2;
    private final int LIVE = 3;
    private List<String> list;

    public HomeAdapter(Context context) {
        this.context = context;
        homeBean=new HomeBean();
        bannerbean = new ArrayList<>();
    }

    public void setHomeBean(HomeBean homeBean) {
        this.homeBean = homeBean;
        notifyDataSetChanged();
    }

    public void setBannerbean(List<BannerBean.ResultBean> bannerbean) {
        list = new ArrayList<>();
        for (BannerBean.ResultBean resultBean : bannerbean) {
            list.add(resultBean.getImageUrl());
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view;
        if (getItemViewType(i)==BANNER){
            view = LayoutInflater.from(context).inflate(R.layout.main_item_banner,viewGroup,false);
            return new ViewHoder_banner(view);
        }else if (getItemViewType(i)==HOT){
            view = LayoutInflater.from(context).inflate(R.layout.main_item_rxsp,viewGroup,false);
            return new ViewHoder_hot(view);
        }else if (getItemViewType(i)==STYLE){
            view = LayoutInflater.from(context).inflate(R.layout.main_item_mlss,viewGroup,false);
            return new ViewHoder_style(view);
        }else {
            view = LayoutInflater.from(context).inflate(R.layout.main_item_pzsh,viewGroup,false);
            return new ViewHoder_live(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int i) {
    if (getItemViewType(i)==BANNER){
        if (list!=null) {
            ((ViewHoder_banner) holder).xBanner.setData(list,null);
            ((ViewHoder_banner) holder).xBanner.setmAdapter(new XBanner.XBannerAdapter() {
                @Override
             public void loadBanner(XBanner banner, Object model, View view, int position) {
              Glide.with(context).load(list.get(position)).into((ImageView) view);
            }
          });
        }
     }else if (getItemViewType(i)==HOT){
      HotAdapter hotAdapter = new HotAdapter(context, homeBean.result.rxxp.commodityList);
       ((ViewHoder_hot)holder).rxsp_rcve.setLayoutManager(new GridLayoutManager(context,3));
            hotAdapter.setHotClicklinsenter(this);
            ((ViewHoder_hot)holder).rxsp_rcve.setAdapter(hotAdapter);
        }else if (getItemViewType(i)==STYLE){
            StyleAdapter styleAdapter = new StyleAdapter(context,homeBean.result.mlss.commodityList);
            ((ViewHoder_style)holder).mlss_rcve.setLayoutManager(new LinearLayoutManager(context));
            styleAdapter.setStyleClicklinsenter(this);
            ((ViewHoder_style)holder).mlss_rcve.setAdapter(styleAdapter);
        }else{
            LiveAdapter liveAdapter = new LiveAdapter(context,homeBean.result.pzsh.commodityList);
            ((ViewHoder_live)holder).pzsh_rcve.setLayoutManager(new GridLayoutManager(context,2));
            liveAdapter.setLiveClicklinsenter(this);
            ((ViewHoder_live)holder).pzsh_rcve.setAdapter(liveAdapter);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return BANNER;
        }else if (position==1){
            return HOT;
        }else if (position==2){
            return STYLE;
        }else {
            return LIVE;
        }
    }

    @Override
    public int getItemCount() {
        return homeBean==null?0:4;
    }

    private HomeClicklinsenter homeClicklinsenter;
    public void setHomeClicklinsenter(HomeClicklinsenter homeClicklinsenter) {
        this.homeClicklinsenter = homeClicklinsenter;
    }

    public interface HomeClicklinsenter{
        void homeclick(String homeid);
    }


    @Override
    public void hotclick(String hotid) {
        homeClicklinsenter.homeclick(hotid);
    }


    @Override
    public void liveclick(String hotid) {
        homeClicklinsenter.homeclick(hotid);
    }

    @Override
    public void styleclick(String hotid) {
        homeClicklinsenter.homeclick(hotid);
    }
    

    class ViewHoder_banner extends RecyclerView.ViewHolder{

        private final XBanner xBanner;

        public ViewHoder_banner(View itemView) {
            super(itemView);
            xBanner = itemView.findViewById(R.id.xbanner);
        }
    }

    class ViewHoder_hot extends RecyclerView.ViewHolder{
        private final RecyclerView rxsp_rcve;

        public ViewHoder_hot(View itemView) {
            super(itemView);
            rxsp_rcve = itemView.findViewById(R.id.rxsp_recy);
        }
    }

    class ViewHoder_style extends RecyclerView.ViewHolder{
        private final RecyclerView mlss_rcve;

        public ViewHoder_style(View itemView) {
            super(itemView);
            mlss_rcve = itemView.findViewById(R.id.mlss_recy);
        }
    }

    class ViewHoder_live extends RecyclerView.ViewHolder{
        private final RecyclerView pzsh_rcve;

        public ViewHoder_live(View itemView) {
            super(itemView);
            pzsh_rcve = itemView.findViewById(R.id.pzsh_recy);
        }
    }
}

第二个条目适配器

public class HotAdapter extends RecyclerView.Adapter<HotAdapter.Myvh> {

    private Context context;
    private List<HomeBean.ResultBean.listBean.CommodityListBean> list;

    public HotAdapter(Context context, List<HomeBean.ResultBean.listBean.CommodityListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.main_item_rxsp_item,parent,false);
        return new Myvh(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Myvh holder, int position) {

        Glide.with(context).load(list.get(position).masterPic).into(holder.imageView);
        holder.name.setText(list.get(position).commodityName);
        holder.price.setText(list.get(position).price+"");
    }

    @Override
    public int getItemCount() {

        return list.size();
    }

    public class Myvh extends RecyclerView.ViewHolder {

        private final ImageView imageView;
        private final TextView price;
        private final TextView name;

        public Myvh(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image1);
            name = itemView.findViewById(R.id.name);
            price = itemView.findViewById(R.id.price);

        }
    }
}

第三个条目适配器

public class StyleAdapter extends RecyclerView.Adapter<StyleAdapter.Myvh> {

    private Context context;
    private List<HomeBean.ResultBean.listBean.CommodityListBean> list;

    public StyleAdapter(Context context, List<HomeBean.ResultBean.listBean.CommodityListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.main_item_mlss_item,parent,false);
        return new Myvh(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Myvh holder, int position) {
        Glide.with(context).load(list.get(position).masterPic).into(holder.imageView);
        holder.name.setText(list.get(position).commodityName);
        holder.price.setText(list.get(position).price+"");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Myvh extends RecyclerView.ViewHolder {

        private final ImageView imageView;
        private final TextView price;
        private final TextView name;

        public Myvh(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image1);
            name = itemView.findViewById(R.id.name);
            price = itemView.findViewById(R.id.price);
        }
    }
}

第四个条目适配器

public class LiveAdapter extends RecyclerView.Adapter<LiveAdapter.Myvh> {

    private Context context;
    private List<HomeBean.ResultBean.listBean.CommodityListBean> list;

    public LiveAdapter(Context context, List<HomeBean.ResultBean.listBean.CommodityListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.main_item_pzsh_item,parent,false);
        return new Myvh(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Myvh holder, int position) {
        Glide.with(context).load(list.get(position).masterPic).into(holder.imageView);
        holder.name.setText(list.get(position).commodityName);
        holder.price.setText(list.get(position).price+"");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Myvh extends RecyclerView.ViewHolder {

        private final ImageView imageView;
        private final TextView price;
        private final TextView name;

        public Myvh(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image1);
            name = itemView.findViewById(R.id.name);
            price = itemView.findViewById(R.id.price);

        }
    }
}

bean类

public class HomeBean {

    public ResultBean result;
    public String message;
    public String status;

    public static class ResultBean {
        public listBean rxxp;
        public listBean pzsh;
        public listBean mlss;

        public static class listBean {
            public int id;
            public String name;
            public List<CommodityListBean> commodityList;

            public static class CommodityListBean {

                public int commodityId;
                public String commodityName;
                public String masterPic;
                public int price;
                public int saleNum;
            }
        }
    }

}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,033评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,725评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,473评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,846评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,848评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,691评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,053评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,700评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,856评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,676评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,787评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,430评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,034评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,990评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,218评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,174评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,526评论 2 343

推荐阅读更多精彩内容