У меня есть приложение для 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
показывать правильные значения в журнале.