2013-04-15 66 views
0

我有一個實現ActionBarSherlock的應用程序,它主要由具有不同內容的片段(每個頁面)的 ViewPager和ViewPager指示器組成。如何在方向更改時保存ViewPager中的片段狀態

這裏是一個擴展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); 

} 

這裏是我的新聞列表適配器(創建的片段)的活動我的onCreate方法,

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

回答

2

讓片段通過覆蓋onSa來處理它的狀態veInstanceState並將必要的值放入outState。使用savedInstanceState恢復onCreateView中的狀態。另外我很確定,如果你使用Actionbarsherlock片段需要擴展SherlockFragment而不是Fragment。

相關問題