Giter Club home page Giter Club logo

bannerrecyclerview's Introduction

先看效果

实现功能

class BannerSetting(var loop: Boolean = false, // 是否循坏
        var canSlideByTouch: Boolean = true, // 是否允许手动滑动
        var canAutoSlide: Boolean = true, // 是否允许自动滑动
        var slideTimeGap: Long = 2000,  // 自动滑动时间间隔
        var autoSlideSpeed: Int = 500 // 自动滑动一次的时长

)
  1. 滑动伴有缩放效果

使用方式

添加依赖

  implementation 'com.github.ZhangHao555:BannerRecyclerView:1.1.4'

使用方式类似于RecyclerView 设置一个layoutManager 和一个Adapter即可使用

//添加布局
    <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>
    
  // 设置 LayoutManager, ScaleBannerLayoutManager是在滑动的时候有一个缩放的效果
  banner_view.layoutManager = ScaleBannerLayoutManager()
        // 指示器 可以不设置
        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")
            }
        }
         // 指示器 可以不设置
        banner_view.indicator = banner_indicator
        // 设置滑动参数
        banner_view.setUp(BannerSetting().apply {
            slideTimeGap = 3000  // 自动滑动时间间隔
            autoSlideSpeed = 1000 // 完成滑动一次的时间
            loop = true         // 是否可以循环滑动
            canAutoSlide = true // 是否允许手动滑动
        }, Adapter())

    // Adapter即为普通的RecyclerView的适配器
    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)
            return ViewHolder(item)
        }

        override fun getItemCount() = data.size

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.cardItem.layoutParams?.width = (getDisplayMetrics(this@BannerViewActivity).widthPixels.times(0.85f)).toInt()
            Glide.with(this@BannerViewActivity).load(data[position]).into(holder.image)
        }
    }

github地址 :https://github.com/ZhangHao555/BannerRecyclerView

bannerrecyclerview's People

Contributors

zhanghao555 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bannerrecyclerview's Issues

自动滑动的问题

设置自动滑动后,先停止滑动,在启动的话无法自动滑动。稍微延迟后就可以。猜测是handler信消息队列的启动滑动消息被清空了。

RecycleView嵌套BannerView,奔溃

当父RecyclerView自己的notifyDataSetChanged的时候,子BannerView就会崩溃:
kotlin.KotlinNullPointerException
at com.shwatch.news.banner.BannerLayoutManager.scrollToLeft(BannerLayoutManager.kt:150)
at com.shwatch.news.banner.BannerLayoutManager.offsetDx(BannerLayoutManager.kt:92)
at com.shwatch.news.banner.BannerLayoutManager.scrollHorizontallyBy(BannerLayoutManager.kt:85)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4834)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1092)
at android.view.Choreographer.doCallbacks(Choreographer.java:893)
at android.view.Choreographer.doFrame(Choreographer.java:809)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.