近段时间的学习碎片整理(22)

一、Banner 2.0 ,轮播图的使用

        1、引入依赖

implementation 'io.github.youth5201314:banner:2.2.2'

        2、权限声明

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

        3、布局文件

   <com.youth.banner.Banner
                android:id="@+id/banner_photos"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintDimensionRatio="h,3:4"
                />
            <com.youth.banner.indicator.CircleIndicator
                android:id="@+id/banner_indicator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/banner_photos"
                app:layout_constraintBottom_toTopOf="@id/tv_title"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_margin="12dp"
                />

        4、初始化轮播图        

//测试数据
val imageUrls = arrayListOf(
        "https://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png",
        "https://img.zcool.cn/community/016a2256fb63006ac7257948f83349.jpg",
        "https://img.zcool.cn/community/01233056fb62fe32f875a9447400e1.jpg",
        "https://img.zcool.cn/community/01700557a7f42f0000018c1bd6eb23.jpg"
    )

  /**
     * 初始化轮播图
     */
    private fun initBanner() {
        val dp6 = BannerUtils.dp2px(6f)
        binding.bannerPhotos.apply {
            addBannerLifecycleObserver(this@MoonDetailActivity)
            setAdapter(PhotoAdapter(imageUrls), false)
            isAutoLoop(false)
            setIndicator(binding.bannerIndicator, false)
            setIndicatorSelectedColorRes(R.color.orange)
            setIndicatorNormalColorRes(R.color.gray_efeff4)
            setIndicatorSpace(dp6)
            setIndicatorWidth(dp6, dp6)
            setIndicatorGravity(IndicatorConfig.Direction.CENTER)
        }
    }

        5、轮播图适配器

class PhotoAdapter(photoList: ArrayList<String>) : BannerAdapter<String, PhotoAdapter.PhotoHolder>(photoList) {

    class PhotoHolder(view: View) : RecyclerView.ViewHolder(view) {
        var imageView: ImageView = view as ImageView
    }

    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): PhotoHolder {
        val imageView = ImageView(parent!!.context)
        val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        imageView.layoutParams = params
        return PhotoHolder(imageView)
    }

    override fun onBindView(holder: PhotoHolder, data: String, position: Int, size: Int) {
        GlideApp.with(holder.itemView)
            .load(data)
            .centerCrop()
            .placeholder(R.drawable.ps_image_placeholder)
            .into(holder.imageView)
    }
}

         

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值