里程表式跑马灯自定义控件

效果图

《里程表式跑马灯自定义控件》 20180628.gif

优点:

  • 内容item复用,节约内存
  • 基于ViewGroup,比较轻量
  • 支持recyclerview中使用
  • item布局支持自定义
  • 极端需求(支持嵌套)

缺点:

  • item的布局只能唯一
  • 不能手动开始和结束动画(不是不可以加,只是本人觉得没必要。。)

源码:

https://github.com/nelson1110/MarqueeView

用法:

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

Step 2. Add the dependency

    dependencies {
            implementation 'com.github.nelson1110:MarqueeView:0.1.0-release'
    }

Step 3. 在xml中添加该控件

<com.libs.nelson.marqueeviewlib.MarqueeView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

xml中的一些可用属性

参数名意义
orientation动画滚动方向
animator_duration动画时间
stay_duration每个item动画结束后停留的时间
reverse_animator是否反向动画,默认↑或←

Step 4. 给MarqueeView设置Adapter

marqueeView.setAdapter(object : MarqueeView.MarqueeViewAdapter(){
            override fun getItemLayout(): Int {
                return R.layout.layout
            }
            override fun onBindItemView(itemView: View, position: Int) {
              itemView.findViewById<TextView>(R.id.text).text = position.toString()
            }
            override fun getItemCount(): Int {
                return 4
            }
        })

Adapter中各方法的含义:

方法名意义
getItemLayout决定滚动的内容的布局,当前版本默认内容布局只有一种
onBindItemView给当前position的内容布局绑定数据
getItemCount决定内容item有多少个
    原文作者:坑吭吭
    原文地址: https://www.jianshu.com/p/04ccffbeceee
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞