AdapterViewFlipper 图片_文字 轮播动画控件,2021年京东Android岗面试必问

private var textFlipper: AdapterViewFlipper? = null
private var imgFlipper: AdapterViewFlipper? = null
private var preBtn: Button? = null
private var nextBtn: Button? = null
private var autoBtn: Button? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_flipper_anim)
initTextFlipper()
initImgFlipper()
}

// 文字轮播
private fun initTextFlipper() {
textFlipper = findViewById(R.id.text_flipper)
val list = listOf(“文字轮播测试0”, “文字轮播测试02…”)
textFlipper?.adapter = TextFlipperAdapter(this, list)
textFlipper?.setInAnimation(this, R.animator.text_flipper_in_from_bottom)
textFlipper?.setOutAnimation(this, R.animator.text_flipper_out_to_top)
// textFlipper?.flipInterval
// textFlipper?.startFlipping()
}

// 图片轮播
private fun initImgFlipper() {
imgFlipper = findViewById(R.id.img_flipper)
val list = listOf(“http://www.nicesoso.com/test/file/img/test.jpg”, “http://www.nicesoso.com/test/file/img/test_h_1.jpg”,
“http://www.nicesoso.com/test/file/img/test_h_2.jpg”)
imgFlipper?.adapter = ImgFlipperAdapter(this, list)
imgFlipper?.setInAnimation(this, R.animator.img_flipper_in)
preBtn = findViewById(R.id.prev_btn)
nextBtn = findViewById(R.id.next_btn) as Button
autoBtn = findViewById(R.id.auto_btn) as Button

preBtn?.setOnClickListener(this)
nextBtn?.setOnClickListener(this)
autoBtn?.setOnClickListener(this)
}

override fun onClick(v: View?) {
when (v?.id) {
R.id.prev_btn -> {
imgFlipper?.showPrevious()
imgFlipper?.stopFlipping()
}
R.id.next_btn -> {
imgFlipper?.showNext()
imgFlipper?.stopFlipping()
}
R.id.auto_btn -> {
imgFlipper?.startFlipping()
}
}
}

override fun onDestroy() {
super.onDestroy()
textFlipper?.takeIf { it.isFlipping }?.stopFlippi

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

ng()
imgFlipper?.takeIf { it.isFlipping }?.stopFlipping()
}
}

3.1 文字轮播:TextFlipperAdapter

class TextFlipperAdapter(private val context: Context, private val datas: List) : BaseAdapter() {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = convertView ?: LayoutInflater.from(context).inflate(R.layout.item_flipper_text, parent, false)
val textView = view?.findViewById<TextView?>(R.id.text)
textView?.text = datas.get(position)
return view
}

override fun getItem(position: Int): Any {
return datas.get(position)
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

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

3.2 图片轮播:ImgFlipperAdapter

class ImgFlipperAdapter(private val context: Context, private val datas: List) : BaseAdapter() {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = convertView ?: ImageView(context)
(view as? ImageView)?.scaleType = ImageView.ScaleType.FIT_XY
view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
(view as? ImageView)?.let { Glide.with(context).load(datas.get(position)).into(it) }

return view
}

override fun getItem(position: Int): Any {
return datas.get(position)
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

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

3.3 布局:activity_flipper_anim.xml

<?xml version="1.0" encoding="utf-8"?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值