android 上下轮播,广播 BulletinView

上下滚动组件比较少,而且使用方面也不是很灵活,这边就自己写了个,这边取了个巧 简单使用了一个动画就实现了上下滚动,仿照recyclerView 支持每个条目自定义

1.首先准备一个容器 LinearLayout

2. 容器内准备2个View 从adapter 创建并绑定

3.将view 添加到容器 并根据容器设置 layoutParams

4.给view 添加上下移动的平移动画

5.启动线程进行定时轮播 轮播前移除所有View 加入新View,在执行动画,就完美实现了上下无线轮播效果

kotlin 复制代码
class BulletinView(
    context: Context,
    attrs: AttributeSet?,
    defStyleAttr: Int,
    defStyleRes: Int
) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    constructor(context: Context) : this(context, null, 0, 0)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )
    //适配器
    var adapter: Adapter<ViewHolder>? = null
        set(value) {
            field = value
            if (onPause==2){
                onPause = 0
                handler.post(run)
            }
            onPause = 0
        }
    //当前位置
    private var position = 0

    /**
     * 0 未暂停 1标识要暂停 2 已暂停
     */
    private var onPause = 2
    private val handler = Handler(Looper.getMainLooper())
    private val run = Runnable {
        initView()
        next()
    }

    init { //设置排版 方向
        orientation = VERTICAL
    }
    //下一条
    private fun next() {
        if (onPause == 1) onPause = 2
        if (onPause == 0) {
            handler.postDelayed(run, 3000)
        }
    }

    //设置View
    private fun initView() {
        if (getPosition(++position) == 0) {
            position = 0
        }
        adapter?.let {
            val holder1 = it.bindingViewHolder(
                it.getViewType(getPosition(position)),
                this,
                getPosition(position)
            )
            val holder2 = it.bindingViewHolder(
                it.getViewType(getPosition(position + 1)),
                this,
                getPosition(position + 1)
            )
            it.bindingView(holder1, it.getViewType(getPosition(position)), getPosition(position))
            it.bindingView(
                holder2,
                it.getViewType(getPosition(position + 1)),
                getPosition(position + 1)
            )
            removeAllViews()
            addView(holder1.itemView)
            addView(holder2.itemView)
            holder1.itemView.layoutParams = LayoutParams(layoutParams)
            holder2.itemView.layoutParams = LayoutParams(layoutParams)
            setAnimate(holder1.itemView)
            setAnimate(holder2.itemView)
        }
    }
    //获取下标 防止越界
    private fun getPosition(position: Int): Int {
        return if (position >= (adapter?.getCount() ?: 0)) {
            0
        } else {
            position
        }
    }

    //适配器
    interface Adapter<T : ViewHolder> {
        fun bindingViewHolder(viewType: Int, group: ViewGroup, position: Int): T
        fun getViewType(viewType: Int): Int
        fun bindingView(holder: T, viewType: Int, position: Int)
        fun getCount(): Int
    }

    /**
     * 属性动画
     * 平移
     * 从下往上
     */
    private fun setAnimate(view: View): TranslateAnimation {
//      imageView中凡是有get,set方法的属性,都可以通过属性动画操作
//      创建属性动画对象,并设置移动的方向和偏移量
//      translationX是imageview的平移属性
        val objectAnimator = TranslateAnimation(0f, 0f, 0f, -height.toFloat())
//      设置移动时间
        objectAnimator.duration = 500
//      开始动画
        objectAnimator.fillAfter = true
        view.animation = objectAnimator
        objectAnimator.start()
        return objectAnimator
    }
    //暂停
    fun onPause(){
        onPause = 1
    }
    //启动
    fun onStart(){
        if (onPause==2){
            onPause = 0
            handler.post(run)
        }
        onPause = 0
    }

}

使用方法

kotlin 复制代码
 fun setBulletin(){
        val list = mutableListOf("https://lmg.jj20.com/up/allimg/1114/0406210Z024/2104060Z024-5-1200.jpg","https://lmg.jj20.com/up/allimg/1114/0406210Z024/2104060Z024-5-1200.jpg")
        binding.bulletinView.adapter = object : Adapter<ViewHolder> {
            override fun bindingViewHolder(
                viewType: Int,
                group: ViewGroup,
                position: Int
            ): ViewHolder { //这边绑定ViewHolder
               return ViewHolder (
                   BaseBannerBinding.inflate(
                       LayoutInflater.from(
                           this@MainActivity
                       ), group, false
                   )
               )
            }

            override fun getViewType(viewType: Int): Int {
               return 0
            }

            override fun getCount(): Int {
                return list.size
            }

            override fun bindingView(holder: ViewHolder, viewType: Int, position: Int) {
                val binding: BaseBannerBinding = BaseBannerBinding.bind(holder.itemView)
                binding.ivIcon.load(list[position])
            }


        }
    }
相关推荐
好的佩奇5 小时前
Dart 之元数据与注解
android·flutter·dart
三少爷的鞋6 小时前
很多开发者把物理上的分层,当成了架构
android
FungLeo7 小时前
FengCMS · PHP+MySQL 升级实录 · MySQL DDL不能回滚:模型字段结构操作的失败续做
android·mysql·php·工程实践·fengcms
雨白8 小时前
Android 建造者模式实战:从 AlertDialog 源码到 TopBar 的两种实现
android
_祝你今天愉快11 小时前
Android 高级绘制:[五] 贝塞尔曲线与计算规则
android
FungLeo1 天前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks1 天前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui1 天前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
雨白2 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android2 天前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin