使用 Kotlin 为跨片段 android 的进度条设置进度条
Setprogress for a progress bar across fragments android using Kotlin
我对 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()
}
}
}
感谢任何帮助。
尝试将您的代码放在 onCreateView() 方法中。
希望有用!!
尝试
parentmain_progress?.post({
parentmain_progress?.progress = 3
})
而不是parentmain_progress?.setProgress(3)
像那样?
在这个例子中我使用了新的包结构:Androidx
如果您还没有使用它,只需更改
import androidx.fragment.app.FragmentTransaction
来自
import android.app.FragmentTransaction
您将在这笔存款中找到代码:
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)
}
}
我对 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()
}
}
}
感谢任何帮助。
尝试将您的代码放在 onCreateView() 方法中。 希望有用!!
尝试
parentmain_progress?.post({
parentmain_progress?.progress = 3
})
而不是parentmain_progress?.setProgress(3)
像那样?
在这个例子中我使用了新的包结构:Androidx
如果您还没有使用它,只需更改
import androidx.fragment.app.FragmentTransaction
来自
import android.app.FragmentTransaction
您将在这笔存款中找到代码:
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)
}
}