ImageAdapter.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package com.hdl.xl.ui.adapter;
  2. import android.view.ViewGroup;
  3. import android.widget.ImageView;
  4. import androidx.annotation.NonNull;
  5. import androidx.recyclerview.widget.RecyclerView;
  6. import com.bumptech.glide.Glide;
  7. import com.hdl.xl.bean.BannerBean;
  8. import com.youth.banner.adapter.BannerAdapter;
  9. import java.util.List;
  10. /**
  11. * 自定义布局,下面是常见的图片样式,更多实现可以看demo,可以自己随意发挥
  12. */
  13. public class ImageAdapter extends BannerAdapter<BannerBean, ImageAdapter.BannerViewHolder> {
  14. public ImageAdapter(List<BannerBean> mDatas) {
  15. //设置数据,也可以调用banner提供的方法,或者自己在adapter中实现
  16. super(mDatas);
  17. }
  18. //创建ViewHolder,可以用viewType这个字段来区分不同的ViewHolder
  19. @Override
  20. public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
  21. ImageView imageView = new ImageView(parent.getContext());
  22. //注意,必须设置为match_parent,这个是viewpager2强制要求的
  23. imageView.setLayoutParams(new ViewGroup.LayoutParams(
  24. ViewGroup.LayoutParams.MATCH_PARENT,
  25. ViewGroup.LayoutParams.MATCH_PARENT));
  26. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  27. return new BannerViewHolder(imageView);
  28. }
  29. @Override
  30. public void onBindViewHolder(@NonNull BannerViewHolder holder, int position, @NonNull List<Object> payloads) {
  31. super.onBindViewHolder(holder, position, payloads);
  32. }
  33. @Override
  34. public void onBindView(BannerViewHolder holder, BannerBean data, int position, int size) {
  35. Glide.with(holder.imageView.getContext()).load(data.getCover()).into(holder.imageView);
  36. }
  37. class BannerViewHolder extends RecyclerView.ViewHolder {
  38. ImageView imageView;
  39. public BannerViewHolder(@NonNull ImageView view) {
  40. super(view);
  41. this.imageView = view;
  42. }
  43. }
  44. }