0

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

这是我扩展 SherlockFragmentActivity 的 Activity 的 onCreate 方法

// 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);
    }
}
4

1 回答 1

2

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

于 2013-04-16T00:03:47.807 回答