2016-06-28 100 views
0

我設計了一個由一些片段組成的應用程序。 MainScreen的主屏幕由一個工具欄和一個顯示一些標籤及其相應片段的框架佈局組成。對於Mainscreen單擊後退按鈕後,片段無法正確顯示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<include layout="@layout/toolbar" 
/> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/tab_and_view" /> 
</LinearLayout> 

對於FrameLayout裏tab_and_view

佈局文件,佈局類似下面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<com.android.luftschlafer.engdictionary.Tab.SlidingTabLayout 
    android:id="@+id/tabs" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorAccent"/> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="0px" 
    android:layout_weight="1" /> 
</LinearLayout> 

我用從谷歌SlidingTabLayout例如建立標籤。 然後我設計了一個頁面作爲主頁,這就是所謂的Home_Page。 Home_Page只有一個ListView項目來顯示一些選擇,它將顯示在MainScreen的ViewPager中。對於Home_Page

版式文件:

FragmentTransaction ftran=getFragmentManager().beginTransaction(); 
ftran.replace(R.id.tab_and_view,new NewContent()); 
ftran.addToBackStack("HomePage"); 
ftran.commit(); 

我的設計是,當我點擊在home_function_list一個項目,片段將交換:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".Corr_Pages.HomePage"> 

<!-- TODO: Update blank fragment layout --> 
<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/home_function_list"/> 
</RelativeLayout> 

其處理交換在Home_Page.java的代碼到另一個片段。例如,從Home_Page到New_Content。 在New_Content中,有一個可以返回home_page的按鈕。

在按鈕的onClickListener中,我使用這些代碼來執行操作。

FragmentTransaction ftran=getFragmentManager().beginTransaction(); 
ftran.replace(R.id.tab_and_view, new TabFragment()); 
ftran.commit(); 

參考the picture after pressing back button in New_Content,我發現,首頁的內容和搜索選項卡失蹤之前,我點擊設置和應用簡介。所以我想知道如何在New_Content中按下「返回」按鈕後再次顯示標籤中的所有內容。

謝謝!

回答

0

我不知道是否有更有效的方法,但是當我遇到這個問題時使用了片段+活動+後退按鈕,我通過在相關活動中實現onBackPressed()來解決該問題,我的片段管理器用於我的背部堆疊。基於後面的堆棧(這可以讓我確定從哪裏按下),然後我就可以正確地重建我的UI元素。在我的情況下,這樣做非常重要,因爲我有一個用作應用程序操作欄的工具欄,並且我需要在整個應用程序中導航時更改此工具欄的外觀。

因此,例如,我做的是:

@Override 
    public void onBackPressed() { 
     if (mFragManager.getBackStackEntryCount() > 1) { 

      mFragManager.popBackStack(); 
      getSupportActionBar().setTitle(R.string.action_settings); 
      getSupportActionBar().setSubtitle(""); 


     } else if (mFragManager.getBackStackEntryCount() > 0) { 
      mFragManager.popBackStack(); 

      getSupportActionBar().setTitle(R.string.app_name); 
      getSupportActionBar().setIcon(null); 

      mDrawerToggle.setDrawerIndicatorEnabled(true); 
      statusbar.setVisibility(View.VISIBLE);; 

      setTheme(R.style.AppTheme); 


     } else { 
      super.onBackPressed(); 
     } 
    } 
+0

這一切都是假設在主要活動的地方就是每個選項卡的內容在被加載 – gabe3vino

+0

總的來說,我認爲你所遇到的問題。當你點擊New_Content中的那個按鈕時,你正在創建一個新的Tab Fragment,它會改變你的backstack,當它聽起來像你真正想做的事情是返回到應該已經在你的後臺堆棧中的原始主頁片段點 – gabe3vino

+0

其實我只是想回到原來的主頁當我按下回來,我想知道如何做到這一點 –