0

My app uses 2 identical ViewPagers that contain 6 images in each of their 6 Fragments.

enter image description here The leftViewPager's OnPageChangeListener swipe is used to control the rightViewPager.

As I swipe through the 6 leftViewpager fragments the rightViewpager just rotate between the first 2 Fragments back and forth!

ViewPager only displays 2 out of 6 child Fragmentshas I understand this is due to Android loading the current and next Fragment in the viewPager, but when I swipe the rightViewpager manually (Just to check) all 6 Fragments are there!!

So the Question: How can I get all 6 Fragments in Vp2 by swiping Vp1?? As the Hierachy Viewer shows just 2 Fragments. Or Do I need some special trickery to re-use/recycle the two fragments?

Click link to animated gif illustrating issue

  •      // Set View pager on page changed listner
        first_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
            @Override
            public void onPageSelected(int position) {
            //second_pager.setCurrentItem(position); //Jerky
                Toast.makeText(MainActivity.this, 
                        "Selected page position: " + position, Toast.LENGTH_SHORT).show();
    
            }
    
            @Override
            public void onPageScrolled(int position , float positionOffset , int positionOffsetPixels) {
            if(positionOffsetPixels>0)
            {
            second_pager.scrollTo(positionOffsetPixels-second_width  ,0); // 2/3 vers
            //second_pager.scrollTo(positionOffsetPixels-second_width + positionOffsetPixels ,0);   
    
            }
    
            }
    
    
            @Override
            public void onPageScrollStateChanged(int position) {
    
            }
    
        });
    

MyAdapter uses

public class SecondPageAdapter extends FragmentPagerAdapter  {


public SecondPageAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

@Override
public int getCount() {
    // Return how many Fragment's attached on View Pager
    return 6;
}

@Override
public Fragment getItem(int position) {
    Log.i("First", "postion="+position);
    switch (position) {
    case 0: // Fragment # 0 - This will show FirstFragment
        return Fragment1.init(position);
    case 1: // Fragment # 1 - This will show SecondFragment
        return Fragment2.init(position);
    case 2: // Fragment # 0 - This will show FirstFragment
        return Fragment3.init(position);
    case 3: // Fragment # 1 - This will show SecondFragment
        return Fragment4.init(position);
    case 4: // Fragment # 0 - This will show FirstFragment
        return Fragment5.init(position);
    case 5: // Fragment # 1 - This will show SecondFragment
        return Fragment6.init(position);
    default:// Fragment # default - Will show FirstFragment
        return null;
    }
}


public int getItemPosition(Object object) {
    return POSITION_NONE;
}



@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return super.saveState();

}

@Override
public void notifyDataSetChanged() {
    // TODO Auto-generated method stub
    super.notifyDataSetChanged();
    Log.d("DataSetChanged", "notifyDataSetChanged=");
}

}

1 Answers1

0
@Override
    public int getCount() {
        return 6;
    }

In your FragmentPagerAdapter and return number of pages you want to display

Joren
  • 3,068
  • 25
  • 44
koutuk
  • 832
  • 1
  • 8
  • 17
  • I already did this. See the animated gif link in this post http://2imgs.com/a10f7a5083 and you'll see its working but not by swiping the LeftHandViewPager onPageChangeListener. I suspect this will take some more powerful trickery! –  Jan 21 '15 at 13:15
  • so you want to handle right swipe pager from left swipe pager – koutuk Jan 22 '15 at 05:41