banner适配器封装

这个博客介绍了如何创建一个自定义的BannerAdapter,用于显示和处理轮播图。Adapter中包含了图片路径、标题的集合,并设置了图片加载器、轮播动画、间隔时间和指示器位置。还实现了点击轮播图时的监听事件,通过EventBus传递数据并跳转到详情页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class BannerAdapter implements OnBannerListener {
    //banner
    public Banner banner;
    //图片路径的集合
    List<MyGson_Home.RetEntity.ListEntity.ChildListEntity> childList;
    //图片地址的集合
    List<String> list_path = new ArrayList<>();
    //标题集合
    List<String> list_title = new ArrayList<>();

    public BannerAdapter(Banner banner, List<MyGson_Home.RetEntity.ListEntity.ChildListEntity> childList) {
        this.banner = banner;
        this.childList = childList;
        for (int i = 0; i < childList.size(); i++) {
            list_path.add(childList.get(i).getPic());
            list_title.add(childList.get(i).getTitle());
        }
    }

    public void setbanner() {
        //设置内置样式,共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CENTER);
        //设置图片加载器,图片加载器在下方
        banner.setImageLoader(new MyLoader());
        //设置图片网址或地址的集合
        banner.setImages(list_path);
        //设置轮播的动画效果,内含多种特效,可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播图的标题集合
        banner.setBannerTitles(list_title);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播,默认是“是”。
        banner.isAutoPlay(true);
        //设置指示器的位置,小点点,左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //以上内容都可写成链式布局,这是轮播图的监听。比较重要。方法在下面。
                .setOnBannerListener(this)
                //必须最后调用的方法,启动轮播图。
                .start();


    }

    //轮播图的监听方法
    @Override
    public void OnBannerClick(int position) {
        Log.i("tag", "你点了第" + position + "张轮播图");
        String dataId = childList.get(position).getDataId();
        //eventbus传值
        EventBus.getDefault().postSticky(new MyMessage(dataId));
        //跳转
        Intent intent = new Intent(context, DetailActivity.class);
        intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }

    //自定义的图片加载器
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load((String) path).into(imageView);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值