Swipe issues with Two Viewpager and One Tab Layout

Mobile Technologies Mobile Computing 2 years ago

2 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ I have two viewpagers (one below the tabs another inside the collapsing toolbar) and a tab layout, all inside a fragment as shown in the below figure. My issue is, If I swipe a viewpager I want the other viewpager to swipe too (automatically) along with the tabs. I'm able to swipe the bottom viewpager and the tabs by using tabLayout.setupWithViewPager(mBottomViewPager); but can't swipe the top fragment inside the coordinator layout. I've tried tabLayout.setupWithViewPager(mBottomViewPager); but it only swipes the top view pager and the tabs. I'm only able to swipe one viewpager and the tabs but not both viewpagers and tabs. How can I solve this issue? My Fragment Layout:

Posted on 30 Aug 2022, this text provides information on Mobile Computing related to Mobile Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ mViewPager = (ViewPager) findViewById(R.id.container); mViewPager2 = (ViewPager) findViewById(R.id.viewpager); tabLayout = (TabLayout) findViewById(R.id.tabs); mViewPager.setAdapter(mSectionsPagerAdapter); tabLayout.setupWithViewPager(mViewPager); mViewPager2.setAdapter(mSectionsPagerAdapter1); mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { private int mScrollState = ViewPager.SCROLL_STATE_IDLE; @Override public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) { if (mScrollState == ViewPager.SCROLL_STATE_IDLE) { return; } mViewPager2.scrollTo(mViewPager.getScrollX(), mViewPager2.getScrollY()); } @Override public void onPageSelected(final int position) { } @Override public void onPageScrollStateChanged(final int state) { mScrollState = state; if (state == ViewPager.SCROLL_STATE_IDLE) { mViewPager2.setCurrentItem(mViewPager.getCurrentItem(), false); } } }); mViewPager2.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { private int mScrollState = ViewPager.SCROLL_STATE_IDLE; @Override public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) { if (mScrollState == ViewPager.SCROLL_STATE_IDLE) { return; } mViewPager.scrollTo(mViewPager2.getScrollX(), mViewPager.getScrollY()); tabLayout.setScrollPosition(mViewPager2.getCurrentItem(), 0f, true); } @Override public void onPageSelected(final int position) { } @Override public void onPageScrollStateChanged(int state) { mScrollState = state; if (state == ViewPager.SCROLL_STATE_IDLE) { mViewPager.setCurrentItem(mViewPager2.getCurrentItem(), false); } } }); public class SectionsPagerAdapter extends FragmentStatePagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). return PlaceholderFragment.newInstance(position + 1); } @Override public int getCount() { // Show 7 total pages. return 7; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "SECTION 1"; case 1: return "SECTION 2"; case 2: return "SECTION 3"; case 3: return "SECTION 4"; case 4: return "SECTION 5"; case 5: return "SECTION 6"; case 6: return "SECTION 7"; } return null; } } public class SectionsPagerAdapter1 extends FragmentStatePagerAdapter { ArrayList productList; public SectionsPagerAdapter1(FragmentManager fm, ArrayList productList) { super(fm); this.productList = productList; } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). return PlaceholderFragment1.newInstance(position, productList); } @Override public int getCount() { // Show 7 total pages. return productList.size(); } }

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Mobile Technologies Links