Use ConvenientBanner to implement carousel graph in Kotlin project

Foreword: Banner (carousel) is no stranger to Android developers. Almost all APPs on the market integrate the function of banner, and our company's projects are no exception. We are no strangers to kotlin. Since Google announced at the 2017 IO conference that Kotlin was officially used as the official language for Android development, more and more projects have been developed using kotlin. This article just records my use of the ConvenientBanner in the kotlin project.

1. Project integration

Add the dependency of the convenientBanner under the dependencies of build.gradle (Model:app)

implementation 'com.bigkoo:convenientbanner:2.0.5'

2. Layout usage

<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. Code implementation

(1) Define banner

private lateinit was mBanner : ConvenientBanner <Int>
Note: There are some differences between this and java, kotlin needs to have parameter types

java definition:ConvenientBanner mBanner

kotlin definition: var mBanner:ConvenientBanner<Int> >>>>int loads the image resources under the local res

(2) Initialization and setting
//Carousel image
 mBanner = headView .findViewById(R.id. convenient_banner )
 mBanner
         .setPages( { BannerImageHolderView() } , DataUtil.getBannerList())
         //Set two point images as page turning indicators, if not set, there is no Indicator ,
         you can match your own indicator according to your own needs. You don't need a dot
         indicator . You can use it without setting
 .         (ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL ) //
         Set the rotation interval.startTurning
         ( 2000 )

Note: In kotlin, you can directly set parameters for the control (such as: tv_message.text="test"), but here you need to get the banner first through findById(id: Int), and then set the parameters, otherwise an error will be reported.


4. Create BannerImageViewHolder

/**
 * Created by ruancw on 18/4/24.
 * Local image loading example
 */
 class BannerImageHolderView : Holder<Int> {
     private var imageView : ImageView? = null
     override fun createView (context: Context): View {
         // Here you can create any layout you want according to your needs, not necessarily the imageView control
 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!!)
    }
}
Postscript: convenientBanner is a very good carousel control. It has been used in the project and is relatively simple to use. The following is the link address of the convenientBanner on github: https://github.com/saiwu-bigkoo/Android-ConvenientBanner

At this point, the use of convenientBanner in kotlin is over, and corrections are welcome!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324896285&siteId=291194637