TabLayout and ViewPager -Tabs doesn't work after swiping between sliding drawer items
Asked Answered
A

1

8

TabLayout and ViewPager - Click on tabs doesn't work after swiping between sliding drawer item.

I work on app that have Sliding drawer and Tablayout. I refer this tutorials For Sliding Drawer and For TabLayout.

Inside Sliding Drawer

  1. TaskList -->TabLayout(Tabs(Today,Monthly,Yearly))
  2. Settings
  3. Orders

I have issue between Sliding Drawer and Tablayout. first time viewpager loads correctly. but when i move to next drawer item like (Settings Fragment) after that back to the TaskList tab fragment viewpager cant load any fragment,when swipe that pager then load few fragment.

Here Java Code

public class TaskListFragment extends Fragment {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

public TaskListFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_my_page, container, false);

    viewPager = (ViewPager)rootView.findViewById(R.id.viewpager);
    tabLayout = (TabLayout)rootView.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    // Inflate the layout for this fragment
    return rootView;
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
    adapter.addFragment(new Mypage_TodayFragment(), "TODAY");
    adapter.addFragment(new Mypage_TodayFragment(), "WEEKLY");
    adapter.addFragment(new Mypage_TodayFragment(), "MONTHLY");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
} 

Here .XML Code

   <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="262dp"
        android:id="@+id/view"
        android:layout_weight="0.16">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabBackground="@drawable/mytabselector"
            app:tabIndicatorColor="@color/Orange" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

       </android.support.design.widget.CoordinatorLayout>

Thanks in Advance!!

Allot answered 1/12, 2015 at 11:26 Comment(1)
Use FragmentStatePagerAdapter instead of FragmentPagerAdapterSunderland
R
16

Try this instead of

ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());

do

ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

This is because since you have a viewpager in a fragment, the fragment manager that this viewpager should be using to make fragment transactions should be a childfragmentmanager

Ric answered 1/12, 2015 at 11:35 Comment(2)
Thanks a lot !! its work for me...tell me one another thing getFragmentManager() not work in AppCompatActivity ???Allot
since you are loading fragments into a fragment (i.e your view pager loads fragments, and your view pager itself is a separate fragment) you need to use the childfragmentmanager to perform fragment transactions. You see activity is the parent and its fragments are its children, and children cannot use the fragmentManager, fragmentManager is for activity, children should use the childFragmentManagerRic

© 2022 - 2024 — McMap. All rights reserved.