多布局

第一种 banner的有两个集合

package com.example.zhao.geeknewsmvp.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhao.geeknewsmvp.R;
import com.example.zhao.geeknewsmvp.bean.RiBao;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;

public class RiBaoAdapter extends RecyclerView.Adapter {


    private static int VIEW_TYPE_BANNER = 0;
    private static int VIEW_TYPE_TIME = 1;
    private static int VIEW_TYPE_NEWS = 2;

    private Context context;
    private ArrayList<RiBao.TopStoriesBean> bannerList;
    private ArrayList<RiBao.StoriesBean> newsList;
    private String dataDate;

    public RiBaoAdapter(Context context, ArrayList<RiBao.TopStoriesBean> bannerList, ArrayList<RiBao.StoriesBean> newsList) {
        this.context = context;
        this.bannerList = bannerList;
        this.newsList = newsList;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == VIEW_TYPE_BANNER) {
            View banner = LayoutInflater.from(context).inflate(R.layout.ribao_banner, null);
            return new BannerViewHolder(banner);
        } else if (viewType == VIEW_TYPE_TIME) {
            View time = LayoutInflater.from(context).inflate(R.layout.ribao_text, null);
            return new TimeViewHolder(time);
        } else {
            View item = LayoutInflater.from(context).inflate(R.layout.ribao_item, null);
            return new NewsViewHolder(item);
        }


    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {


        int viewType = getItemViewType(position);
        if (viewType == VIEW_TYPE_BANNER) {
             BannerViewHolder holder1 = (BannerViewHolder) holder;
            ArrayList<String> list = new ArrayList<>();

            ArrayList<String> list1 = new ArrayList<>();
            for (int i = 0; i < bannerList.size(); i++) {
                list.add(bannerList.get(i).getImage());
                list1.add(bannerList.get(i).getTitle());
            }
            holder1.banner.setBannerTitles(list1);
            holder1.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
            holder1.banner.setImages(list)
                    .setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            Glide.with(context).load(path).into(imageView);

                        }
                    }).start();
        } else if (viewType == VIEW_TYPE_TIME) {

            ((TimeViewHolder) holder).txt.setText(dataDate);

        } else if (viewType == VIEW_TYPE_NEWS) {

            int newPosition = position - 1;
            if (bannerList.size() > 0) {
                newPosition -= 1;
            }
            NewsViewHolder holder1 = (NewsViewHolder) holder;
            holder1.name.setText(newsList.get(newPosition).getTitle());
            Glide.with(context).load(newsList.get(newPosition).getImages().get(0)).into(((NewsViewHolder) holder).img);

            final int finalNewPosition = newPosition;
            holder1.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.OnClick(finalNewPosition);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (bannerList.size() > 0) {
            return newsList.size() + 1 + 1;
        } else {
            return newsList.size() + 1;
        }

    }


    @Override
    public int getItemViewType(int position) {
        if (bannerList.size() > 0) {
            if (position == 0) {
                return VIEW_TYPE_BANNER;
            } else if (position == 1) {
                return VIEW_TYPE_TIME;
            } else {
                return VIEW_TYPE_NEWS;
            }
        } else {
            if (position == 0) {
                return VIEW_TYPE_TIME;
            } else {
                return VIEW_TYPE_NEWS;
            }
        }
    }

    public void setDailyData(RiBao data) {
        dataDate = data.getDate();

        List<RiBao.TopStoriesBean> top_stories = data.getTop_stories();
        bannerList.clear();
        bannerList.addAll(top_stories);
        
        List<RiBao.StoriesBean> stories = data.getStories();
        newsList.clear();
        newsList.addAll(stories);
        notifyDataSetChanged();
    }


    class BannerViewHolder extends RecyclerView.ViewHolder {
        private Banner banner;

        public BannerViewHolder(View itemView) {
            super(itemView);
            banner = itemView.findViewById(R.id.banner);
        }
    }

    class TimeViewHolder extends RecyclerView.ViewHolder {
        private TextView txt;

        public TimeViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.text);
        }
    }

    class NewsViewHolder extends RecyclerView.ViewHolder {

        private TextView name;

        private ImageView img;

        public NewsViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);

            img = itemView.findViewById(R.id.img);
        }
    }

    public OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener{
        void OnClick(int po);
    }
}

第二种一个集合

package com.jiyun.recyclerview3;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

/**
 * Created by $lzj on 2018/12/27.
 */
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ArrayList<ResultDemo.DataBean.DatasBean> list;

    //构造方法传递
    public MyAdapter(Context context, ArrayList<ResultDemo.DataBean.DatasBean> list) {
        this.context = context;
        this.list = list;
    }

    //设置数据源
    public void setList(ArrayList<ResultDemo.DataBean.DatasBean> list) {
        this.list = list;
    }

    //创建显示视图
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {

        /**
         * 根据不同type类型、创建不同布局ViewHolder
         */
        RecyclerView.ViewHolder viewHolder = null;//抽出父类统一返回
        if (type == 100){//显示第一种布局
            View inflate = LayoutInflater.from(context).inflate(R.layout.layout_item1, null);
            viewHolder = new ViewHolderA(inflate);
        }else{//显示第二种布局
            View inflate = LayoutInflater.from(context).inflate(R.layout.layout_item2, null);
            viewHolder = new ViewHolderB(inflate);
        }
        return viewHolder;//返回统一
    }

    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {

        //获取数据源
        ResultDemo.DataBean.DatasBean datasBean = list.get(position);

        /**
         * 根据不同的viewHolder显示不同的数据操作
         */
        if (viewHolder instanceof  ViewHolderA){//判断viewHolder是否为ViewHolderA类型
            ViewHolderA viewHolderA = (ViewHolderA) viewHolder;//将父类viewHolder强转为子类ViewHolderA

            //数据绑定显示
            viewHolderA.titleA.setText(datasBean.getTitle());
            viewHolderA.sourceA.setText(datasBean.getChapterName());
            Glide.with(context).load(datasBean.getEnvelopePic()).into(viewHolderA.imgA);
        }else if (viewHolder instanceof  ViewHolderB){//判断viewHolder是否为ViewHolderB类型
            ViewHolderB viewHolderB = (ViewHolderB) viewHolder;//将父类viewHolder强转为子类ViewHolderB

            //数据绑定显示
            viewHolderB.titleB.setText(datasBean.getTitle());
            viewHolderB.sourceB.setText(datasBean.getChapterName());
            Glide.with(context).load(datasBean.getEnvelopePic()).into(viewHolderB.imgB);
        }

        /**
         * 3.实现点击事件的回调处理
         */
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener !=null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });

        //长按监听、返回对应的position位置
        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //1.通过接口回调,把position传给页面使用
                if (onItemClickListener !=null){
                    onItemClickListener.onLongItemClick(position);
                }
                //2.默认设置不响应
                return false;
            }
        });
    }

    //返回集合长度
    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     *  根据position等不同规则,返回不同的类型值
     *
     *      本案例使用奇偶数划分
     */
    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0){
            return 100;
        }else{
            return 200;
        }
    }

    //布局辅助类ViewHolderA
    class ViewHolderA extends RecyclerView.ViewHolder{

        private ImageView imgA;
        private TextView titleA;
        private TextView sourceA;
        public ViewHolderA(@NonNull View itemView) {
            super(itemView);
            imgA = itemView.findViewById(R.id.img);
            titleA = itemView.findViewById(R.id.title);
            sourceA = itemView.findViewById(R.id.source);
        }
    }

    //布局辅助类ViewHolderB
    class ViewHolderB extends RecyclerView.ViewHolder{
        private ImageView imgB;
        private TextView titleB;
        private TextView sourceB;
        public ViewHolderB(@NonNull View itemView) {
            super(itemView);
            imgB = itemView.findViewById(R.id.img);
            titleB = itemView.findViewById(R.id.title);
            sourceB = itemView.findViewById(R.id.source);
        }
    }

    /**
     * 1.定义接口,提供成员变量、实现公开的set方法
     */
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
        void onLongItemClick(int position);
    }
}

3.第三种

package com.example.zhao.wanandroid2.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhao.wanandroid2.R;
import com.example.zhao.wanandroid2.bean.Zhengwen;
import com.example.zhao.wanandroid2.bean.Zibotu;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

public class MyAdapterB extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context context;
    private ArrayList<Zibotu.DataBean> listA;
    private ArrayList<Zhengwen.DataBean.DatasBean> listB;

    public MyAdapterB(Context context, ArrayList<Zibotu.DataBean> listA, ArrayList<Zhengwen.DataBean.DatasBean> listB) {
        this.context = context;
        this.listA = listA;
        this.listB = listB;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if(viewType == 0){
            View inflate = LayoutInflater.from(context).inflate(R.layout.zibotu, null);
            viewHolder = new Aviewhoader(inflate);
        }
        if(viewType == 1){
            View inflate = LayoutInflater.from(context).inflate(R.layout.wenzhang, null);
            viewHolder = new Bviewhoader(inflate);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof  Aviewhoader){
            Aviewhoader aviewhoader = (Aviewhoader) holder;
            aviewhoader.banner.setImages(listA);
            aviewhoader.banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Zibotu.DataBean dataBean= (Zibotu.DataBean) path;
                    Glide.with(context).load(dataBean.getImagePath()).into(imageView);
                }
            });
            ((Aviewhoader) holder).banner.start();
        }

        if(holder instanceof Bviewhoader){
         int a=0;
            if(position > 0){
                a=position-1;
            }
            final Bviewhoader bviewhoader = (Bviewhoader) holder;

            final Zhengwen.DataBean.DatasBean datasBean = listB.get(a);

            bviewhoader.tv_name.setText(datasBean.getAuthor());
            bviewhoader.tv_info.setText(datasBean.getSuperChapterName());
            bviewhoader.tv_content.setText(datasBean.getTitle());
            bviewhoader.tv_time.setText(datasBean.getNiceDate());

            bviewhoader.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(aa !=null){
                        aa.BB(datasBean);
                    }
                }
            });
        }


    }


    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return 0;
        }else {
            return 1;
        }
    }
    @Override
    public int getItemCount() {
        if(listA.size()>0){
            return listB.size()+1;
        }else {
            return  listB.size();
        }

    }


    private class Aviewhoader extends RecyclerView.ViewHolder {

        private Banner banner;

        public Aviewhoader(View itemView) {
            super(itemView);
            banner = itemView.findViewById(R.id.br);

        }
    }

    private class Bviewhoader extends RecyclerView.ViewHolder {

        private TextView tv_name;
        private TextView tv_info;
        private TextView tv_content;
        private TextView tv_time;

        public Bviewhoader(View itemView) {
            super(itemView);

            tv_name = itemView.findViewById(R.id.tv_name);
            tv_info = itemView.findViewById(R.id.tv_info);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_time = itemView.findViewById(R.id.tv_time);

        }
    }

    private AA aa;
    public void setAa(AA aa) {
        this.aa = aa;
    }
    public interface AA{
        void BB(Zhengwen.DataBean.DatasBean getlink);
    }


}

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

推荐阅读更多精彩内容