gpt4 book ai didi

android - 如何单击 fragment 中的按钮以切换到 Kotlin 中的其他选项卡?

转载 作者:行者123 更新时间:2023-11-29 18:37:34 25 4
gpt4 key购买 nike

以下代码基于 Android Studio Wizard 3.2.1 用于创建 Tabbed Activity。

PlaceholderFragment1上有一个button1连接Tab1,在PlaceholderFragment2上有一个button2连接Tab2

我希望点击button1切换到Tab2,点击button2切换到Tab1,怎么办?

顺便说一句,我已经阅读了How to change tab on button click in Android?How to programmatically switch tabs using buttonclick in Android

代码

class MainActivity : AppCompatActivity() {

private var mSectionsPagerAdapter: SectionsPagerAdapter? = null

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

mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)


container.adapter = mSectionsPagerAdapter

container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))

}


inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

override fun getItem(position: Int): Fragment {
val fragment: Fragment
when (position) {
0 -> fragment = PlaceholderFragment1() //Tab1
1 -> fragment = PlaceholderFragment2() //Tab2
else -> throw IllegalArgumentException("Invalid section number")
}
return fragment
}

override fun getCount(): Int {
return 2
}
}


class PlaceholderFragment1 : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_main1, container, false)
return rootView
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
button1.setOnClickListener {
//Switch to Tab2
}
}
}


class PlaceholderFragment2 : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_main2, container, false)
return rootView
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
button2.setOnClickListener {
//Switch to Tab1
}
}
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">


<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_1"/>

<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_2"/>

</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


</android.support.design.widget.CoordinatorLayout>

fragment_main1.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>


<Button
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"/>
</android.support.constraint.ConstraintLayout>

我的方式

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
...

setControl(view)
}


private fun setControl(view: View){
button1.setOnClickListener {
var viewPager=view.parent as ViewPager
viewPager.setCurrentItem(1,true)
}
}

最佳答案

不需要手动绑定(bind)这些事件...

您只需使用 Viewpager 设置 TabLayout:

container.adapter = mSectionsPagerAdapter
tabs.setupWithViewPager(container);

然后这些事件将由框架以一种相当方便的方式处理。

PS:containerViewPager 的一个相当不幸的变量名。

关于android - 如何单击 fragment 中的按钮以切换到 Kotlin 中的其他选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53682485/

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