私が持っているもの:
API 22 で最新の Android サポートとデザイン ライブラリを使用しています。次のように、アプリが最初にインストールされたときにのみ実行するProgressFragmentというフラグメントがあります。
mProgressFragment = new ProgressFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, mProgressFragment, TAG_TASK_FRAGMENT)
.commit();
これがレイアウトです
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<ProgressBar
android:id="@+id/build_lib_progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progress_msg1"
android:layout_gravity="center"
android:textSize="22sp"
android:textColor="@color/text_primary"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progress_msg2"
android:layout_gravity="center"
android:paddingTop="10dp"
android:textColor="@color/text_secondary"/>
</LinearLayout>
</LinearLayout>
このフラグメントにはAsyncTaskが含まれています。完了すると、次のように AsyncTask コールバックonPostExecute()内のMainActivityの別のフラグメント ( MainFragmentと呼ばれる) に置き換えられます。
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, new MainFragment())
.commit();
これはMainFragmentレイアウトです。
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/main_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.AppBarLayout
android:id="@+id/main_appbar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<include layout="@layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="@+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorHeight="4dp"
app:tabTextColor="@color/white"/>
</android.support.design.widget.AppBarLayout>
編集:
これは MainActivity のレイアウトです。
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main content displayed here -->
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Navigation Drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_drawer_header"
app:menu="@menu/nav_view_menu"
android:background="#ffffff"
app:itemTextColor="@color/text_tertiary"
app:itemIconTint="#8b8b8b"
/>
問題:
ProgressFragment の AsyncTask が完了し、MainFragment に置き換えられると、TabLayoutビューにタブが表示されません。
私のアプリケーションには、起動時に 2 つの可能なパスがあります。次のいずれかです。
ProgressFragment を追加します (最初に実行した場合) -> 次に MainFragment に置き換えます
それ以外の場合は MainFragment を追加します (ここではタブが正常に表示されます)
どちらの Fragment 置換も MainActivity で行われます。
問題はパス1を扱います
編集2:
MainActivity でrecreate()を呼び出すと、今のところこれが「修正」されます。私はそれを解決策とは呼びませんが。後で戻ってきます。