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])
}
}
}