2014-10-10 89 views
1

請幫我使用我的代碼。我的滑動標籤的實現有什麼問題?

我正在使用android導航抽屜和滑動選項卡實現,類似於他們在android開發人員網站上有什麼。當我啓動應用程序並使用導航抽屜打開新聞片段時,它可以正常工作,如下圖所示。

News fragment working properly

但是當我使用抽屜式導航欄導航到另一個菜單,然後回到世界新聞和體育片段顯示內容爲空,如下圖所示。

News fragment not working properly

下面是新聞片段

public class NewsFragment extends Fragment { 

private FragmentActivity mActivity; 
private ViewPager mViewPager; 
private NewsTabsFragmentPagerAdapter mAdapter; 
private ActionBar mActionBar; 
// Tab titles 
private String[] mTabs = { "World", "Sports", "Entertainment" }; 

private static final String TAG = "NewsFragment"; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    Log.i(TAG, getClass().getSimpleName() + ":entered onCreateView()"); 

    return inflater.inflate(R.layout.news_fragment, 
      container, false); 

} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    Log.i(TAG, getClass().getSimpleName() + ":entered onActivityCreated()"); 
    super.onActivityCreated(savedInstanceState); 

    // Get a fragment activity instance that will be used by mAdapter and action bar 
    mActivity = (FragmentActivity) getActivity(); 

    // Create the adapter that will return a fragment for each of the three primary sections 
    // of the app. 
    mAdapter = new NewsTabsFragmentPagerAdapter(mActivity.getSupportFragmentManager()); 

    // Set up the action bar. 
    //final ActionBar actionBar = mActivity.getActionBar(); 
    mActionBar = mActivity.getActionBar(); 

    // Specify that the Home/Up button should not be enabled, since there is no hierarchical 
    //parent. Not used in this case since the activity implementing this fragment uses 
    //navigation drawer. 
    //actionBar.setHomeButtonEnabled(false); 
    //mActionBar.setHomeButtonEnabled(false); 

    // Specify that we will be displaying tabs in the action bar. 
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    //mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Set up the ViewPager, attaching the adapter and setting up a listener for when the 
    // user swipes between sections. 
    mViewPager = (ViewPager) mActivity.findViewById(R.id.pager); 
    mViewPager.setAdapter(mAdapter); 
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
     @Override 
     public void onPageSelected(int position) { 
      // When swiping between different app sections, select the corresponding tab. 
      // We can also use ActionBar.Tab#select() to do this if we have a reference to the 
      // Tab. 
      mActionBar.setSelectedNavigationItem(position); 
     } 
    }); 

    // Setup the tabListiner for the tabs. 
    ActionBar.TabListener tabListener = new ActionBar.TabListener() { 
     @Override 
     public void onTabSelected(Tab tab, FragmentTransaction ft) { 
      // When the tab is selected, switch to the 
      // corresponding page in the ViewPager. 
      mViewPager.setCurrentItem(tab.getPosition()); 

      Log.i(TAG, getClass().getSimpleName() + ":entered onTabSelected()"); 
     } 

     @Override 
     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     } 

     @Override 
     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     } 
    }; 

    if (mViewPager.getAdapter() != null) { 
     // For each of the news category in the app, add a tab to the action bar. 
     for (String tab_name : mTabs) { 
      mActionBar.addTab(mActionBar.newTab().setText(tab_name) 
        .setTabListener(tabListener)); 
     } 
    } 
} 

我的問題是,爲什麼是世界體育片段不是當我往返於新聞片段導航離去的顯示內容的代碼

快速的幫助是非常感謝。謝謝!

回答

0

只需將此mViewPager.setOffscreenPageLimit(3); 添加到代碼中即可。當你離開它們時,它會保存你的碎片的狀態