How to add a Fragment inside a ViewPager using Nested Fragment (Android 4.2)(如何使用嵌套片段(Android 4.2)在 ViewPager 中添加片段)
问题描述
我有一个 ViewPager
,其中包含三个 Fragments
,每个片段显示一个 List
(或 Grid
).
I have a ViewPager
with three Fragments
, each one shows a List
(or Grid
).
在新的 Android API 级别 17
(Jelly Bean 4.2)中,其中一项功能是 嵌套片段.新的功能描述说:
In the new Android API level 17
(Jelly Bean 4.2), one of the features is Nested Fragments. The new functionality description says:
如果您使用 ViewPager 创建左右滑动的片段占用大部分屏幕空间,您现在可以插入片段进入每个片段页面.
if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page.
所以,如果我理解正确,现在我可以在里面创建一个 ViewPager
和 Fragments
(例如里面有一个按钮),当用户按下按钮时显示另一个Fragment
不松散ViewPager
使用这个新功能.
So, if I understand right, now I can create a ViewPager
with Fragments
(with a button inside for example) inside, and when user press the button show another Fragment
without loose the ViewPager
using this new feature.
我花了一上午的时间尝试以几种不同的方式实现它,但我无法让它工作......有人可以添加一个如何实现它的简单示例吗?
I've expended my morning trying to implement this several different ways, but I can´t get it working... Can somebody add a simple example of how to implement this?
PS:我只对这种方式感兴趣,用 getChildFragmentManager
来了解如何工作.
PS: I'm only interested in doing at this way, with getChildFragmentManager
to learn how works.
推荐答案
假设您已经创建了正确的 xml 布局.现在在另一个 Fragment 托管的 ViewPager 中显示 Fragment 非常简单.
Assuming you have created the correct xml layouts. It is now very simple to display fragments in a ViewPager that is hosted by another Fragment.
以下是显示子片段的父片段:
The following is a parent fragment that displays child fragments:
class ParentViewPagerFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root = inflater.inflate(R.layout.fragment_parent_viewpager, container, false)
val viewPager = root.findViewById(R.id.viewPager) as ViewPager
// Important: Must use the child FragmentManager or you will see side effects.
viewPager.adapter = MyAdapter(childFragmentManager)
val tabStrip = root.findViewById<TabLayout>(R.id.pagerTabStrip)
tabStrip.setupWithViewPager(viewPager)
return root
}
class MyAdapter internal constructor(fm: FragmentManager) : FragmentPagerAdapter(fm) {
override fun getCount(): Int = 4
override fun getItem(position: Int): Fragment {
val args = Bundle().apply { putInt(ChildFragment.POSITION_KEY, position) }
return ChildFragment.newInstance(args)
}
override fun getPageTitle(position: Int): CharSequence = "Tab $position"
}
companion object {
val TAG: String = ParentViewPagerFragment::class.java.name
}
}
在实例化 FragmentPagerAdapter 时使用 Fragment.getChildFragmentManager() 很重要.另请注意,您不能在子片段上使用 Fragment.setRetainInstance() ,否则会出现异常.为简洁起见,省略了导入.
It is important to use Fragment.getChildFragmentManager() when instantiating the FragmentPagerAdapter. Also note that you cannot use Fragment.setRetainInstance() on the children fragments or you'll get an exception. The imports were omitted for brevity.
源代码可以在以下位置找到: https://github.com/marcoRS/nested-fragments
这篇关于如何使用嵌套片段(Android 4.2)在 ViewPager 中添加片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用嵌套片段(Android 4.2)在 ViewPager 中添加片段
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- UITextView 内容插图 2022-01-01