欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Banner基本使用 2.1.0

程序员文章站 2022-07-03 16:50:21
Step 1.依赖bannerGradledependencies{ compile 'com.youth.banner:banner:2.1.0' }Step 2.添加权限到你的 AndroidManifest.xml...

Step 1.依赖banner

Gradle

dependencies{
    compile 'com.youth.banner:banner:2.1.0'  
}

Step 2.添加权限到你的 AndroidManifest.xml

<!-- if you want to load images from the internet -->
<uses-permission android:name="android.permission.INTERNET" /> 

Step 3.在布局文件中添加Banner,可以设置自定义属性

!!!此步骤可以省略,可以直接在Activity或者Fragment中new Banner();       

有时候banner不显示  是因为height要设置一个固定值

<com.youth.banner.Banner
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/banner"
    android:layout_width="match_parent"
    android:layout_height="高度自己设置" />

1.简单的图片展示

public class IMGBean {

    public IMGBean(int imageRes) {
        this.imageRes = imageRes;
    }

    public int imageRes;

    public int getImageRes() {
        return imageRes;
    }

    public void setImageRes(int imageRes) {
        this.imageRes = imageRes;
    }


    public static List<IMGBean> getData() {
        List<IMGBean> list = new ArrayList<>();
        list.add(new IMGBean(R.mipmap.fta_ad1));
        list.add(new IMGBean(R.mipmap.fta_ad2));
        return list;
    }

}
    private void initBanner() {
        mBanner.setAdapter(new BannerImageAdapter<IMGBean>(IMGBean.getData()) {
            @Override
            public void onBindView(BannerImageHolder holder, IMGBean data, int position, int size) {
                //图片加载自己实现
                RoundedCorners roundedCorners = new RoundedCorners(10);
                RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
                Glide.with(holder.itemView).load(data.imageRes).apply(options).into(holder.imageView);
            }
        })
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(getActivity()));
        //更多使用方法仔细阅读文档,或者查看demo
    }

2.网络获取的List<Bean> 数据结合使用  我这里用MMM.BBB代替JavaBean对象

~~1.定义Adapter

public class MyBannerAdapter extends BannerAdapter<MMM.BBB,MyBannerAdapter.BannerViewHolder> {


    public MyBannerAdapter(List<MMM.BBB> datas) {
        super(datas);
    }

    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        ImageView imageView = new ImageView(parent.getContext());
        //注意,必须设置为match_parent,这个是viewpager2强制要求的
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return new BannerViewHolder(imageView);
    }

    @Override
    public void onBindView(BannerViewHolder holder, MMM.BBB data, int position, int size) {
        RoundedCorners roundedCorners = new RoundedCorners(10);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(holder.itemView).load(data.getOguyEkaJ()).apply(options).into(holder.imageView);
//        holder.imageView.setImageResource(data.getOguyEkaJ());
            //设置点击事件
           holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("tttttttt",data.getXODHXxeB()+"");
            }
        });

    }


    class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public BannerViewHolder(@NonNull ImageView view) {
            super(view);
            this.imageView = view;
        }
    }

}

2.设置

     mBanner.setAdapter(new MyBannerAdapter(data))
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(getActivity()));

 

本文地址:https://blog.csdn.net/ulddfhv/article/details/109365907

相关标签: Android