1

I'm trying to create a calendar application. Each month is one Fragment. I'm using ViewPager and FragmentPageAdapter. I want to add one Fragment when swiping to the left and position is 2 and I want to add one Fragment when swiping to the right and position is fragments.size() - 2.

I have no problem to add Fragment to the right but the problem occurs when adding to 0 position because of Can't change tag of fragment exception. I was looking to the solution to the problem with no success. I'm wondering if there is some alternative to ViewPager or another solution to this problem.

Here is my adapter:

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragments = new ArrayList<>();

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

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

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

    public void addFragment(Fragment fragment) {
        fragments.add(fragment);
        notifyDataSetChanged();
    }

    public void addFragment(Fragment fragment, int position) {
        fragments.add(position, fragment);
        notifyDataSetChanged();
    }

    public void removeFragment(int position) {
        fragments.remove(position);
        notifyDataSetChanged();
    }

    public void removeFragment(Fragment fragment) {
        fragments.remove(fragment);
        notifyDataSetChanged();
    }
}
Community
  • 1
  • 1
miskohut
  • 957
  • 1
  • 14
  • 34

0 Answers0