2

enter image description here

Hi, I have been trying to use swipe tab with bottom navigation bar, but on re-selection of tab swipe lacks and nothing shows. I am calling fragments of swipe tabs from bottom bar fragment. This is my code

    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;
    }
}
Jenis Kasundra
  • 583
  • 9
  • 19
Vishal Arora
  • 81
  • 1
  • 1
  • 12

3 Answers3

4

Use getChildFragmentManager() instead of getFragmentManager() . See viewpager didnt show anything after change bottom navigation menu . I've tried it and it work perfectly

Community
  • 1
  • 1
alhamwa
  • 79
  • 5
0

Try with this

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

It there is parent view pager for bottom navigation then you can also use this

parrentViewPager.setOffscreenPageLimit(4);
Mayur Raval
  • 3,250
  • 6
  • 34
  • 57
  • 1
    @VishalArora, Can you try with this https://github.com/ncapdevi/FragNav ?I think it may help – Mayur Raval Jan 17 '17 at 06:17
  • I am using this Fragnav code and it works fine, but issue is when i try to re-call tabs fragment from Fragnav fragment it stops working. Just want to tell you, i am not using fragment activity. – Vishal Arora Jan 17 '17 at 06:39
  • @VishalArora, can you try to debug the stack of the current tab which is you selected from FragNav? – Mayur Raval Jan 17 '17 at 06:51
  • I have enabled debugging on current tab and this is the result. No adapter attached; skipping layout mName=null mIndex=-1 mCommitted=false – Vishal Arora Jan 17 '17 at 07:07
0

I had that problem too. My problem was that I had two ViewPagers in two different tabs of BottomNavigation and they both contained ViewPager in layout:

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

After I change the ID of one of them to differentiate, it worked!

 <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" />
Amiraslan
  • 794
  • 1
  • 8
  • 19