gpt4 book ai didi

android - 如何在 Kotlin 的 fragment 内使用按钮在 fragment 之间切换?

转载 作者:行者123 更新时间:2023-11-29 22:35:50 25 4
gpt4 key购买 nike

我希望我不是在问一个重复的问题,而是只在 Java 中找到了这个答案并且我使用的是 Kotlin。我正在尝试使用按钮在 fragment 之间进行交换。这是我的代码。首先,谢谢。

对于主要

       class MainActivity : AppCompatActivity (){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

及其xml文件

           <fragment
android:id="@+id/main"
android:name="com.example.stackoverflow.fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_fragment1"/>
</LinearLayout>

对于 fragment 1

      class fragment1 : Fragment(){
override fun onCreateView(Layoutinflater: LayoutInflater,
container: ViewGroup?, savedInstantState: Bundle?): View? {
return Layoutinflater.inflate(R.layout.fragment_fragment1, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button1.setBackgroundColor(Color.parseColor("#faaf45"))
button1.setOnClickListener(){
//Not sure what to put here
}
}

和 fragment 2

       class fragment2 : Fragment(){
override fun onCreateView(Layoutinflater: LayoutInflater,
container: ViewGroup?, savedInstantState: Bundle?): View? {
return Layoutinflater.inflate(R.layout.fragment_fragment2, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button2.setBackgroundColor(Color.parseColor("#aaaf45"))
button2.setOnClickListener(){
//Not sure what to put here
}
}

最佳答案

在主 Activity 中:

class MainActivity : AppCompatActivity (){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

fun changeFragment(fragmnet:Fragment){
getFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
}

在 MainActivity 布局中:

     <FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

在 fragment 1中:

class fragment1 : Fragment(){
override fun onCreateView(Layoutinflater: LayoutInflater,
container: ViewGroup?, savedInstantState: Bundle?): View? {
return Layoutinflater.inflate(R.layout.fragment_fragment1, container,false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button1.setBackgroundColor(Color.parseColor("#faaf45"))
button1.setOnClickListener(){
(context as MainActivity).changeFragment(fragmnet2.newInstance())
}
companion object {
fun newInstance(): fragment1 {
return fragment1()
}
}
}

在 fragment 2中:

class fragment2 : Fragment(){
override fun onCreateView(Layoutinflater: LayoutInflater,
container: ViewGroup?, savedInstantState: Bundle?): View? {
return Layoutinflater.inflate(R.layout.fragment_fragment2, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button2.setBackgroundColor(Color.parseColor("#aaaf45"))
button2.setOnClickListener(){
(context as MainActivity).changeFragment(fragmnet1.newInstance())
}

companion object {
fun newInstance(): fragment2 {
return fragment2()
}
}
}

关于android - 如何在 Kotlin 的 fragment 内使用按钮在 fragment 之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59516016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com