From aa512ff5fc428fbee046d6bc0761c3675023769e Mon Sep 17 00:00:00 2001
From: 罗明文 <125975490@qq.com>
Date: 星期三, 21 五月 2025 15:49:32 +0800
Subject: [PATCH] save

---
 app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt |  202 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 183 insertions(+), 19 deletions(-)

diff --git a/app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt b/app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt
index c6f0d38..1242aca 100644
--- a/app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt
+++ b/app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt
@@ -15,9 +15,12 @@
 import cn.sinata.xldutils.fragment.BaseFragment
 import cn.sinata.xldutils.gone
 import cn.sinata.xldutils.invisible
+import cn.sinata.xldutils.utils.SPUtils
+import cn.sinata.xldutils.utils.myToast
 import cn.sinata.xldutils.visible
 import com.dollearn.student.R
 import com.dollearn.student.utils.AudioUtils
+import com.dollearn.student.utils.Const
 import com.dollearn.student.utils.extention.clickDelay
 import kotlinx.android.synthetic.main.fragment_q_a.*
 import org.jetbrains.anko.support.v4.dip
@@ -30,6 +33,15 @@
     private var handler: Handler? = null
     private val PLAY_VOICE = 1
     private val TO_NEXT = 2
+
+    private val PLAY_ERROR = 3
+    private val PLAY_RIGHT = 4
+
+    private val errorVoice by lazy { SPUtils.instance().getString(Const.EV) }
+    private val rightVoice by lazy { SPUtils.instance().getString(Const.RV) }
+
+    private var rightPlaying = false
+    private var errorPlaying = false
 
     private val player by lazy { AudioUtils() }
     private val group by lazy { arguments?.getInt("group")?:0 }
@@ -45,7 +57,7 @@
     private val voiceViews by lazy { arrayListOf(cl_voice_1,cl_voice_2,cl_voice_3,cl_voice_4) } //语音蓝条
     private val v1Views by lazy { arrayListOf(iv1_1,iv1_2,iv1_3,iv1_4,iv1_answer_1,iv1_answer_2,iv1_answer_3) }
     private val v2Views by lazy { arrayListOf(iv2_1,iv2_2,iv2_3,iv2_4,iv2_answer_1,iv2_answer_2,iv2_answer_3) }
-    private val v3Views by lazy { arrayListOf(iv3_1,iv3_2,iv3_3,iv3_4) }
+    private val resultViews by lazy { arrayListOf(iv_right_1,iv_right_2,iv_right_3,iv_right_4) }
     private val playingViews by lazy { arrayListOf(iv_playing_1,iv_playing_2,iv_playing_3,iv_playing_4,iv_playing_answer_1,iv_playing_answer_2,iv_playing_answer_3) }
 
     private var voiceIndex = -1 //点击播放的声音序号 0-3取值
@@ -57,6 +69,12 @@
 
     private var answerList = arrayListOf<String>() //右侧答案语音
 
+    private var nowVoiceView: View? = null
+
+    private var isAutoPlaying = 0
+
+    var right = true //true进入下一题为全对,false 一旦答错过就置为false,此时进入下一题,本题算做错误
+
 
     override fun onFirstVisibleToUser() {
         player.setOnAudioStatusUpdateListener(this)
@@ -66,6 +84,7 @@
                 super.handleMessage(msg)
                 when(msg.what){
                     PLAY_VOICE->{
+                        playing = true
                         if (voiceIndex in 0..3)
                             player.startPlayMusic(requireContext(),list[voiceIndex].correct)
                         else
@@ -76,6 +95,14 @@
                             (requireActivity() as QAActivity).next()
                         }
                     }
+                    PLAY_RIGHT->{
+                        rightPlaying = true
+                        player.startPlayMusic(requireContext(),rightVoice)
+                    }
+                    PLAY_ERROR->{
+                        errorPlaying = true
+                        player.startPlayMusic(requireContext(),errorVoice)
+                    }
                 }
             }
         }
@@ -83,82 +110,151 @@
         iv_2.setImageURI(list[1].img)
         iv_3.setImageURI(list[2].img)
         iv_4.setImageURI(list[3].img)
-        showVoiceUi()
-
+        recover()
         rl_check_1.clickDelay {
+            if (playing)
+                return@clickDelay
             if (list[questionIndex].completed) //已作答
                 return@clickDelay
+            if (!rl_check_1.isSelected||!rl_check_2.isSelected||!rl_check_3.isSelected){
+                myToast("请先听完")
+                return@clickDelay
+            }
             act.totalCount++
             if (list[questionIndex].correct == answerList[0]){
                 act.rightCount++
                 Log.e(TAG,"选择正确")
                 transAnimation(cl_answer_1,voiceViews[questionIndex])
-                list[questionIndex].completed = true
+                val resultView = resultViews[questionIndex]
+                resultView.visible()
+                resultView.postDelayed({
+                    resultView.gone()
+                },2000)
+                nowVoiceView = voiceViews[questionIndex]
+                handler?.sendEmptyMessage(PLAY_RIGHT)
             }else{
+                right = false
+                recoverAnswer()
+                handler?.sendEmptyMessage(PLAY_ERROR)
                 dot_1.visible()
                 iv_error_1.visible()
                 dot_1.postDelayed({
                     dot_1.gone()
                     iv_error_1.gone()
-                },500)
+                },2000)
             }
         }
         rl_check_2.clickDelay {
+            if (playing)
+                return@clickDelay
             if (list[questionIndex].completed) //已作答
                 return@clickDelay
+            if (!rl_check_1.isSelected||!rl_check_2.isSelected||!rl_check_3.isSelected){
+                myToast("请先听完")
+                return@clickDelay
+            }
             act.totalCount++
             if (list[questionIndex].correct == answerList[1]){
                 act.rightCount++
                 Log.e(TAG,"选择正确")
                 transAnimation(cl_answer_2,voiceViews[questionIndex])
-                list[questionIndex].completed = true
+                val resultView = resultViews[questionIndex]
+                resultView.visible()
+                resultView.postDelayed({
+                    resultView.gone()
+                },2000)
+                nowVoiceView = voiceViews[questionIndex]
+                handler?.sendEmptyMessage(PLAY_RIGHT)
             }else{
+                recoverAnswer()
+                handler?.sendEmptyMessage(PLAY_ERROR)
                 dot_2.visible()
                 iv_error_2.visible()
                 dot_2.postDelayed({
                     dot_2.gone()
                     iv_error_2.gone()
-                },500)
+                },2000)
             }
         }
         rl_check_3.clickDelay {
+            if (playing)
+                return@clickDelay
             if (list[questionIndex].completed) //已作答
                 return@clickDelay
+            if (!rl_check_1.isSelected||!rl_check_2.isSelected||!rl_check_3.isSelected){
+                myToast("请先听完")
+                return@clickDelay
+            }
             act.totalCount++
             if (list[questionIndex].correct == answerList[2]){
                 act.rightCount++
                 Log.e(TAG,"选择正确")
                 transAnimation(cl_answer_3,voiceViews[questionIndex])
-                list[questionIndex].completed = true
+                val resultView = resultViews[questionIndex]
+                resultView.visible()
+                resultView.postDelayed({
+                    resultView.gone()
+                },2000)
+                nowVoiceView = voiceViews[questionIndex]
+                handler?.sendEmptyMessage(PLAY_RIGHT)
             }else{
+                recoverAnswer()
+                handler?.sendEmptyMessage(PLAY_ERROR)
                 dot_3.visible()
                 iv_error_3.visible()
                 dot_3.postDelayed({
                     dot_3.gone()
                     iv_error_3.gone()
-                },500)
+                },2000)
             }
         }
+
+        isAutoPlaying = 1
+        voiceViews.filterIndexed { index, constraintLayout ->
+            val subject = list[index]
+            constraintLayout.isEnabled&&subject.isQuestion == 1
+        }.firstOrNull()?.callOnClick()
     }
 
 
-
+    /**
+     * 恢复答案和题目ui
+     */
     fun recover(){
+        right = true
+        showVoiceUi()
+        list.forEach { it.completed = false }
+        recoverAnswer()
+    }
 
+    /**
+     * 只恢复答案ui
+     */
+    private fun recoverAnswer(){
+        rl_check_1.isSelected = false
+        rl_check_2.isSelected = false
+        rl_check_3.isSelected = false
+        if (list.filter { it.completed }.isEmpty()){
+            voiceViews[2].isEnabled = false
+            voiceViews[3].isEnabled = false
+        }else{
+            voiceViews[2].isEnabled = true
+            voiceViews[3].isEnabled = true
+        }
     }
 
     private fun showVoiceUi(){
         list.forEachIndexed { index, subject ->
             if (subject.isQuestion == 1){
-                v3Views[index].setImageResource(R.mipmap.voice_question)
                 voiceViews[index].visible()
             }else{
-                v3Views[index].setImageResource(R.mipmap.voice_answer)
                 voiceViews[index].invisible()
             }
             voiceViews[index].clickDelay {
-                voiceIndex = index
-                handler?.sendEmptyMessage(PLAY_VOICE)
+                if (!playing){
+                    voiceIndex = index
+                    handler?.sendEmptyMessage(PLAY_VOICE)
+                }
             }
         }
     }
@@ -177,24 +273,40 @@
             layoutChanged = true
         }
         questionIndex = position
+        if (questionIndex == 0||questionIndex == 2){
+            iv2_answer_1.setImageResource(R.mipmap.voice_question_2)
+            iv2_answer_2.setImageResource(R.mipmap.voice_question_2)
+            iv2_answer_3.setImageResource(R.mipmap.voice_question_2)
+        }else{
+            iv2_answer_1.setImageResource(R.mipmap.voice_answer_2)
+            iv2_answer_2.setImageResource(R.mipmap.voice_answer_2)
+            iv2_answer_3.setImageResource(R.mipmap.voice_answer_2)
+        }
         rl_check_1.visible()
         rl_check_2.visible()
         rl_check_3.visible()
         cl_answer_1.visible()
         cl_answer_2.visible()
         cl_answer_3.visible()
+        answerList.clear()
         answerList.add(list[position].correct)
         answerList.addAll(list[position].error?.split(",")?: arrayListOf())
         answerList.shuffle()
         cl_answer_1.setOnClickListener {
+            if (playing)
+                return@setOnClickListener
             voiceIndex = 4
             handler?.sendEmptyMessage(PLAY_VOICE)
         }
         cl_answer_2.setOnClickListener {
+            if (playing)
+                return@setOnClickListener
             voiceIndex = 5
             handler?.sendEmptyMessage(PLAY_VOICE)
         }
         cl_answer_3.setOnClickListener {
+            if (playing)
+                return@setOnClickListener
             voiceIndex = 6
             handler?.sendEmptyMessage(PLAY_VOICE)
         }
@@ -207,7 +319,7 @@
         val x = PropertyValuesHolder.ofFloat("translationX",0f,offx.toFloat())
         val y = PropertyValuesHolder.ofFloat("translationY",0f,offy.toFloat())
         val animator = ObjectAnimator.ofPropertyValuesHolder(v, x, y)
-        animator.duration = 1000
+        animator.duration = 500
         animator.interpolator = LinearInterpolator()
         animator.start()
         animator.addListener(object :Animator.AnimatorListener{
@@ -257,20 +369,22 @@
 
     override fun onStartPlay() {
         playing = true
+        if (rightPlaying||errorPlaying)
+            return
         v1Views[voiceIndex].gone()
         v2Views[voiceIndex].gone()
         playingViews[voiceIndex].visible()
-        if (voiceIndex == 0&&!list[1].completed){
+        if (voiceIndex == 0&&list[0].isQuestion == 1&&!list[1].completed){
             showAnswerView(1)
         }
-        if (voiceIndex == 1&&!list[0].completed){
+        if (voiceIndex == 1&&list[1].isQuestion == 1&&!list[0].completed){
             showAnswerView(0)
 
         }
-        if (voiceIndex == 2&&!list[3].completed){
+        if (voiceIndex == 2&&list[2].isQuestion == 1&&!list[3].completed){
             showAnswerView(3)
         }
-        if (voiceIndex == 3&&!list[2].completed){
+        if (voiceIndex == 3&&list[3].isQuestion == 1&&!list[2].completed){
             showAnswerView(2)
         }
     }
@@ -280,5 +394,55 @@
         v1Views[voiceIndex].visible()
         v2Views[voiceIndex].visible()
         playingViews[voiceIndex].gone()
+        if (!errorPlaying&&!rightPlaying&&!list[questionIndex].completed){
+            if (voiceIndex == 4){
+                rl_check_1.isSelected = true
+            }
+            if (voiceIndex == 5){
+                rl_check_2.isSelected = true
+            }
+            if (voiceIndex == 6){
+                rl_check_3.isSelected = true
+            }
+        }
+
+        if (isAutoPlaying in 1..2){
+            if (voiceIndex in 0..3){
+                cl_answer_1.callOnClick()
+            }else if (voiceIndex == 4)
+                cl_answer_2.callOnClick()
+            else if (voiceIndex == 5)
+                cl_answer_3.callOnClick()
+            else if (voiceIndex == 6){
+                isAutoPlaying = if (isAutoPlaying == 1) 0 else -1
+            }
+        }
+
+        if (errorPlaying)
+            errorPlaying = false
+        else if (rightPlaying){
+            rightPlaying = false
+            nowVoiceView?.callOnClick()
+            list[questionIndex].completed = true
+            recoverAnswer()
+        }else if (list.filter { it.completed }.size == 2){ //全部作答,3秒后下一题
+            handler?.sendEmptyMessageDelayed(TO_NEXT,500) //万俊杰:缩短为0.5秒
+        }else if (list.filter { it.completed }.size == 1&& isAutoPlaying == 0){//答完一题,自动播放下一题
+            isAutoPlaying = 2
+            voiceViews.filterIndexed { index, constraintLayout ->
+                val subject = list[index]
+                constraintLayout.isEnabled&&subject.isQuestion == 1
+            }.lastOrNull()?.callOnClick()
+        }
+    }
+
+    override fun onPause() {
+        super.onPause()
+        player.stopPlayMusic()
+    }
+
+    override fun onDestroy() {
+        super.onDestroy()
+        handler?.removeCallbacksAndMessages(null)
     }
 }
\ No newline at end of file

--
Gitblit v1.7.1