gpt4 book ai didi

android-studio - 向Android Developer Studio的选项卡式事件模板添加片段

转载 作者:行者123 更新时间:2023-12-02 13:42:19 24 4
gpt4 key购买 nike

初学者在这里。使用Android Developer Studios标签式 Activity 模板创建应用。我有两个xml,即模板要包含的生成的fragment_main.xml和要成为第二个选项卡的secondtab.xml。

这是模板生成的代码:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
val viewPager: ViewPager = findViewById(R.id.view_pager)
viewPager.adapter = sectionsPagerAdapter
val tabs: TabLayout = findViewById(R.id.tabs)
tabs.setupWithViewPager(viewPager)
val fab: FloatingActionButton = findViewById(R.id.fab)

fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}

我一直在阅读ViewPager文档,而我发现很多都是从头开始做的,而且我很难理解。我觉得我缺少明显的东西。如何将我的secondtab.xml添加到此预生成的模板中,以便在用户单击第二个选项卡时显示它?

最佳答案

您的SectionsPagerAdapter getItem函数应如下所示:

class SectionsPagerAdapter(
private val context: Context,
fm: FragmentManager
) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

override fun getItem(position: Int) = when (position) {
0 -> FirstTabFragment()
1 -> SecondTabFragment()
else -> error("Unknown")
}

override fun getPageTitle(position: Int) = context.resources.getString(TAB_TITLES[position])

override fun getCount() = 2
}

因为默认情况下,此模板中的 SectionsPagerAdapter始终创建PlaceholderFragment:
 override fun getItem(position: Int): Fragment {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1)
}

但是在您的情况下,您需要使用两个不同的片段以及两个不同的xml布局,如下所示:
class FirstTabFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_main, container, false)
val textView: TextView = root.findViewById(R.id.section_label)
return root
}
}

class SecondTabFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.secondtab, container, false)
val textView: TextView = root.findViewById(R.id.section_label)
return root
}
}

还请确保添加:
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />

到片段xml

关于android-studio - 向Android Developer Studio的选项卡式事件模板添加片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62312662/

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