我設計了一個由一些片段組成的應用程序。 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中按下「返回」按鈕後再次顯示標籤中的所有內容。
謝謝!
這一切都是假設在主要活動的地方就是每個選項卡的內容在被加載 – gabe3vino
總的來說,我認爲你所遇到的問題。當你點擊New_Content中的那個按鈕時,你正在創建一個新的Tab Fragment,它會改變你的backstack,當它聽起來像你真正想做的事情是返回到應該已經在你的後臺堆棧中的原始主頁片段點 – gabe3vino
其實我只是想回到原來的主頁當我按下回來,我想知道如何做到這一點 –