我有一个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
的值确实在日志中显示了正确的值。
答案 0 :(得分:0)
您似乎遇到了生命周期问题,但是我不确定100%,因为很难在不看代码的情况下就知道如何执行片段事务。
您的代码调用setViewGPA(...)
在 onCreateView(...)
中,例如,我会将其重构为onViewCreated(...)
,以确保视图被执行后代码得以执行。创建。
在您的生命周期方法(不是断点)中添加日志记录,并观察事物被调用的顺序,您认为要执行的计算很可能没有发生。
或者,或者您没有弹出正确的片段,如果不能看到更多代码,我不能肯定地说。
答案 1 :(得分:0)
我仍然不知道出了什么问题,但是正如我说的那样,当我从Adldap Provider class
打开Fragment A
时一切正常,所以我所做的就是当我从{{1 }},我为事件设置了一个侦听器,如下所示:
NavigationDrawer
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()
。