效果圖展示
這個(gè)是RecyclerView的多布局效果逊彭。用的是輪播圖和加載網(wǎng)絡(luò)數(shù)據(jù)做的效果拆撼,下面是代碼案例:
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;
//如果條目的類(lèi)型等于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;//強(qiáng)轉(zhuǎn)成子類(lèi)
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;
}
});
}
}
//例:文章列表默認(rèn)加載20條數(shù)據(jù),現(xiàn)在我們布局中又多了一個(gè)控件,現(xiàn)在的長(zhǎng)度就變成了21,所以需要考慮輪播圖加載或沒(méi)加載
@Override
public int getItemCount() {
if (bannerList.size() > 0) {
return articleList.size() + 1;
} else {
return articleList.size();
}
}
//根據(jù)條件返回條目的類(lèi)型
@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);
}
}
}