From 1f45a54dc8e149548d3a61d1228741627aa4f23e Mon Sep 17 00:00:00 2001 From: lmw <125975490@qq.com> Date: 星期二, 18 六月 2024 23:05:42 +0800 Subject: [PATCH] save --- app/src/main/java/com/dollearn/student/ui/home/MatchFragment.kt | 598 ++++++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 383 insertions(+), 215 deletions(-) diff --git a/app/src/main/java/com/dollearn/student/ui/home/MatchFragment.kt b/app/src/main/java/com/dollearn/student/ui/home/MatchFragment.kt index c7e7288..d33c5e6 100644 --- a/app/src/main/java/com/dollearn/student/ui/home/MatchFragment.kt +++ b/app/src/main/java/com/dollearn/student/ui/home/MatchFragment.kt @@ -8,15 +8,17 @@ import androidx.core.os.bundleOf import cn.sinata.xldutils.fragment.BaseFragment import cn.sinata.xldutils.gone -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.extention.clickDelay -import kotlinx.android.synthetic.main.fragment_include.* +import kotlinx.android.synthetic.main.fragment_match.* +import org.jetbrains.anko.imageResource + +//todo 2.选择正确后不能再选择 3.选择正确后处理语音条位置 class MatchFragment : BaseFragment(), AudioUtils.OnAudioStatusUpdateListener { - override fun contentViewId() = R.layout.fragment_include + override fun contentViewId() = R.layout.fragment_match private val TAG = "Match=======》" @@ -27,17 +29,15 @@ private val player by lazy { AudioUtils() } private val group by lazy { arguments?.getInt("group")?:0 } private val data by lazy { - (requireActivity() as IncludeActivity).data + (requireActivity() as MatchActivity).data } - private val act by lazy { requireActivity() as IncludeActivity } + private val act by lazy { requireActivity() as MatchActivity } - private var voiceIndex = -1 //点击播放的声音序号 0-5取值 - private val voiceViews by lazy { arrayListOf(cl_voice1,cl_voice2,cl_voice3,cl_voice4,cl_voice5,cl_voice6) } + private val voiceViews by lazy { arrayListOf(cl_voice_1,cl_voice_2,cl_voice_3,cl_voice_4) } private var playing = false - - private val answerImg = arrayListOf<String>() //随机答案图片 - private var currentImage = 0 //当前作答的题号:2,4,5 + private val voiceList = arrayListOf<String>() //语音列表,随机排序 + private var voiceIndex = -1 //点击播放的声音序号 0-3取值 override fun onFirstVisibleToUser() { player.setOnAudioStatusUpdateListener(this) @@ -47,216 +47,368 @@ super.handleMessage(msg) when(msg.what){ PLAY_VOICE->{ - player.startPlayMusic(requireContext(),data!!.subjectList[group][voiceIndex].correct) + player.startPlayMusic(requireContext(),voiceList[voiceIndex]) } TO_NEXT->{ if (isAdded&&!playing){ - (requireActivity() as IncludeActivity).next() + (requireActivity() as MatchActivity).next() } } } } } data?.apply { - val originList = subjectList[group] - iv_1.setImageURI(originList[0].img) - iv_2.setImageURI(originList[1].img) - iv_4.setImageURI(originList[3].img) - answerImg.clear() - answerImg.add(originList[2].img) - answerImg.add(originList[4].img) - answerImg.add(originList[5].img) + val imgs = subjectList[group] + iv_1.setImageURI(imgs[0].img) + iv_2.setImageURI(imgs[1].img) + iv_3.setImageURI(imgs[2].img) + iv_4.setImageURI(imgs[3].img) + voiceList.addAll(imgs.map { it.correct }) + voiceList.shuffle() } - voiceViews.forEachIndexed { index, constraintLayout -> - if (index == 0) - constraintLayout.isEnabled = true - else{ - if (index == 3||index == 5) - constraintLayout.isEnabled = data!!.subjectList[group][index-1].completed - else - constraintLayout.isEnabled = data!!.subjectList[group][index].listend - } - } - showVoiceEnable() initClick() } private fun initClick() { + cover_3.setOnClickListener { } + cover_2.setOnClickListener { } + cover_1.setOnClickListener { } + cover_4.setOnClickListener { } voiceViews.forEachIndexed { index, constraintLayout -> constraintLayout.clickDelay { if (!playing){ - if (index!=0&&!data!!.subjectList[group][index-1].listend){ - myToast("请按顺序听语音") - return@clickDelay - } voiceIndex = index handler?.sendEmptyMessage(PLAY_VOICE) + motion.transitionToEnd() } } } - cl_answer_1.clickDelay { - if (currentImage == 0) + cl_1.clickDelay { + val voiceIndex = voiceIndex + if (playing||voiceIndex == -1) //未播放或者正在播放,不可点击 return@clickDelay - val subject = data!!.subjectList[group][currentImage] - if (!subject.listend||subject.completed){ + if (data!!.subjectList[group][0].completed) //已作答不可点击 return@clickDelay - } - act.totalCount++ - if (answerImg[0] == subject.img){ - act.rightCount++ - data!!.subjectList[group][currentImage].completed = true - voiceViews[currentImage].callOnClick() - motion.transitionToEnd() - }else{ - iv_error_1.visible() - iv_error_1.postDelayed({ - clearAnswerImg() - },3000) - } - } - - cl_answer_2.clickDelay { - if (currentImage == 0) - return@clickDelay - val subject = data!!.subjectList[group][currentImage] - if (!subject.listend||subject.completed){ - return@clickDelay - } - if (answerImg[1] == subject.img){ - data!!.subjectList[group][currentImage].completed = true - voiceViews[currentImage].callOnClick() - motion.transitionToEnd() - }else{ - iv_error_2.visible() - iv_error_2.postDelayed({ - clearAnswerImg() - },3000) - } - } - - cl_answer_3.clickDelay { - if (currentImage == 0) - return@clickDelay - val subject = data!!.subjectList[group][currentImage] - if (!subject.listend||subject.completed){ - return@clickDelay - } - if (answerImg[2] == subject.img){ - data!!.subjectList[group][currentImage].completed = true - voiceViews[currentImage].callOnClick() - motion.transitionToEnd() - }else{ - iv_error_3.visible() - iv_error_3.postDelayed({ - clearAnswerImg() - },3000) - } - } - } - - private fun showVoiceEnable(){ - voiceViews.forEachIndexed { index, constraintLayout -> - if (index == 0) - constraintLayout.isEnabled = true - else{ - if (index == 3||index == 5) - constraintLayout.isEnabled = data!!.subjectList[group][index-1].completed - else - constraintLayout.isEnabled = data!!.subjectList[group][index-1].listend - } - } - } - - private fun clearAnswerImg(){ - iv_answer_1.setImageURI("") - iv_answer_2.setImageURI("") - iv_answer_3.setImageURI("") - iv_3.setImageURI("") - iv_5.setImageURI("") - iv_6.setImageURI("") - iv_full_1.gone() - iv_full_2.gone() - iv_full_3.gone() - iv_error_1.gone() - iv_error_2.gone() - iv_error_3.gone() - data!!.subjectList[group][currentImage].listend = false - } - - private fun showAnswerImg(){ - answerImg.shuffle() - iv_answer_1.setImageURI(answerImg[0]) - iv_answer_2.setImageURI(answerImg[1]) - iv_answer_3.setImageURI(answerImg[2]) - - iv_full_1.visible() - iv_full_2.visible() - iv_full_3.visible() - - val voiceNow = data!!.subjectList[group][currentImage] - //设置动画飞行路径 3对3 - val rightImageIndex = answerImg.indexOf(voiceNow.img) - var start = 0 - var end = 0 - when(rightImageIndex){ - 0->{ - start = R.id.start_1 - when(currentImage){ - 2-> end = R.id.end_1_1 - 4-> end = R.id.end_1_2 - 5-> end = R.id.end_1_3 - } - } - 1->{ - start = R.id.start_2 - when(currentImage){ - 2-> end = R.id.end_2_1 - 4-> end = R.id.end_2_2 - 5-> end = R.id.end_2_3 - } - } - 2->{ - start = R.id.start_3 - when(currentImage){ - 2-> end = R.id.end_3_1 - 4-> end = R.id.end_3_2 - 5-> end = R.id.end_3_3 - } - } - } - motion.progress = 0f - motion.setTransition(start,end) - motion.setTransitionListener(object : MotionLayout.TransitionListener{ - override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) { - when(rightImageIndex){ + act.totalCount ++ + if (data!!.subjectList[group][0].correct == voiceList[voiceIndex]){ + result_1.imageResource = R.mipmap.successs + data!!.subjectList[group][0].completed = true + act.rightCount ++ + //执行飞行动画 + var start = 0 + var end = 0 + when(voiceIndex){ 0->{ - iv_full_1.gone() + start = R.id.start_1 + end = R.id.end_1_1 } 1->{ - iv_full_2.gone() + start = R.id.start_2 + end = R.id.end_2_1 } 2->{ - iv_full_3.gone() + start = R.id.start_3 + end = R.id.end_3_1 + } + 3->{ + start = R.id.start_4 + end = R.id.end_4_1 } } + voiceViews[voiceIndex].callOnClick() + motion.setTransitionListener(object :MotionLayout.TransitionListener{ + override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) { + + } + + override fun onTransitionChange( + p0: MotionLayout?, + p1: Int, + p2: Int, + p3: Float + ) { + } + + override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) { + cl_voice_end_1.visible() + when(voiceIndex){ + 0->cover_1.visible() + 1->cover_2.visible() + 2->cover_3.visible() + 3->cover_4.visible() + } + } + + override fun onTransitionTrigger( + p0: MotionLayout?, + p1: Int, + p2: Boolean, + p3: Float + ) { + } + }) + motion.setTransition(start,end) + motion.transitionToEnd() + }else{ + result_1.imageResource = R.mipmap.zhifushibai + } + showResultAnim(cl_1) + } + + cl_2.clickDelay { + val voiceIndex = voiceIndex + + if (playing||voiceIndex == -1) //未播放或者正在播放,不可点击 + return@clickDelay + if (data!!.subjectList[group][1].completed) //已作答不可点击 + return@clickDelay + act.totalCount ++ + if (data!!.subjectList[group][1].correct == voiceList[voiceIndex]){ + result_2.imageResource = R.mipmap.successs + data!!.subjectList[group][1].completed = true + act.rightCount ++ + //执行飞行动画 + var start = 0 + var end = 0 + when(voiceIndex){ + 0->{ + start = R.id.start_1 + end = R.id.end_1_2 + } + 1->{ + start = R.id.start_2 + end = R.id.end_2_2 + } + 2->{ + start = R.id.start_3 + end = R.id.end_3_2 + } + 3->{ + start = R.id.start_4 + end = R.id.end_4_2 + } + } + voiceViews[voiceIndex].callOnClick() + + motion.setTransitionListener(object :MotionLayout.TransitionListener{ + override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) { + + } + + override fun onTransitionChange( + p0: MotionLayout?, + p1: Int, + p2: Int, + p3: Float + ) { + } + + override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) { + cl_voice_end_2.visible() + when(voiceIndex){ + 0->cover_1.visible() + 1->cover_2.visible() + 2->cover_3.visible() + 3->cover_4.visible() + } + } + + override fun onTransitionTrigger( + p0: MotionLayout?, + p1: Int, + p2: Boolean, + p3: Float + ) { + } + }) + motion.setTransition(start,end) + motion.transitionToEnd() + }else{ + result_2.imageResource = R.mipmap.zhifushibai + } + showResultAnim(cl_2) + } + + cl_3.clickDelay { + val voiceIndex = voiceIndex + + if (playing||voiceIndex == -1) //未播放或者正在播放,不可点击 + return@clickDelay + if (data!!.subjectList[group][2].completed) //已作答不可点击 + return@clickDelay + act.totalCount ++ + if (data!!.subjectList[group][2].correct == voiceList[voiceIndex]){ + result_3.imageResource = R.mipmap.successs + data!!.subjectList[group][2].completed = true + act.rightCount ++ + //执行飞行动画 + var start = 0 + var end = 0 + when(voiceIndex){ + 0->{ + start = R.id.start_1 + end = R.id.end_1_3 + } + 1->{ + start = R.id.start_2 + end = R.id.end_2_3 + } + 2->{ + start = R.id.start_3 + end = R.id.end_3_3 + } + 3->{ + start = R.id.start_4 + end = R.id.end_4_3 + } + } + voiceViews[voiceIndex].callOnClick() + + motion.setTransitionListener(object :MotionLayout.TransitionListener{ + override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) { + + } + + override fun onTransitionChange( + p0: MotionLayout?, + p1: Int, + p2: Int, + p3: Float + ) { + } + + override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) { + cl_voice_end_3.visible() + when(voiceIndex){ + 0->cover_1.visible() + 1->cover_2.visible() + 2->cover_3.visible() + 3->cover_4.visible() + } + } + + override fun onTransitionTrigger( + p0: MotionLayout?, + p1: Int, + p2: Boolean, + p3: Float + ) { + } + }) + motion.setTransition(start,end) + motion.transitionToEnd() + }else{ + result_3.imageResource = R.mipmap.zhifushibai + } + showResultAnim(cl_3) + } + + cl_4.clickDelay { + val voiceIndex = voiceIndex + if (playing||voiceIndex == -1) //未播放或者正在播放,不可点击 + return@clickDelay + if (data!!.subjectList[group][3].completed) //已作答不可点击 + return@clickDelay + act.totalCount ++ + if (data!!.subjectList[group][3].correct == voiceList[voiceIndex]){ + result_4.imageResource = R.mipmap.successs + data!!.subjectList[group][3].completed = true + act.rightCount ++ + //执行飞行动画 + var start = 0 + var end = 0 + when(voiceIndex){ + 0->{ + start = R.id.start_1 + end = R.id.end_1_4 + } + 1->{ + start = R.id.start_2 + end = R.id.end_2_4 + } + 2->{ + start = R.id.start_3 + end = R.id.end_3_4 + } + 3->{ + start = R.id.start_4 + end = R.id.end_4_4 + } + } + voiceViews[voiceIndex].callOnClick() + + motion.setTransitionListener(object :MotionLayout.TransitionListener{ + override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) { + + } + + override fun onTransitionChange( + p0: MotionLayout?, + p1: Int, + p2: Int, + p3: Float + ) { + } + + override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) { + cl_voice_end_4.visible() + when(voiceIndex){ + 0->cover_1.visible() + 1->cover_2.visible() + 2->cover_3.visible() + 3->cover_4.visible() + } + } + + override fun onTransitionTrigger( + p0: MotionLayout?, + p1: Int, + p2: Boolean, + p3: Float + ) { + } + }) + motion.setTransition(start,end) + motion.transitionToEnd() + }else{ + result_4.imageResource = R.mipmap.zhifushibai + } + showResultAnim(cl_4) + } + + cl_voice_end_1.setOnClickListener { + voiceIndex = 4 + player.startPlayMusic(requireContext(),data!!.subjectList[group][0].correct) + } + cl_voice_end_2.setOnClickListener { + voiceIndex = 5 + player.startPlayMusic(requireContext(),data!!.subjectList[group][1].correct) + + } + cl_voice_end_3.setOnClickListener { + voiceIndex = 6 + player.startPlayMusic(requireContext(),data!!.subjectList[group][2].correct) + + } + cl_voice_end_4.setOnClickListener { + voiceIndex = 7 + player.startPlayMusic(requireContext(),data!!.subjectList[group][3].correct) + + } + } + + private fun showResultAnim(m:MotionLayout){ + m.transitionToEnd() + m.setTransitionListener(object :MotionLayout.TransitionListener{ + override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) { } override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) { } override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) { - when(currentImage){ - 2->{ - iv_3.setImageURI(data!!.subjectList[group][2].img) - } - 4->{ - iv_5.setImageURI(data!!.subjectList[group][4].img) - } - 5->{ - iv_6.setImageURI(data!!.subjectList[group][5].img) - } - } + m.progress = 0f } override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) { @@ -270,13 +422,19 @@ fun recover(){ Log.e(TAG,"回到上一题,恢复答题前的状态") motion.progress = 0f - clearAnswerImg() - currentImage = 0 data!!.subjectList[group].forEach { it.completed = false it.listend = false } - showVoiceEnable() + cover_1.gone() + cover_2.gone() + cover_3.gone() + cover_4.gone() + tv_name.text ="" + cl_voice_end_1.gone() + cl_voice_end_2.gone() + cl_voice_end_3.gone() + cl_voice_end_4.gone() } companion object{ @@ -299,10 +457,14 @@ override fun onStartPlay() { playing = true + val list = data!!.subjectList[group] + if (voiceIndex in 0..3) + tv_name.text = list[list.map { it.correct }.indexOf(voiceList[voiceIndex])].name when(voiceIndex){ 0->{ iv1_1.gone() iv2_1.gone() + iv_playing_1.visible() iv_playing_1.visible() } 1->{ @@ -321,15 +483,26 @@ iv2_4.gone() iv_playing_4.visible() } + 4->{ - iv1_5.gone() - iv2_5.gone() - iv_playing_5.visible() + iv1_1_end.gone() + iv2_1_end.gone() + iv_playing_1_end.visible() } 5->{ - iv1_6.gone() - iv2_6.gone() - iv_playing_6.visible() + iv1_2_end.gone() + iv2_2_end.gone() + iv_playing_2_end.visible() + } + 6->{ + iv1_3_end.gone() + iv2_3_end.gone() + iv_playing_3_end.visible() + } + 7->{ + iv1_4_end.gone() + iv2_4_end.gone() + iv_playing_4_end.visible() } } @@ -337,8 +510,6 @@ override fun onFinishPlay() { playing = false - val subject = data!!.subjectList[group][voiceIndex] - subject.listend = true when(voiceIndex){ 0->{ iv1_1.visible() @@ -354,10 +525,6 @@ iv1_3.visible() iv2_3.visible() iv_playing_3.gone() - if (!subject.completed){ - currentImage = 2 - showAnswerImg() - } } 3->{ iv1_4.visible() @@ -365,27 +532,28 @@ iv_playing_4.gone() } 4->{ - iv1_5.visible() - iv2_5.visible() - iv_playing_5.gone() - if (!subject.completed){ - currentImage = 4 - showAnswerImg() - } + iv1_1_end.visible() + iv2_1_end.visible() + iv_playing_1_end.gone() } 5->{ - iv1_6.visible() - iv2_6.visible() - iv_playing_6.gone() - if (!subject.completed){ - currentImage = 5 - showAnswerImg() - }else{ - Log.e(TAG,"本题全部答对,3秒后进入下一图") - handler?.sendEmptyMessageDelayed(TO_NEXT,3000) - } + iv1_2_end.visible() + iv2_2_end.visible() + iv_playing_2_end.gone() + } + 6->{ + iv1_3_end.visible() + iv2_3_end.visible() + iv_playing_3_end.gone() + } + 7->{ + iv1_4_end.visible() + iv2_4_end.visible() + iv_playing_4_end.gone() } } - showVoiceEnable() + if (data!!.subjectList[group].filter { it.completed }.size == 4){ //全部答对 + handler?.sendEmptyMessage(TO_NEXT) + } } } \ No newline at end of file -- Gitblit v1.7.1