Kotlin項(xiàng)目中使用ConvenientBanner實(shí)現(xiàn)輪播圖

前言:Banner(輪播圖),對(duì)于Android開發(fā)者來說并不陌生,市面上的APP基本都集成了banner這一功能,我們公司的項(xiàng)目也不例外。kotlin,我們也不陌生了,隨著Google 在2017 IO大會(huì)上宣布Kotlin正式被作為Android開發(fā)的官方語言以來,使用kotlin開發(fā)的項(xiàng)目也逐漸多了起來。本文只是記錄下本人在kotlin項(xiàng)目中的ConvenientBanner的使用。

1.項(xiàng)目集成

在build.gradle(Model:app)的dependencies下添加convenientBanner的依賴

implementation 'com.bigkoo:convenientbanner:2.0.5'

2.布局使用

<com.bigkoo.convenientbanner.ConvenientBanner
  android:id="@+id/convenient_banner"
  android:layout_width="match_parent"
  android:layout_height="200dp"
  android:src="@mipmap/ic_launcher"
  app:canLoop="true">   
</com.bigkoo.convenientbanner.ConvenientBanner>

3.代碼實(shí)現(xiàn)

(1)定義banner

private lateinit var mBanner:ConvenientBanner<Int>

注:此處和java有些區(qū)別,kotlin需要帶參數(shù)類型

java定義:ConvenientBanner mBanner

kotlin定義:var mBanner:ConvenientBanner<Int> >>>>int加載本地的res下的圖片資源

(2)初始化及設(shè)置

//輪播圖 
mBanner=headView.findViewById(R.id.convenient_banner)
mBanner
    .setPages( { BannerImageHolderView() }, DataUtil.getBannerList())
    //設(shè)置兩個(gè)點(diǎn)圖片作為翻頁指示器,不設(shè)置則沒有指示器,可以根據(jù)自己需求自行配合自己的指示器,不需要圓點(diǎn)指示器可用不設(shè)
    .setPageIndicator(intArrayOf(R.drawable.banner_point_select, R.drawable.banner_point_normal))
    //設(shè)置指示器的方向
    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
    //設(shè)置輪播時(shí)間間隔
    .startTurning(2000)

注:kotlin中可以直接用對(duì)控件進(jìn)行設(shè)置參數(shù)(如:tv_message.text="測(cè)試"),但此處需通過findViewById(id:Int)先獲取bannner,再進(jìn)行設(shè)置參數(shù),不然會(huì)報(bào)錯(cuò)。

image

4.創(chuàng)建BannerImageViewHolder

/**
** Created by ruancw on 18/4/24.
** 本地圖片加載例子* 
**/
class BannerImageHolderView : Holder<Int> {
    private var imageView: ImageView? = null
    override fun createView(context: Context): View {
        //此處可以根據(jù)需求創(chuàng)建任何你想要的布局,不一定是imageView控件
        imageView = ImageView(context)
        imageView!!.*scaleType* = ImageView.ScaleType.FIT_XY
        return imageView as ImageView
    }

    override fun UpdateUI(context: Context, position: Int, data: Int) {
        imageView!!.setImageResource(data)
        //Glide.with(context).load(data).into(imageView!!)
  }
}

后記:convenientBanner是一個(gè)很不錯(cuò)的輪播圖控件,項(xiàng)目中一直有在使用,使用也比較簡(jiǎn)單,下面是github上的convenientBanner的鏈接地址:https://github.com/saiwu-bigkoo/Android-ConvenientBanner

至此,convenientBanner在kotlin中的使用就結(jié)束了,不足之處歡迎指正!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容