From 98faa1d5d540a7e4de4086ea11df3fd61b5c7ab9 Mon Sep 17 00:00:00 2001
From: lmw <125975490@qq.com>
Date: 星期三, 17 七月 2024 11:47:03 +0800
Subject: [PATCH] 将要取消游戏页面item的结果动画

---
 app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt |  123 ++++++++++++++++++++++++++++-------------
 1 files changed, 84 insertions(+), 39 deletions(-)

diff --git a/app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt b/app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt
index a84aa71..bc2b591 100644
--- a/app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt
+++ b/app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt
@@ -8,7 +8,6 @@
 import androidx.recyclerview.widget.GridLayoutManager
 import cn.sinata.xldutils.gone
 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.dialog.CountdownDialog
@@ -26,15 +25,20 @@
 import kotlinx.android.synthetic.main.activity_super_listen.*
 import kotlinx.android.synthetic.main.fragmetn_look_img.*
 import org.greenrobot.eventbus.EventBus
+import kotlin.math.abs
+import kotlin.math.max
+import kotlin.math.min
+import kotlin.math.sqrt
+
 
 class SuperListenActivity : TransparentStatusBarActivity(), AudioUtils.OnAudioStatusUpdateListener {
     override fun setContentView() = R.layout.activity_super_listen
 
     private val week by lazy {
-        intent.getIntExtra("week",0)
+        intent.getIntExtra("week", 0)
     }
     private val season by lazy {
-        intent.getIntExtra("season",0)
+        intent.getIntExtra("season", 0)
     }
     var data:GameBean? = null
     var index = 0
@@ -77,7 +81,19 @@
     override fun initClick() {
         tv_exit.setOnClickListener {
             if (tv_exit.text == "提交"){
-                ResultActivity.startResult(this,0,0,0,6,list.size,rightCount,list.filter { it.right }.sumBy { 1 },time,data!!.data.id,difficulty)
+                ResultActivity.startResult(
+                    this,
+                    0,
+                    0,
+                    0,
+                    6,
+                    list.size,
+                    rightCount,
+                    list.filter { it.right }.sumBy { 1 },
+                    time,
+                    data!!.data.id,
+                    difficulty
+                )
                 finish()
             }else
                 onBackPressed()
@@ -86,23 +102,24 @@
 
     override fun initView() {
         tv_sort.postDelayed({
-             val difficultyDialog = DifficultyDialog()
-            difficultyDialog.setCallback(object :DifficultyDialog.OnClickCallback{
+            val difficultyDialog = DifficultyDialog()
+            difficultyDialog.setCallback(object : DifficultyDialog.OnClickCallback {
                 override fun onOk(d: Int) {
                     difficulty = d
-                    HttpManager.gameHearing(season,week,difficulty).request(this@SuperListenActivity){_,data->
-                        difficultyDialog.dismissAllowingStateLoss()
-                        this@SuperListenActivity.data = data
-                        refreshUi()
-                    }
+                    HttpManager.gameHearing(season, week, difficulty)
+                        .request(this@SuperListenActivity) { _, data ->
+                            difficultyDialog.dismissAllowingStateLoss()
+                            this@SuperListenActivity.data = data
+                            refreshUi()
+                        }
                 }
 
                 override fun onCancel() {
                     finish()
                 }
             })
-            difficultyDialog.show(supportFragmentManager,"dif")
-        },500)
+            difficultyDialog.show(supportFragmentManager, "dif")
+        }, 500)
 
         player.setOnAudioStatusUpdateListener(this)
         player.stopPlayMusic()
@@ -110,39 +127,39 @@
             override fun handleMessage(msg: Message) {
                 super.handleMessage(msg)
                 when(msg.what){
-                    STUDY_TIME->{
+                    STUDY_TIME -> {
                         time++
-                        sendEmptyMessageDelayed(STUDY_TIME,1000)
+                        sendEmptyMessageDelayed(STUDY_TIME, 1000)
                     }
-                    PLAY_VOICE->{
-                        if (index<voiceList.size)
-                            player.startPlayMusic(this@SuperListenActivity,voiceList[index])
+                    PLAY_VOICE -> {
+                        if (index < voiceList.size)
+                            player.startPlayMusic(this@SuperListenActivity, voiceList[index])
                     }
-                    COUNT_DOWN->{
-                        if (!playing){
-                            countTime --
+                    COUNT_DOWN -> {
+                        if (!playing) {
+                            countTime--
                             state = 2
                         }
                         tv_tip.text = "请在${countTime}s内选择答案!"
-                        if ( countTime == 0){
+                        if (countTime == 0) {
                             index++
                             startGame()
-                        }else{
-                            sendEmptyMessageDelayed(COUNT_DOWN,1000)
+                        } else {
+                            sendEmptyMessageDelayed(COUNT_DOWN, 1000)
                         }
                     }
-                    PLAY_RIGHT->{
+                    PLAY_RIGHT -> {
                         rightPlaying = true
-                        player.startPlayMusic(this@SuperListenActivity,rightVoice)
+                        player.startPlayMusic(this@SuperListenActivity, rightVoice)
                     }
-                    PLAY_ERROR->{
+                    PLAY_ERROR -> {
                         errorPlaying = true
-                        player.startPlayMusic(this@SuperListenActivity,errorVoice)
+                        player.startPlayMusic(this@SuperListenActivity, errorVoice)
                     }
                 }
             }
         }
-        handler?.sendEmptyMessageDelayed(STUDY_TIME,1000)
+        handler?.sendEmptyMessageDelayed(STUDY_TIME, 1000)
     }
 
     private fun refreshUi() {
@@ -150,15 +167,15 @@
         tv_sort.visible()
         tv_exit.visible()
         tv_tip.text = "准备听题"
-        rv_list.layoutManager = GridLayoutManager(this,5)
         list.clear()
-        list.addAll(data?.subjectList?: arrayListOf())
+        list.addAll(data?.subjectList ?: arrayListOf())
+        initRecycler(list.size)
         voiceList.clear()
         voiceList.addAll(list.map { it.correct }) //声音顺序
-        Log.e(TAG,"声音顺序:${voiceList.joinToString(",,,") { it }}")
+        Log.e(TAG, "声音顺序:${voiceList.joinToString(",,,") { it }}")
         list.shuffle() //图片随机顺序
         rv_list.adapter = adapter
-        Log.e(TAG,"图片打乱后顺序:${list.joinToString(",,,") { it.name }}")
+        Log.e(TAG, "图片打乱后顺序:${list.joinToString(",,,") { it.name }}")
         TIME = data!!.data.time.toInt()
         val countdownDialog = CountdownDialog()
         countdownDialog.callback = object :StringCallback{
@@ -167,18 +184,45 @@
                 startGame()
             }
         }
-        countdownDialog.show(supportFragmentManager,"timer")
+        countdownDialog.show(supportFragmentManager, "timer")
+    }
+
+    /**
+     * 需要计算行列数
+     */
+    private fun initRecycler(total: Int){
+        var bestRows = 1
+        var bestColumns: Int = total
+        var i = 1
+        while (i <= sqrt(total.toDouble())) {
+            val rows = i
+            val columns: Int = if (total%i>0) (total / i)+1 else total / i
+            // Calculate the absolute difference between rows and columns
+            val diff = abs(rows - columns)
+            // Update the best rows and columns if this pair has a smaller difference
+            if (diff < abs(bestRows - bestColumns)) {
+                bestRows = rows
+                bestColumns = columns
+            }
+            i++
+        }
+        Log.e(TAG,"总数:$total,计算得到行数:${min(bestColumns,bestRows)},列数:${max(bestColumns,bestRows)}")
+        val lineHeight = rv_list.height.toDouble() / min(bestColumns, bestRows)
+        Log.e(TAG,"高度:${rv_list.height},计算得到行高:${lineHeight}")
+        rv_list.layoutManager = GridLayoutManager(this, max(bestColumns,bestRows))
+        adapter.height = lineHeight.toInt()
+        adapter.notifyDataSetChanged()
     }
 
     fun startGame() {
         if (index < voiceList.size){
-            Log.e(TAG,"开始答题:index=${index}")
+            Log.e(TAG, "开始答题:index=${index}")
             tv_sort.text = (index+1).toString()
             handler?.removeMessages(COUNT_DOWN)
             countTime = TIME //重置答题时间
             tv_tip.text = "准备听题"
             state = 0
-            handler?.sendEmptyMessageDelayed(PLAY_VOICE,if (index == 0) 200 else 3000)
+            handler?.sendEmptyMessageDelayed(PLAY_VOICE, if (index == 0) 200 else 3000)
         }else{
             tv_sort.visibility = View.INVISIBLE
             tv_tip.text = ""
@@ -202,7 +246,7 @@
         if (!data!!.subjectList[data!!.subjectList.map { it.correct }.indexOf(voiceList[index])].completed){//首次播放
             state = 1
             tv_tip.text = "请在${countTime}s内选择答案!"
-            handler?.sendEmptyMessageDelayed(COUNT_DOWN,1000)
+            handler?.sendEmptyMessageDelayed(COUNT_DOWN, 1000)
         }
         iv1.gone()
         iv2.gone()
@@ -222,14 +266,15 @@
             errorPlaying = false
         if (rightPlaying){
             rightPlaying = false
-            val subject = data!!.subjectList[data!!.subjectList.map { it.correct }.indexOf(voiceList[index])] //当前音频对应题目
+            val subject = data!!.subjectList[data!!.subjectList.map { it.correct }
+                .indexOf(voiceList[index])] //当前音频对应题目
             subject.completed = true
             handler?.sendEmptyMessage(PLAY_VOICE)
         }
     }
 
     override fun onBackPressed() {
-        HttpManager.exitGameOrStory(time).request(this){_,_->
+        HttpManager.exitGameOrStory(time).request(this){ _, _->
             super.onBackPressed()
         }
     }

--
Gitblit v1.7.1