简体   繁体   English

查看片段中的分页器

[英]View Pager within a fragment

I have bottom navigation bar implemented with view pager. 我有底部的导航栏与视图寻呼机实现。 Now on the first fragment I want tabs on the top so I need a view pager inside of that fragment only. 现在在第一个片段上,我想在顶部显示标签,因此我只需要在该片段内使用一个视图分页器。 That view pager will have its own set of fragments. 该视图分页器将具有其自己的片段集。 Is it possible? 可能吗? Can some one help me do it? 有人可以帮我吗?

Yes, it is completely possible since your inner viewpager and tablayout will be independent from its parent fragment. 是的,这是完全可能的,因为您的内部viewpager和tablayout将独立于其父片段。 The process to achieve this is exactly the same you used to create your first set of tabs, but you have to use getChildFragmentManager() instead: 实现此过程的过程与创建第一组选项卡的过程完全相同,但必须使用getChildFragmentManager()代替:

getChildFragmentManager().beginTransaction().add(R.id.yourinnercontainer, YourFragment.newInstance()).commit();
getChildFragmentManager().executePendingTransactions();

And for the TabLayout just create your views and use the setupWithViewPager(viewPager) function as usual. 对于TabLayout,只需创建视图并照常使用setupWithViewPager(viewPager)函数即可。

Yes you can add viewPager to the XML of your fragment like this: 是的,您可以像这样将viewPager添加到片段的XML中:

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="ltr"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:contentInsetEnd="16dp"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start|center_vertical"
            android:gravity="center|start"
            android:text="@string/app_name"
            android:textAppearance="@style/TextAppearance.AppCompat.Title"/>
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:tabIndicatorHeight="3dp"
        app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

and manage it in your code like this: 并按如下方式在您的代码中进行管理:

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
    mAdapter = new MyPagerAdapter(getSupportFragmentManager());

    mAdapter.addFragment(first_fragment, firts_fragment_title);
    mAdapter.addFragment(second_fragment, second_fragment_title);
    pager.setAdapter(mAdapter);

    TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
    tabs.setupWithViewPager(pager);
    TextView first_fragment_tab = (TextView) getLayoutInflater().inflate(R.layout.tab_indicator, null);
    TextView second_fragment_tab = (TextView) getLayoutInflater().inflate(R.layout.tab_indicator, null);

    tabs.getTabAt(0).setCustomView(first_fragment_tab);
    tabs.getTabAt(1).setCustomView(second_fragment_tab);
    pager.setCurrentItem(mAdapter.getCount());

Nested Fragment is possible. 嵌套Fragment是可能的。 But Fragment tends to bring new sets of troubles, so with integration of another Fragment layer on top of it, we might end up in deep trouble. 但是Fragment往往会带来新的麻烦,因此,如果在其上面集成另一个Fragment层,我们可能最终会陷入麻烦。 If you are performing basic operations, it might be OK but if there is cases of data exchanges and other inter fragments dependency, I would suggest to be very careful. 如果您正在执行基本操作,则可以,但是如果存在数据交换和其他片段间相关性的情况,我建议您要非常小心。

Yes, it is possible! 对的,这是可能的!

Step 1: Create Tabbed activity. 步骤1:创建选项卡式活动。 (It will automatically create an activity with ViewPager) (它将使用ViewPager自动创建活动)

在此处输入图片说明

Step 2: Convert that activity to a fragment to be able add it to your BottomNavigation. 步骤2:将该活动转换为片段,然后将其添加到您的BottomNavigation中。 Use this answer: Converting activity to fragment 使用以下答案: 将活动转换为片段

Note: I did this in a project and having other ViewPager s on other tabs of BottomNavigation bar made every a bit more complicated. 注意:我是在项目中完成此操作的,并且在BottomNavigation栏的其他选项卡上使用了其他ViewPager ,这使一切变得更加复杂。 Using viewPager.setOffscreenPageLimit(...); 使用viewPager.setOffscreenPageLimit(...); will solve some of your possible problem. 将解决您的一些可能的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM