2014-01-30 214 views
5

我有一個viewpager在一個片段中,我顯示了3個片段,但是當我第一次打開片段時,它打開的很好,當我移動到另一個片段並返回到viewpager片段時,它不會在查看尋呼機中不顯示片段,然後我必須改變手機的方向,或者不斷刷看視圖,以顯示隱藏的片段。保留viewpager片段的狀態android

我用Google搜索它,現在我知道我要的標籤設置爲每個片段並用findfragmentbytag方法來檢索片段,但問題是,當我嘗試設置標籤片段

getSupportFragmentManager().beginTransaction().add(myFragment, "Some Tag").commit(); 

上面一行崩潰我應用程序和logcat顯示「不能設置標記到片段」,我知道我在做一些愚蠢的錯誤

這是我的片段的完整代碼。如果能指導我在哪裏編寫用於設置片段標籤的代碼,我將非常感激。提前致謝。

public class MatchesListingFragment extends Fragment implements ActionBar.TabListener { 

    Context context; 
    SectionsPagerAdapter mSectionsPagerAdapter; 
    ViewPager mViewPager; 
    ActionBar actionBar; 
    Matchlistings db; 
    Bundle savedInstanceState; 

    Fragment livescore,fixture,results; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     this.savedInstanceState = savedInstanceState; 
     db = new Matchlistings(getActivity()); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_matches, container, false); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     context = getActivity().getApplicationContext(); 

     if(savedInstanceState == null){ 
      livescore = new LiveScoreListingFragment(); 
      fixture = new FixturesListingFragment(); 
      results = new ResultsListingFragment(); 
     } 

     if(!Constants.is_listing_refreshed){ 
      Log.i(MainActivity.TAG,"refreshing list"); 
      db.loadListingsFromServer().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
     } 

     actionBar = getActivity().getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     if(mSectionsPagerAdapter == null) 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager()); 
     mViewPager = (ViewPager) getView().findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       actionBar.setSelectedNavigationItem(position); 
      } 
     }); 


     for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
      Tab tb = actionBar.newTab() 
      .setText(mSectionsPagerAdapter.getPageTitle(i)) 
      .setTabListener(this); 

      actionBar.addTab(tb); 

     } 
    } 

    public class SectionsPagerAdapter extends FragmentPagerAdapter { 
     Fragment fragment; 

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

     @Override 
     public Fragment getItem(int position) { 
      switch(position){ 
      case 0: 

       fragment = livescore; 
       return fragment; 
      case 1: 
       fragment = fixture; 
       return fragment; 
      case 2: 
       fragment = results; 
       return fragment; 
      case 3: 
       fragment = new LiveScoreListingFragment(); 
       return fragment; 
      } 
      return null; 
     } 

     @Override 
     public int getCount() { 
      return 4; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
      case 0: 
       return "LIVE"; 
      case 1: 
       return "FIXTURES"; 
      case 2: 
       return "RESULTS"; 
      case 3: 
       return "HOT"; 
      } 
      return null; 
     } 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     actionBar.removeAllTabs(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
    } 

    @Override 
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) { 
     // TODO Auto-generated method stub 

    } 

} 

enter image description here

編輯: 我現在已經試圖通過標籤來得到viewpager的片段,而不是創造新的片段,每次的。但問題仍然是一樣的。

FixturesListingFragment fixture = (FixturesListingFragment) getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.pager+":1"); 
       if(fixture == null) 
        fixture = new FixturesListingFragment(); 
+0

FragmentPagerAdapter使用標籤本身 – MGDroid

+0

我該如何使用它來獲取片段? –

回答

0

試試下面所提到保留實例:

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    //add this line 
    setRetainInstance(true); 

    context = getActivity().getApplicationContext(); 
+0

不能正常工作.... –

10

您是否嘗試過使用setOffscreenPageLimit?如果您有3個頁面,則將其設置爲2.即使應用處於閒置狀態,這種方式也不會重新創建頁面。這裏是link

+0

拋出空指針例外:S –

+0

在設置適配器後使用它,頁數爲4,您應該將其設置爲3的屏幕外頁限制。 – Angmar

+0

mViewPager.setAdapter(mSectionsPagerAdapter); mViewPager.setOffscreenPageLimit(3); 我做到了這一點,我仍然得到pullpointer更新問題,以顯示屏幕截圖... –

4

我已經改變FragmentPagerAdapter到FragmentStatePagerAdapter固定的問題... 這是我在這個link

/** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the 
    * three primary sections of the app. We use a {@link android.support.v4.app.FragmentPagerAdapter} 
    * derivative, which will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 

下載示例應用程序讀取和改變..但仍然不知道應該有辦法做到這一點與FragmentPagerAdapter

2

我已經解決了通過使用setOffScreenpageLimit(int)到我有的附加選項卡的數量的問題。

private void setupViewPager(ViewPager viewPager) { 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
    adapter.addFragment(new FragmentHome(), "Home"); 
    adapter.addFragment(new FragmentEProfile(), "e-Profile"); 
    adapter.addFragment(new FragmentResults(), "Results"); 
    adapter.addFragment(new FragmentOutpass(), "Outpass"); 
    viewPager.setAdapter(adapter); 
    viewPager.setOffscreenPageLimit(3); 
}