如何添加一个片段使用嵌套的片段(安卓4.2)一个ViewPager内 [英] How to add a Fragment inside a ViewPager using Nested Fragment (Android 4.2)

本文介绍了如何添加一个片段使用嵌套的片段(安卓4.2)一个ViewPager内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewPager 片段,每一个显示了列表(或电网)。

I have a ViewPager with three Fragments, each one shows a List (or Grid).

在新的 Android的API级别17 (果冻豆4.2)的特点之一是的Nested片段的。新功能介绍说:

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 片段(内用一个按钮例如),这样当用户preSS的按钮显示另一个片段不松 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布局。这是现在很简单,在由另一个片段主办的ViewPager显示片段。

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.

在code看起来在父片段是这样的:

The code looks something like this in a parent Fragment:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_parent, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
        mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    }

    public static class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public Fragment getItem(int position) {
            Bundle args = new Bundle();
            args.putInt(TextViewFragment.POSITION_KEY, position);
            return TextViewFragment.newInstance(args);
        }
    }

重要的是要使用 Fragment.getChildFragmentManager()实例化FragmentPagerAdapter时。还要注意的是,你不能使用 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.

来源$ C ​​$ C可被发现href="https://github.com/marcoRS/nested-fragments">https://github.com/marcoRS/nested-fragments

这篇关于如何添加一个片段使用嵌套的片段(安卓4.2)一个ViewPager内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆