Android:使用ViewPager更改片段

时间:2013-06-07 20:49:46

标签: android android-fragments android-viewpager pagerslidingtabstrip

我正在为PagerSlidingTabStrip https://github.com/astuetz/PagerSlidingTabStrip使用ViewPager库。我想在滚动时更改片段。我用以下代码实现了这一点。

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;
}
}

我得到了三个片段,我正在用Toasts测试它们。

当应用程序打开MainActivity时,FirstFragment附加到Activity,但它显示两个Toast,一个来自Firstragment,另一个来自SecondFragment,当我滚动到第二个选项卡时,它显示了ThirdFragment的toast

所以,我认为它是这样的。如果我从左向右滚动,则会显示当前片段的右侧片段,如果我从右向左滚动,则会显示留给当前片段的片段。请帮忙。

感谢。

1 个答案:

答案 0 :(得分:2)

这是因为片段缓存。当你访问一些片段时,也会创建它的兄弟片段(onCreate()调用)。