进度条显示 Android 中的错误值?

Progress bar showing wrong values in Android?

我有 android 应用程序,其中有这样的部分:

NavigationDrawer -> Fragment A (shown below) -> Fragment B

Fragment A 就像一个列表,在其中单击一个项目会打开 Fragment B,其中会显示其详细信息。 我的问题是,当我从 Fragment B 返回到 Fragment A 时,列表所有项目的 ProgressBar 显示错误值,即使尝试更改方向也是如此。 它仅在从 NavigationDrawer 打开时显示正确的值。 我很困惑和无能为力,因为项目的其他字段确实显示了正确的值。

作为观察,每个 ProgressBar 的进度都设置为列表中最后一项的进度。

Fragment A

import android.content.Context
import android.graphics.Typeface
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.cardview.widget.CardView
import androidx.fragment.app.Fragment
import com.google.gson.Gson
import kotlinx.android.synthetic.main.contents_grades_sem_wise.view.*
import kotlinx.android.synthetic.main.grades_sem_wise_row.view.*
import kotlin.properties.Delegates

class FragmentGradesSemWise : Fragment(), DialogGPAAddGroup.GPAAddGroupListener {

    private val myCGPA = RecordOfCGPA()
    private class RecordOfCGPA {
        var cgpa = 0f
        var credits = 0
        var previousDone = true
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val theView = inflater.inflate(R.layout.contents_grades_sem_wise, container, false)
        activity!!.title = "Grades"

        val gpaPrefs = activity!!.getSharedPreferences("GPA", Context.MODE_PRIVATE)
        val gpaSet = HashSet(gpaPrefs.getStringSet("semesters", HashSet()))

        val rootView = theView.gsw_root
        rootView.addView(setHeader(inflater, container))

        myCGPA.cgpa = 0f            // I have re-initialised these variables here
        myCGPA.credits = 0          // because otherwise they result in wrong calculation
        myCGPA.previousDone = true  // which I think was somehow related to my strange problem
        for (sem in gpaSet.sorted()) {
            val gpaModelString = gpaPrefs.getString(sem, "")
            val modelGPA = Gson().fromJson(gpaModelString, ModelGPA::class.java)
            rootView.addView(setViewGPA(modelGPA, inflater, container))
        }
        return theView
    }

    private fun setHeader(inflater: LayoutInflater, container: ViewGroup?): View {
        val hView = inflater.inflate(R.layout.grades_sem_wise_row, container, false)

        hView.gswr_sno.text = "Semester"
        hView.gswr_sno.typeface = Typeface.DEFAULT_BOLD
        hView.gswr_tc.text = "Credits"
        hView.gswr_tc.typeface = Typeface.DEFAULT_BOLD
        hView.gswr_sgpa.text = "SGPA"
        hView.gswr_sgpa.typeface = Typeface.DEFAULT_BOLD
        hView.gswr_cgpa.text = "CGPA"
        hView.gswr_cgpa.typeface = Typeface.DEFAULT_BOLD
        hView.gswr_pbv.visibility = View.GONE

        hView.gswr_root.isClickable = false
        hView.gswr_root.isFocusable = false
        hView.gswr_root.isLongClickable = false

        return hView
    }

    private fun setViewGPA(modelGPA: ModelGPA, inflater: LayoutInflater, container: ViewGroup?): View {
        val mView = inflater.inflate(R.layout.grades_sem_wise_row, container, false)
        mView.tag = modelGPA.sem

        mView.gswr_sno.text = modelGPA.sem.toString()

        var credits = 0
        var gradeSum = 0
        var gradedCredits = 0
        var possibleGrades = 0

        for (sub in modelGPA.list) {
            credits += sub.credits
            if (sub.grade == null) {
                possibleGrades += sub.credits * 10
            } else {
                possibleGrades += sub.credits * sub.grade!!
                gradeSum += sub.credits * sub.grade!!
                gradedCredits += sub.credits
            }
        }

        mView.gswr_tc.text = credits.toString()
        if (credits != 0) {
            if (gradedCredits == credits) {
                val sgpa = gradeSum.toFloat() / credits
                mView.gswr_sgpa.text = String.format("%.2f", sgpa)
                if (myCGPA.previousDone) {
                    myCGPA.cgpa = ((myCGPA.cgpa * myCGPA.credits) + gradeSum) / (myCGPA.credits + gradedCredits)
                    myCGPA.credits += gradedCredits
                    mView.gswr_cgpa.text = String.format("%.2f", myCGPA.cgpa)
                }
            } else myCGPA.previousDone = false
/*here is*/ mView.gswr_pbv.progress = if (gradedCredits != 0) 10 * gradeSum / gradedCredits else 0
/*progress*/mView.gswr_pbv.secondaryProgress = 10 * possibleGrades / credits
/*bar*/ } else myCGPA.previousDone = false

        mView.setOnClickListener {
            if (actionMode == null) {
                listener.showGPADetails(modelGPA.sem)
            } else {
                myTracker.toggle(modelGPA.sem)
            }
        }
        mView.setOnLongClickListener {
            myTracker.toggle(modelGPA.sem)
            true
        }

        return mView
    }

}

grades_sem_wise_row.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gswr_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:longClickable="true"
    android:focusable="true"
    android:foreground="?android:attr/selectableItemBackground">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="8dp"
        android:paddingEnd="8dp"
        android:paddingTop="8dp"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/gswr_sno"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="16sp"
                android:textColor="?android:attr/textColorPrimary"/>
            <TextView
                android:id="@+id/gswr_tc"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="16sp"
                android:textColor="?android:attr/textColorPrimary"/>
            <TextView
                android:id="@+id/gswr_sgpa"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="-"
                android:textStyle="italic"
                android:gravity="center"
                android:textSize="16sp"
                android:textColor="?android:attr/textColorPrimary"/>
            <TextView
                android:id="@+id/gswr_cgpa"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="-"
                android:gravity="center"
                android:textSize="16sp"
                android:textColor="?android:attr/textColorPrimary"/>
        </LinearLayout>
        <ProgressBar
            android:id="@+id/gswr_pbv"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:paddingStart="24dp"
            android:paddingEnd="24dp"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>
        <View
            android:layout_marginTop="8dp"
            android:background="?android:attr/listDivider"
            android:layout_width="match_parent"
            android:layout_height="1dp"/>
    </LinearLayout>
    <View
        android:id="@+id/gswr_sel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/colorActivatedHighlight"
        android:visibility="invisible"/>
</androidx.cardview.widget.CardView>

content_grades_sem_wise.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/gsw_root"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- layouts added from code -->
    </LinearLayout>
</ScrollView>

我设置为 ProgressBar 的值确实在日志中显示了正确的值。

看来您遇到了生命周期问题,但我不能 100% 确定,因为如果不看代码就很难说出您是如何执行片段事务的。

您的代码在 onCreateView(...) 期间调用 setViewGPA(...) ,例如,我会将其重构为 onViewCreated(...),以确保代码得到执行创建视图后。

在您的生命周期方法(而不是断点)中添加日志记录并观察事物被调用的顺序,很可能您想要执行的计算并没有在您认为的时候发生。

要么,要么你没有弹出正确的片段,如果没有看到你的更多代码,我不能肯定地说。

我仍然不知道是什么问题,但正如我所说,当我从 NavigationDrawer 打开 Fragment A 时一切正常,所以我所做的是当我从 Fragment B,我已经为事件设置了一个监听器如下:

Fragment B

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    view.isFocusableInTouchMode = true
    view.requestFocus()
    view.setOnKeyListener { _, _, event ->
        if (event.action == KeyEvent.ACTION_DOWN) {
            listener.onBackPressedFromGradesDetails()
            true
        } else false
    }
}

并按如下方式处理 Fragment A 中的方向变化:

Fragment A

override fun onCreateView(//...
    if (savedInstanceState != null) listener.onRotationOfGPA()

并处理 MainActivity 中的两个侦听器以重新开始 Fragment A