| | |
| | | package com.dollearn.student.ui.home.adapter |
| | | |
| | | import android.view.View |
| | | import androidx.constraintlayout.widget.ConstraintLayout |
| | | import cn.sinata.xldutils.activity.ImagePagerActivity |
| | | 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 org.jetbrains.anko.dip |
| | | import org.jetbrains.anko.startActivity |
| | | 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 height:Int = 200 //行高 |
| | | var widgh:Int = 300 //行宽 |
| | | |
| | | //第一次翻开的 |
| | | var position1 = -1 |
| | | var p1View:View? = null |
| | | private var f1View:View? = null |
| | | private var b1View:View? = null |
| | | |
| | | //第2次翻开的 |
| | | var position2 = -1 |
| | | 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) |
| | | frame.setOnClickListener { |
| | | flip(position,frame,back, front) |
| | | val rlVoice = holder.bind<View>(R.id.rl_voice) |
| | | val ivImg = holder.bind<SimpleDraweeView>(R.id.iv_img) |
| | | val iv_play = holder.bind<View>(R.id.iv_play) |
| | | val layoutParams = frame.layoutParams as ConstraintLayout.LayoutParams |
| | | layoutParams.height = height - act.dip(12) |
| | | layoutParams.width = height*3/2 |
| | | if (layoutParams.width > widgh){ |
| | | layoutParams.width = widgh - act.dip(10) |
| | | layoutParams.height = (widgh- act.dip(10))*2/3 |
| | | } |
| | | frame.layoutParams = layoutParams |
| | | if(data.type == 1){ |
| | | rlVoice.gone() |
| | | ivImg.visible() |
| | | ivImg.setImageURI(data.url) |
| | | }else{ |
| | | rlVoice.visible() |
| | | ivImg.gone() |
| | | } |
| | | iv_play.clickDelay { |
| | | if (!act.playing&&position2 == -1){ |
| | | act.index = position |
| | | act.handler?.sendEmptyMessage(act.PLAY_VOICE) |
| | | } |
| | | } |
| | | frame.clickDelay { |
| | | if(!act.playing&&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() |
| | |
| | | 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) { |