第一种 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);
}
}