如何在方向更改时保存ViewPager中的碎片状态

时间:2013-04-15 20:47:04

标签: android android-fragments android-fragmentactivity

我有一个实现ActionBarSherlock的应用程序,它主要由 ViewPager具有不同的片段内容(针对每个页面),带有ViewPager指示符。

这是我的onCreate方法,该活动扩展了SherlockFragmentActivity

// Assume that tabs,sources,types are string arrays e.g. tabs = ["tab1","tab2","tab3"]
// types = ["listview","listview","webview"]
// according to the type of each tab, the type of content of the fragment associated to that tab is determined (e.g. WebView or ListView)
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);

srcContext = getBaseContext();
srcActivity = SaudiActivity.this;

int selectPos = 0;


Intent sender = getIntent();

FragmentPagerAdapter mAdapter = new NewsListAdapter(
            getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(mAdapter);

TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(pager);
    pager.setOffscreenPageLimit(tabs.length);
    pager.setCurrentItem((tabs.length - 1));
    currentPosition = (tabs.length - 1);

    indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            currentPosition = position;
            if ((type[position] == "listview" || type[position]
                    .equals("listview")) && loaded[position] == false) {
                loaded[position] = true;
                getNews(listViews[position], position);
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

    getSupportActionBar().setCustomView(R.layout.logo);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(false);

    Context ctx = getSupportActionBar().getThemedContext();

    SourcesAdapter adapter = new SourcesAdapter(ctx,
    R.layout.navigation_list_item, src_name, icons, src_value);

}

这是我的新闻列表适配器(创建片段),

    public static class MyViewHolder {
    public TextView title;
    public ImageView icon;
}

class NewsListAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        return new NewsFragment(MyActivity.this, position);
    }

    // This is the number of pages -- 5
    @Override
    public int getCount() {
        return tabs.length;
    }

    // This is the title of the page that will apppear on the "tab"
    public CharSequence getPageTitle(int position) {
        return tabs[position];
    }

}

public List<NewsItem> NewsItems;

最后这是我的NewsFragment:

public static class NewsFragment extends Fragment {

    private int position;

    public NewsFragment() {
    }

    public NewsFragment(Context ctx, int pos) {
        this.position = pos;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,

    Bundle savedInstanceState) {
        View view = null;
        if (type[position].equals("listview")) {
            // Put a ListView in the Fragment
        } else {
            // Put a WebView in the Fragment
        }
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}

1 个答案:

答案 0 :(得分:2)

让片段通过重写onSaveInstanceState并将必要的值放入outState来处理它的状态。使用savedInstanceState在onCreateView中恢复该状态。另外我很确定如果你使用Actionbarsherlock片段需要扩展SherlockFragment而不是Fragment。