2014-06-10 60 views
1

我有一個(相對)簡單的向下鑽取應用程序。Android Tab ListFragment in Tabs not displayed on back button return

它有一個Activity(MainActivity)包含和轉換片段。

public void goToPartnumbers(){ 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_left); 
    ft.replace(R.id.container, new IdentifierFragment()).addToBackStack("partnumber").commit(); 
} 

IdentifierFragment是一個標籤佈局一拉http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

public class IdentifierFragment extends Fragment implements ActionBar.TabListener { 
private ViewPager viewPager; 
private FragmentStatePagerAdapter mAdapter; 
private ActionBar actionBar; 
private MainActivity mContext; 
// Tab titles 
private String[] tabs = { "Part Numbers", "OEM Numbers" }; 

public IdentifierFragment() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.partnumber_list, container, false); 
    // Initialization 
    mContext = (MainActivity) getActivity(); 
    viewPager = (ViewPager) view.findViewById(R.id.pager); 
    actionBar = mContext.getSupportActionBar(); 

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    resetAdapter(new IdentifierFragmentAdapter(mContext.getSupportFragmentManager())); 

    for (String tab_name : tabs) { 
     actionBar.addTab(actionBar.newTab().setText(tab_name) 
       .setTabListener(this)); 
    } 
    ...I can show you the rest but I don't think it's relevant 

    return view; 
} 

@Override 
public void onDetach(){ 
    super.onDetach(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
    resetAdapter(null); 
} 

public void resetAdapter(FragmentStatePagerAdapter adapter){ 
    actionBar.removeAllTabs(); 
    viewPager.removeAllViews(); 
    mAdapter = adapter; 
    viewPager.setAdapter(mAdapter); 
} 

... 
} 

兩個子片段與CursorLoaders都ListFragments。當選擇了一個排,IdentifierFragment使用以下方法代替:

public void goToGallery(){ 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    galleryHandle = new GalleryFragment(); 
    HashMap<String,String> selection = new HashMap<String, String>(); 
    for (HashMap<? extends String, ? extends String> c : mCriteria){ 
     selection.putAll(c); 
    } 
    galleryHandle.setSelection(selection); 
    ft.replace(R.id.container, galleryHandle).addToBackStack("gallery").commit(); 
} 

當我移動到IdentifierFragment,它工作正常。然後,我可以選擇一行並進入詳細視圖。當我嘗試使用詳細視圖中的後退按鈕時,我得到ViewPager,但沒有ListViews

很明顯,我可以提供任何其他需要的代碼。我剛剛提供了我認爲立即相關的內容。

我很茫然,非常喜歡任何幫助。提前致謝!

回答

0

是不是因爲您替換了當前片段。 或許,如果你使用。新增(),而不是.replace()當你去一個新的畫廊

public void goToGallery(){ 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    galleryHandle = new GalleryFragment(); 
    HashMap<String,String> selection = new HashMap<String, String>(); 
    for (HashMap<? extends String, ? extends String> c : mCriteria){ 
     selection.putAll(c); 
    } 
    galleryHandle.setSelection(selection); 
    ft.add(R.id.container, galleryHandle).addToBackStack("gallery").commit(); 
} 
+0

這並不是說。如果我這樣做了,那麼標籤就會被移除(就像我進入Gallery中一樣),但是listview仍然是前面和中間的(儘管它不會對動作做出響應)。 – GaleDragon