我是android studio和kotlin的业余爱好者。我正在实现一个场景,其中有一些片段,当我们完成一个片段时,进度显示在主布局上。这些片段替换了主框架布局,并使用setonclicklistener
在上一个片段中完成了从一个片段到下一个片段的切换。但是,问题是我看不到进度条正在发生更新。顺便说一句,我没有使用任何进度对话。当我从一个片段切换到另一个片段时,我所需要的只是更新进度条。当我运行该应用程序时,它不会崩溃,但不会发生更新。如下所示的main_layout文件,并且帧布局被片段替换。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/parentmain_progress"
android:layout_width="match_parent"
android:layout_height="25dp"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:scaleY="2"/>
<FrameLayout
android:id="@+id/parentmain_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/parentmain_progress">
</FrameLayout>
</RelativeLayout>
其中一个进度没有更新的片段:
class ParentChildInfoFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_parent_info_relation, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
headerparentrelation_text?.text= arguments?.getString("name")
parentmain_progress?.max=4
parentmain_progress?.setProgress(3)
val string = "Your Cild is a....\n Girl/Boy"
specifying_relation_txt?.text = string
relation_mom_image?.setImageResource(R.drawable.girl)
relation_dad_image?.setImageResource(R.drawable.boy)
val girlstring = "Girl"
mom_txt?.text = girlstring
val boystring = "Boy"
dad_txt?.text=boystring
nextbtn_parentrelation.setOnClickListener {
fragmentManager?.beginTransaction()?.replace(R.id.parentmain_frame,ParentInfoFragment())?.commit()
}
}
}
任何帮助表示赞赏。
答案 0 :(得分:1)
Like that ?
On this example I use the new package structure : Androidx
If you do not use it yet, simply change
import androidx.fragment.app.FragmentTransaction
by
import android.app.FragmentTransaction
You will find the code on this deposit :
https://github.com/Amadou19/Android-Multiple-fragment-with-progressBar---Kotlin.git
/****************************/
fragment1.xml
/****************************/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c15d5d">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="Fragment 1"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Click Me"/>
</RelativeLayout>
/****************************/
Fragment1
/****************************/
class Fragment1 : Fragment() {
interface Fragment1Listner {
fun onClickFragment1()
}
var a1: Fragment1Listner? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment1, container, false)
view.findViewById<Button>(R.id.button).setOnClickListener {
Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show()
a1?.onClickFragment1()
}
return view
}
}
/****************************/
fragment2.xml
/****************************/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5d80c1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="Fragment 2"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Click Me"/>
</RelativeLayout>
/****************************/
Fragment2
/****************************/
class Fragment2 : Fragment() {
interface Fragment2Listner {
fun onClickFragment2()
}
var a2: Fragment2Listner? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment2, container, false)
view.findViewById<Button>(R.id.button2).setOnClickListener {
Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show()
a2?.onClickFragment2()
}
return view }
}
/****************************/
fragment3.xml
/****************************/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fab049">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="Fragment 3"/>
</RelativeLayout>
/****************************/
Fragment3
/****************************/
class Fragment3 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment3, container, false)
return view }
}
/****************************/
activity_main.xml
/****************************/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.amadoutirera.progressbar2.MainActivity">
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/progress_bar"/>
</RelativeLayout>
/****************************/
MainActivity
/****************************/
class MainActivity : AppCompatActivity(), Fragment1.Fragment1Listner, Fragment2.Fragment2Listner {
lateinit var progressBar : ProgressBar
private lateinit var fragment1: Fragment1
private lateinit var fragment2: Fragment2
private lateinit var fragment3: Fragment3
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*------------------------------------------*/
progressBar = findViewById<ProgressBar>(R.id.progress_bar)
progressBar.max = 90
progressBar.progress = 30
/*------------------------------------------*/
fragment1 = Fragment1()
fragment2 = Fragment2()
fragment3 = Fragment3()
/*------------------------------------------*/
fragment1.a1 = this
fragment2.a2 = this
/*------------------------------------------*/
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment1)
.commit()
}
/*--------------- Onclik interface implementation -------------------*/
override fun onClickFragment1() {
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment2)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit()
progressBar.incrementProgressBy(30)
}
override fun onClickFragment2() {
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment3)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit()
progressBar.incrementProgressBy(30)
}
/*------------------------------------------*/
override fun onBackPressed() {
super.onBackPressed()
if(progressBar.progress ==60 || progressBar.progress ==90) progressBar.incrementProgressBy(-30)
}
}
答案 1 :(得分:0)
尝试将您的代码放入onCreateView()方法中。 希望它能成功!
答案 2 :(得分:0)
尝试
parentmain_progress?.post({
parentmain_progress?.progress = 3
})
代替parentmain_progress?.setProgress(3)