Home » Android » android – Circular ViewPager. Fragments don't work as they supposed to after first round

android – Circular ViewPager. Fragments don't work as they supposed to after first round

Posted by: admin May 14, 2020 Leave a comment

Questions:

Ok, so i needed circular ViewPager. I was having really hard time to implement it. Now that i have implemented it and it is working fine as far as circular scroll is concern. But i have notice a problem.Which is, after first round of scroll fragments don’t work. I have three Fragments with a button inside them. Button works in first round but when i come back to first page button doesn’t work.
p.s i used fake pages technique to make viewpager circular.

Here is code

        mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), this));
    //mViewPager.setOnPageChangeListener(new CircularViewPagerHandler(mViewPager));
    mViewPager.setCurrentItem(1, false);
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                mViewPager.setCurrentItem(3, false);
                Log.d("TAG",
                        "Swiped before first page, looping and resetting to last page.");
            } else if (position == 4) {
                mViewPager.setCurrentItem(1, false);
                Log.d("TAG",
                        "Swiped beyond last page, looping and resetting to first page.");
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            Log.d("onPageScrollState", "state:" + state);
            int currentPage = mViewPager.getCurrentItem();
            if(currentPage==1){
                imgCenter.setImageResource(R.drawable.take_ride);
                imgLeft.setImageResource(R.drawable.give_ride);
                imgRight.setImageResource(R.drawable.setting);
            }
            if(currentPage==2){
                imgCenter.setImageResource(R.drawable.setting);
                imgRight.setImageResource(R.drawable.take_ride);
                imgLeft.setImageResource(R.drawable.give_ride);
            }
            if (currentPage==3){
                imgCenter.setImageResource(R.drawable.give_ride);
                imgLeft.setImageResource(R.drawable.take_ride);
                imgRight.setImageResource(R.drawable.setting);

            }
        }
    });

here is PagerAdapter class

  public class PagerAdapter extends FragmentPagerAdapter {

Context mcontext;

public PagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    mcontext = context;
}

@Override
public Fragment getItem(int position) {
    if (position == 0) {
        return new HomeFragment2();
    }
    else if(position==1){
        return new HomeFragment();
    }
    else if (position == 2) {
        return new SettingFragment();
    }
    else if(position==3){
        return new HomeFragment2();
    }
    else
        return new HomeFragment();
}

@Override
public int getCount() {
    return 5;
}
}
How to&Answers:

I’ve created a simple test-app with you PagerAdapter and simple Fragments with a Button:

enter image description here

And it works fine!

I’ve uploaded the source code here, so you can check it out and see if there’s any difference with yours.

As a Fragment‘s I used:

public class FragmentA extends android.support.v4.app.Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_a, container, false);
        rootView.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Hi, fragment A", Snackbar.LENGTH_SHORT).show();
            }
        });
        return rootView;
    }
}

So what I can recommend

  • replace your fragments with some dummy ones (like the one above). If it works – the issue is in the Fragment code, not in the Circular ViewPager

  • If it still doesn’t work – I’d try to instead of re-creating fragments multiple times – create them once and store:

     public class PagerAdapter extends FragmentPagerAdapter {
        Context mcontext;
        Fragment [] fragments;
        public PagerAdapter(FragmentManager fm, Context context, Fragment [] fragments) {
            super(fm);
            mcontext = context;
            this.fragments = fragments;
        }
    
        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }
    
        @Override
        public int getCount() {
            return fragments.length;
        }
    }
    

    And in Activity:

    Fragment[] fragments = {
            Fragment.instantiate(this, FragmentC.class.getName()),
            Fragment.instantiate(this, FragmentA.class.getName()),
            Fragment.instantiate(this, FragmentB.class.getName()),
            Fragment.instantiate(this, FragmentC.class.getName()),
            Fragment.instantiate(this, FragmentA.class.getName()),
    };
    .....
    viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), this, fragments));
    

Let me know, if it helps!