Question

I'm using PagerSlidingTabStrip library for ViewPager https://github.com/astuetz/PagerSlidingTabStrip. I want to change the fragments while scrolling. I achieved that with the below code.

MainActivity.java

public class MainActivity extends SherlockFragmentActivity {

private MyPagerAdapter adapter;
ViewPager pager;
private PagerSlidingTabStrip tabs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    pager = (ViewPager) findViewById(R.id.pager);
    adapter = new MyPagerAdapter(getSupportFragmentManager());

    pager.setAdapter(adapter);

    final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
            .getDisplayMetrics());
    pager.setPageMargin(pageMargin);

    tabs.setViewPager(pager);

}

public class MyPagerAdapter extends FragmentPagerAdapter {
    private final String[] TITLES = {"First","Second","Third"};
    private SherlockFragment[] fragments = new SherlockFragment[] { new FirstFragment(), new SecondFragment(), new ThirdFragment()};

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

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments[position];
    }

}
}

FirstFragment.java

public class FirstFragment extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.fragment_main, container, false);
    Toast.makeText(getSherlockActivity(), "This is from first", Toast.LENGTH_SHORT).show();
    return v;
}
}

SecondFragment.java

public class SecondFragment extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.fragment_main, container, false);
    Toast.makeText(getSherlockActivity(), "SecondFragment", Toast.LENGTH_SHORT).show();
    return v;
}
}

ThirdFragment.java

public class ThirdFragment extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.fragment_main, container, false);
    Toast.makeText(getSherlockActivity(), "ThirdFragment", Toast.LENGTH_SHORT).show();
    return v;
}
}

I got the three fragments and I'm testing them with Toasts.

When the application opens with the MainActivity, FirstFragment is attached to the Activity , but it shows two toasts, one from Firstragment and other from SecondFragment , and when I scroll to second tab, it shows the toast of ThirdFragment.

So, I figured its going like this. If I scroll from left to right , the fragment right to the current fragment is displayed and if I scroll from right to left, the fragment left to the current fragment is displayed. Please help.

Thanks.

Was it helpful?

Solution

It's because of fragments caching. When you visit some fragments, sibling fragments to it also being created (onCreate() called).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top