如何使用底部导航的可跳过选项卡

时间:2017-01-17 05:53:58

标签: android android-fragments android-bottomnav

enter image description here

嗨,我一直在尝试使用带有底部导航栏的滑动选项卡,但是重新选择选项卡滑动缺少并且没有任何显示。我正在从底栏片段调用滑动标签片段。这是我的代码

    public class WalletFragment extends Fragment {
    private ViewPager viewPager;
    private WalletTabsAdapter mAdapter;
    PagerSlidingTabStrip wallettabs;
    // Tab titles

    public static WalletFragment newInstance(int instance) {
        Bundle args = new Bundle();
        args.putInt("input", instance);
        WalletFragment fragment = new WalletFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_wallet, container, false);
        // Initilization
        viewPager = (ViewPager) rootView.findViewById(R.id.walletpager);
        wallettabs = (PagerSlidingTabStrip) rootView.findViewById(R.id.wallettabs);
        mAdapter = new WalletTabsAdapter(getFragmentManager());
        viewPager.setAdapter(mAdapter);
        wallettabs.setViewPager(viewPager);
        /**
         * on swiping the viewpager make respective tab selected
         * */
        return rootView;
    }
}

3 个答案:

答案 0 :(得分:2)

使用getChildFragmentManager()代替getFragmentManager()。见viewpager didnt show anything after change bottom navigation menu 。我已经尝试过了,它完美地运作了

答案 1 :(得分:0)

试试这个

viewPager = (ViewPager) rootView.findViewById(R.id.walletpager);
    wallettabs = (PagerSlidingTabStrip) rootView.findViewById(R.id.wallettabs);
    mAdapter = new WalletTabsAdapter(getFragmentManager());
    viewPager.setAdapter(mAdapter);
    wallettabs.setViewPager(viewPager);
    viewPager.setOffscreenPageLimit(2); // add this line

底部导航有父视图寻呼机,您也可以使用此

parrentViewPager.setOffscreenPageLimit(4);

答案 2 :(得分:0)

我也有这个问题。我的问题是我在BottomNavigation的两个不同标签中有两个ViewPager,它们都在布局中包含ViewPager:

 <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" />

在我更改其中一个的ID以区分后,它工作了!

 <android.support.v4.view.ViewPager
        android:id="@+id/container2" <!-- Changed ID -->
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />