Look at the effect

Realize the function

class BannerSetting(var loop: Boolean = falseVar canSlideByTouch: Boolean =trueVar canAutoSlide: Boolean =trueVar autoSlideSpeed: Int = 500 var autoSlideSpeed: Int = 500Copy the code
  1. Sliding has a scaling effect

use

Add the dependent

 implementation 'com. Making. ZhangHao555: BannerRecyclerView: 1.1.2 @ aar'
Copy the code

Use similar to RecyclerView set up a layoutManager and an Adapter can be used

/ / add layout < androidx. Constraintlayout. Widget. Constraintlayout android: layout_width ="match_parent"
        android:layout_height="wrap_content">

        <com.ahao.bannerview.BannerView
            android:id="@+id/banner_view"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:layout_marginTop="10dp"
            app:layout_constraintTop_toTopOf="parent" />

        <com.ahao.bannerview.BannerIndicator
            android:id="@+id/banner_indicator"
            android:layout_width="25dp"
            android:layout_height="10dp"
            android:layout_marginBottom="5dp"
            app:layout_constraintBottom_toBottomOf="@id/banner_view"
            app:layout_constraintEnd_toEndOf="@id/banner_view"
            app:layout_constraintStart_toStartOf="@id/banner_view" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    
Copy the code
// Set LayoutManager, Banner_view.layoutmanager = ScaleBannerLayoutManager() // The indicator can not be set banner_indicator.adapter = object : BannerIndicator.Adapter() {

            override fun getItemCount(): Int = data.size

            override fun getUnselectedView(context: Context) = CircleView(context, null).apply {
                radius = dp2px(context, 2.5f)
                color = Color.parseColor("#E6E6E6")
            }

            override fun getSelectedView(context: Context) = CircleView(context, null).apply {
                radius = dp2px(context, 2.5f)
                color = Color.parseColor("#FF00CEAA"Indicator = banner_indicator // Set the slider parameter banner_view.setup (BannerSetting().apply { SlideTimeGap = 3000 // autoSlideSpeed = 1000 // Time loop =true// canAutoSlide =true// Whether to allow manual sliding}, Adapter()) // Adapter is a common RecyclerView Adapter inner class Adapter: RecyclerView.Adapter<ViewHolder>() { override fun onCreateViewHolder(group: ViewGroup, position: Int): ViewHolder { val item = LayoutInflater.from(this@BannerViewActivity).inflate(R.layout.view_banner_item_view, group,false)
            returnViewHolder(item) } override fun getItemCount() = data.size override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.cardItem.layoutParams? Width = (getDisplayMetrics (this @ BannerViewActivity). WidthPixels. Times (0.85 f)), toInt () Glide.with(this@BannerViewActivity).load(data[position]).into(holder.image) } }Copy the code

Github address: github.com/ZhangHao555…