package cn.flightfeather.thirdappmodule.view
|
|
import android.annotation.SuppressLint
|
import android.content.Context
|
import android.graphics.Color
|
import android.support.v4.content.ContextCompat
|
import android.support.v4.view.ViewPager
|
import android.util.AttributeSet
|
import android.view.Gravity
|
import android.view.View
|
import android.view.ViewGroup
|
import android.widget.LinearLayout
|
import android.widget.RelativeLayout
|
import android.widget.TextView
|
import cn.flightfeather.thirdappmodule.R
|
import cn.flightfeather.thirdappmodule.util.dp
|
import io.reactivex.Observable
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.schedulers.Schedulers
|
import org.jetbrains.anko.alignParentBottom
|
import org.jetbrains.anko.alignParentEnd
|
import org.jetbrains.anko.backgroundResource
|
|
|
/**
|
* @author riku
|
* Date: 2019/5/20
|
* 轮播ViewPager
|
*/
|
class BannerView : RelativeLayout {
|
|
constructor(context: Context) : super(context){
|
mContext = context
|
}
|
|
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
|
mContext = context
|
}
|
|
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr){
|
mContext = context
|
}
|
|
|
private var mContext: Context? = null
|
private val viewPager = MyViewPager(context)
|
private val dots = LinearLayout(context)
|
private val index = LinearLayout(context)
|
private var currentIndex: TextView? = null
|
private var curPos = 0
|
private var lastPos = 0
|
var isAlive = true
|
set(value) {
|
field = value
|
if (value) initObservable()
|
}
|
|
var dataList = listOf<View>()
|
set(value) {
|
field = value
|
initViewPager()
|
initDots()
|
initIndex()
|
// initObservable()
|
}
|
|
fun refresh() {
|
viewPager.adapter?.notifyDataSetChanged()
|
}
|
|
private fun initObservable() {
|
Observable.create<Boolean> {
|
while (isAlive) {
|
Thread.sleep(3000)
|
it.onNext(true)
|
}
|
it.onComplete()
|
}.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnNext {
|
if (curPos == dataList.size - 1) {
|
curPos = 0
|
viewPager.setCurrentItem(0, false)
|
} else {
|
curPos++
|
viewPager.setCurrentItem(curPos, true)
|
}
|
}.subscribe()
|
}
|
|
private fun initViewPager(){
|
val viewPagerParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
|
viewPager.adapter = ViewPagerAdapter(dataList)
|
viewPager.currentItem = 0
|
viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
override fun onPageScrollStateChanged(p0: Int) {
|
|
}
|
|
override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
|
|
}
|
|
@SuppressLint("SetTextI18n")
|
override fun onPageSelected(p0: Int) {
|
val newPos = p0 % dataList.size
|
dots.getChildAt(lastPos).isEnabled = false
|
dots.getChildAt(newPos).isEnabled = true
|
lastPos = newPos
|
|
(index.getChildAt(0) as TextView).text = "${p0 + 1}"
|
}
|
|
})
|
addView(viewPager, viewPagerParams)
|
}
|
|
private fun initDots() {
|
val dotsParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
dotsParams.alignParentBottom()
|
|
dots.apply {
|
setPadding(4.dp, 4.dp, 4.dp, 4.dp)
|
orientation = LinearLayout.HORIZONTAL
|
gravity = Gravity.CENTER
|
}.layoutParams = dotsParams
|
repeat(dataList.size) {
|
val dot = View(context)
|
dot.backgroundResource = R.drawable.point_background
|
val params = LinearLayout.LayoutParams(8.dp, 8.dp)
|
params.leftMargin = 4.dp
|
dot.layoutParams = params
|
dot.isEnabled = false
|
dots.addView(dot)
|
}
|
if (dataList.isNotEmpty()) {
|
dots.getChildAt(0).isEnabled = true
|
}
|
|
addView(dots)
|
}
|
|
private fun initIndex() {
|
val indexParams = LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
indexParams.alignParentBottom()
|
indexParams.alignParentEnd()
|
|
index.apply {
|
setPadding(4.dp, 4.dp, 4.dp, 4.dp)
|
setBackgroundColor(ContextCompat.getColor(context, R.color.gray))
|
orientation = LinearLayout.HORIZONTAL
|
gravity = Gravity.CENTER
|
}.layoutParams = indexParams
|
|
val textParams = LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
|
currentIndex = TextView(context).apply {
|
text = "1"
|
setTextColor(Color.WHITE)
|
}
|
val divider = TextView(context).apply {
|
text = "/"
|
setTextColor(Color.WHITE)
|
}
|
val totalIndex = TextView(context).apply {
|
text = dataList.size.toString()
|
setTextColor(Color.WHITE)
|
}
|
index.addView(currentIndex, textParams)
|
index.addView(divider, textParams)
|
index.addView(totalIndex, textParams)
|
|
addView(index)
|
}
|
|
}
|