123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package com.hdl.xl.ui.adapter;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import androidx.annotation.NonNull;
- import androidx.recyclerview.widget.RecyclerView;
- import com.bumptech.glide.Glide;
- import com.hdl.xl.bean.BannerBean;
- import com.youth.banner.adapter.BannerAdapter;
- import java.util.List;
- /**
- * 自定义布局,下面是常见的图片样式,更多实现可以看demo,可以自己随意发挥
- */
- public class ImageAdapter extends BannerAdapter<BannerBean, ImageAdapter.BannerViewHolder> {
- public ImageAdapter(List<BannerBean> mDatas) {
- //设置数据,也可以调用banner提供的方法,或者自己在adapter中实现
- super(mDatas);
- }
- //创建ViewHolder,可以用viewType这个字段来区分不同的ViewHolder
- @Override
- public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
- ImageView imageView = new ImageView(parent.getContext());
- //注意,必须设置为match_parent,这个是viewpager2强制要求的
- imageView.setLayoutParams(new ViewGroup.LayoutParams(
- ViewGroup.LayoutParams.MATCH_PARENT,
- ViewGroup.LayoutParams.MATCH_PARENT));
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- return new BannerViewHolder(imageView);
- }
- @Override
- public void onBindViewHolder(@NonNull BannerViewHolder holder, int position, @NonNull List<Object> payloads) {
- super.onBindViewHolder(holder, position, payloads);
- }
- @Override
- public void onBindView(BannerViewHolder holder, BannerBean data, int position, int size) {
- Glide.with(holder.imageView.getContext()).load(data.getCover()).into(holder.imageView);
- }
- class BannerViewHolder extends RecyclerView.ViewHolder {
- ImageView imageView;
- public BannerViewHolder(@NonNull ImageView view) {
- super(view);
- this.imageView = view;
- }
- }
- }
|