效果图展示
这个是RecyclerView的多布局效果。用的是轮播图和加载网络数据做的效果,下面是代码案例:
public class MyListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ArrayList<BannerBean.DataBean> bannerList;
private ArrayList<NewslistBean> articleList;
public MyAdapter(Context context, ArrayList<BannerBean.DataBean> bannerList, ArrayList<NewslistBean> articleList) {
this.context = context;
this.bannerList = bannerList;
this.articleList = articleList;
}
public void setBannerList(ArrayList<BannerBean.DataBean> bannerList) {
this.bannerList = bannerList;
}
public void setArticleList(ArrayList<NewslistBean> articleList) {
this.articleList = articleList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
RecyclerView.ViewHolder holder = null;
//如果条目的类型等于1,加载轮播图,否则加载文章列表
if (i == 1) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_banner, null);
holder = new BannnerViewHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.layout_list, null);
holder = new ArticleViewHolder(view);
}
return holder;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof BannnerViewHolder) {
BannnerViewHolder bannnerViewHolder = (BannnerViewHolder) viewHolder;//强转成子类
bannnerViewHolder.banner.setImages(bannerList);
bannnerViewHolder.banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
BannerBean.DataBean dataBean = (BannerBean.DataBean) path;
Glide.with(context).load(dataBean.getImagePath()).into(imageView);
}
});
bannnerViewHolder.banner.start();
} else if (viewHolder instanceof ArticleViewHolder) {
ArticleViewHolder articleViewHolder = (ArticleViewHolder) viewHolder;
int newPosition = i;
if (bannerList.size() > 0) {
newPosition = i - 1;
}
final NewslistBean newslistBean = articleList.get(newPosition);
articleViewHolder.tv1.setText(newslistBean.getTitle());
RequestOptions options = new RequestOptions().circleCrop();
Glide.with(context).load(newslistBean.getPicUrl()).apply(options).into(articleViewHolder.iv);
articleViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (setOnClickLongListener != null) {
setOnClickLongListener.OnClickListener(newslistBean);
}
return false;
}
});
}
}
//例:文章列表默认加载20条数据,现在我们布局中又多了一个控件,现在的长度就变成了21,所以需要考虑轮播图加载或没加载
@Override
public int getItemCount() {
if (bannerList.size() > 0) {
return articleList.size() + 1;
} else {
return articleList.size();
}
}
//根据条件返回条目的类型
@Override
public int getItemViewType(int position) {
if (position == 0) {
return 1;
} else {
return 2;
}
}
public class BannnerViewHolder extends RecyclerView.ViewHolder {
private Banner banner;
public BannnerViewHolder(@NonNull View itemView) {
super(itemView);
banner = itemView.findViewById(R.id.banner);
}
}
public class ArticleViewHolder extends RecyclerView.ViewHolder {
private ImageView iv;
private TextView tv1;
public ArticleViewHolder(@NonNull View itemView) {
super(itemView);
iv = itemView.findViewById(R.id.iv);
tv1 = itemView.findViewById(R.id.tv1);
}
}
}