lmw
2024-07-16 935a87b3578806ca37fee37f03da8c419a3896ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
        }
    }
}