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"?>