package com.dollearn.student.ui.home.adapter
|
|
import android.view.View
|
import cn.sinata.xldutils.adapter.HFRecyclerAdapter
|
import cn.sinata.xldutils.adapter.util.ViewHolder
|
import cn.sinata.xldutils.gone
|
import cn.sinata.xldutils.visible
|
import com.dollearn.student.R
|
import com.dollearn.student.network.entity.Card
|
import com.dollearn.student.ui.home.MemoryActivity
|
import com.dollearn.student.utils.ViewAnimUtils
|
import com.dollearn.student.utils.extention.clickDelay
|
import com.facebook.drawee.view.SimpleDraweeView
|
import kotlinx.android.synthetic.main.fragment_q_a.*
|
import java.util.ArrayList
|
|
class CardAdapter(list: ArrayList<Card>):HFRecyclerAdapter<Card>(list, R.layout.item_card) {
|
private val act by lazy { context as MemoryActivity }
|
|
//第一次翻开的
|
var position1 = -1
|
private var p1View:View? = null
|
private var f1View:View? = null
|
private var b1View:View? = null
|
|
//第2次翻开的
|
var position2 = -1
|
private var p2View:View? = null
|
private var f2View:View? = null
|
private var b2View:View? = null
|
|
override fun onBind(holder: ViewHolder, position: Int, data: Card) {
|
val frame = holder.bind<View>(R.id.frame)
|
val back = holder.bind<View>(R.id.back)
|
val front = holder.bind<View>(R.id.front)
|
val rlVoice = holder.bind<View>(R.id.rl_voice)
|
val ivImg = holder.bind<SimpleDraweeView>(R.id.iv_img)
|
|
if(data.type == 1){
|
rlVoice.gone()
|
ivImg.visible()
|
ivImg.setImageURI(data.url)
|
}else{
|
rlVoice.visible()
|
ivImg.gone()
|
}
|
|
frame.clickDelay {
|
if(data.isBack){
|
if(position1 == -1)
|
position1 = position
|
else if(position2 == -1)
|
position2 = position
|
flip(position,frame,back, front)
|
act.flyover(position)
|
}
|
}
|
if (data.isBack){
|
back.visible()
|
front.gone()
|
}else{
|
back.gone()
|
front.visible()
|
}
|
}
|
|
private fun flip(position: Int,parent:View,back: View,front: View){
|
var direction = 1
|
if (back.isShown) {
|
direction = -1
|
}
|
ViewAnimUtils.flip(parent, 300, direction)
|
parent.postDelayed({
|
mData[position].isBack = direction != -1
|
switchViewVisibility(back, front) },300)
|
if(p1View == null){
|
p1View = parent
|
b1View = back
|
f1View = front
|
}else if(p2View == null){
|
p2View = parent
|
b2View = back
|
f2View = front
|
}
|
}
|
|
/**
|
* 答案不匹配,2张卡片都翻回去
|
*/
|
fun bothToBack(){
|
if (p1View!=null&&b1View!=null&&f1View!=null)
|
flip(position1,p1View!!,b1View!!,f1View!!)
|
if (p2View!=null&&b2View!=null&&f2View!=null)
|
flip(position2,p2View!!,b2View!!,f2View!!)
|
releaseHolder()
|
}
|
|
fun releaseHolder(){
|
position1 = -1
|
position2 = -1
|
p1View = null
|
b1View = null
|
f1View = null
|
p2View = null
|
b2View = null
|
f2View = null
|
}
|
|
private fun switchViewVisibility(back:View,front:View) {
|
if (back.isShown) {
|
back.visibility = View.GONE
|
front.visibility = View.VISIBLE
|
} else {
|
back.visibility = View.VISIBLE
|
front.visibility = View.GONE
|
}
|
}
|
}
|