lmw
2024-07-17 98faa1d5d540a7e4de4086ea11df3fd61b5c7ab9
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,6 +25,11 @@
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
@@ -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()
@@ -90,7 +106,8 @@
            difficultyDialog.setCallback(object :DifficultyDialog.OnClickCallback{
                override fun onOk(d: Int) {
                    difficulty = d
                    HttpManager.gameHearing(season,week,difficulty).request(this@SuperListenActivity){_,data->
                    HttpManager.gameHearing(season, week, difficulty)
                        .request(this@SuperListenActivity) { _, data ->
                        difficultyDialog.dismissAllowingStateLoss()
                        this@SuperListenActivity.data = data
                        refreshUi()
@@ -150,9 +167,9 @@
        tv_sort.visible()
        tv_exit.visible()
        tv_tip.text = "准备听题"
        rv_list.layoutManager = GridLayoutManager(this,5)
        list.clear()
        list.addAll(data?.subjectList?: arrayListOf())
        initRecycler(list.size)
        voiceList.clear()
        voiceList.addAll(list.map { it.correct }) //声音顺序
        Log.e(TAG,"声音顺序:${voiceList.joinToString(",,,") { it }}")
@@ -168,6 +185,33 @@
            }
        }
        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() {
@@ -222,7 +266,8 @@
            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)
        }