2016-01-09 130 views
0

我有一個ViewPager設置,它使用片段顯示不同的圖標,我可以循環瀏覽以便用戶可以導航不同的菜單選項。Android ViewPager在從backStack返回片段後無法正常顯示

在ViewPager每個項目被容納在一個片段,像這樣:

public static Fragment newInstance(MainHandheldActivity context, int pos) 
{ 
    Bundle b = new Bundle(); 
    b.putInt("pos", pos); 
    return Fragment.instantiate(context, ObjectFragment.class.getName(), b); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, 
         ViewGroup container, Bundle savedInstanceState) { 

    // Get menu image icons 
    int icons[] = new int[3]; 
    icons[0] = R.mipmap.ic_text; 
    icons[1] = R.mipmap.ic_voice; 
    icons[2] = R.mipmap.ic_mail; 

    // Get root view of the fragment layout 
    LinearLayout rootView = (LinearLayout) inflater.inflate(
      R.layout.fragment_collection_object, container, false); 
    Bundle args = getArguments(); 
    int pos = args.getInt("pos"); 

    // Set the current menu image and text 
    ImageView menuImg = (ImageView) rootView.findViewById(R.id.fragment_image); 
    menuImg.setImageResource(icons[pos]); 

    TextView menuText = (TextView) rootView.findViewById(R.id.menuTxt); 
    switch (pos){ 
     case 0: 
      menuText.setText(MENU_HEADER_TEXT); 
      break; 
     case 1: 
      menuText.setText(MENU_HEADER_VOICE); 
      break; 
     case 2: 
      menuText.setText(MENU_HEADER_MAIL); 
      // TODO Get number of messages and concatenate to string eg Inbox (3) 
      break; 
    } 

    // Set the current menu selection 
    MainActivityInterface ai = (MainActivityInterface) getActivity(); 
    ai.setCurrentSelection(pos); 
    return rootView; 
} 

這些片段被用於通過一個頁面適配器像這樣:

public class CollectionPageAdapter extends FragmentPagerAdapter implements 
     ViewPager.OnPageChangeListener { 

    private MainHandheldActivity context; 
    private FragmentManager fm; 

    public CollectionPageAdapter(MainHandheldActivity context, FragmentManager fm) { 
     super(fm); 
     this.fm = fm; 
     this.context = context; 
    } 

    @Override 
    public Fragment getItem(int position) 
    { 
     position = position % MainHandheldActivity.PAGES; 
     Log.d("POS", "POSITION: " + position); 
     return ObjectFragment.newInstance(context, position); 
    } 

    @Override 
    public int getCount() 
    { 
     return MainHandheldActivity.PAGES * MainHandheldActivity.LOOPS; 
    } 
} 

和這是所有安裝在另一片段它將適配器設置在onCreateView方法中:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 

     // ... // 

     // Scrolling menu 
     pager = (ViewPager) view.findViewById(R.id.pager); 
     adapter = new CollectionPageAdapter((MainHandheldActivity)getActivity(),getFragmentManager()); 
     pager.setAdapter(adapter); 
     pager.addOnPageChangeListener(adapter); 
     // Set current item to the middle page 
     pager.setCurrentItem(MainHandheldActivity.FIRST_PAGE); 
     currentPage = MainHandheldActivity.FIRST_PAGE; 
     // Set number of pages 
     pager.setOffscreenPageLimit(3); 
     // Set no margin so other pages are hidden 
     pager.setPageMargin(0); 

     // ... // 
} 

該外部片段被添加到一個事務,像這樣:

mFragmentManagerer = getSupportFragmentManager(); 
mFragmentTransaction = mFragmentManagerer.beginTransaction(); 
mMainFragment = new MainFragment(); 
mFragmentTransaction.add(R.id.fragmentContainer, mMainFragment, FRAG_MAIN); 
mFragmentTransaction.commit(); 
mFragmentManagerer.executePendingTransactions(); 
我在外導航菜單片段在我的ViewPager片段

所以,當我從ViewPager選擇一個項目,我把整個外導航菜單上返回堆棧:

public void replaceFragment(Fragment frag, String fragTag) { 
    mFragmentTransaction = mFragmentManagerer.beginTransaction(); 
    mFragmentTransaction.replace(R.id.fragmentContainer, frag, fragTag); 
    mFragmentTransaction.addToBackStack(null); 
    mFragmentTransaction.commit(); 
} 

這一切工作正常,直到這一點。但是,從backStack返回後(當我按下手機上的後退按鈕時),viewPager不會顯示圖標,但它仍然允許我循環瀏覽viewPager,並且在循環2或3個項目後,圖標再次出現。有誰知道爲什麼會發生這種情況?我有點不確定這些片段在放入並從backStack中檢索時發生了什麼。謝謝。

回答

2

已解決!

很多小時後,我終於解決了這個問題。我發現here,解決的辦法是簡單地替換 'getFragmentManager()' 與 'getChildFragmentManager()' 設置適配器時:

pager = (ViewPager) view.findViewById(R.id.pager); 

// Added getChildFragmentManager in here 
adapter = new CollectionPageAdapter((MainHandheldActivity)getActivity(),getChildFragmentManager()); 

pager.setAdapter(adapter); 
pager.addOnPageChangeListener(adapter); 
// Set current item to the middle page 
pager.setCurrentItem(MainHandheldActivity.FIRST_PAGE); 
Log.d("PAGER", "pager setCurrentItem: " + MainHandheldActivity.FIRST_PAGE); 
currentPage = MainHandheldActivity.FIRST_PAGE; 
// Set number of pages 
pager.setOffscreenPageLimit(3); 
// Set no margin so other pages are hidden 
pager.setPageMargin(0); 
+0

FFS!就是這樣! –