複数のフラグメントを制御する1つのアクティビティ(ナビゲーションコントローラ)があります。ユーザーが見る最初の画面は、1つのコンテナに配置された単一のフラグメントです。別のフラグメントにボタンをクリックすると、最初のフラグメントをバックスタックに追加し、2つの新しいフラグメントを最初のフラグメントとは別のコンテナに追加する必要があります。 2つのフラグメントは上部にマップフラグメントがあり、下部にプロファイルの詳細フラグメントがありますか?単一のフラグメントを複数のフラグメントに置き換える
コードは次のとおりです。
ナビゲーションコントローラホームフラグメント(これはアプリが起動し、メイン画面です):
public void home(Bundle savedInstanceState){
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
CurrentFriendsFragment firstFragment = new CurrentFriendsFragment();
// Add the fragment to the 'fragment_container' FrameLayout
fragmentTransaction
.setCustomAnimations(R.animator.fade_in, R.animator.fade_out)
.replace(R.id.fragment_container, firstFragment, "firstFragment")
.commit();
}
}
ナビゲーションコントローラプロファイルフラグメント:
@Override
public void onProfileButtonClicked() {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState1 != null) {
return;
}
MapFragment mapFragment = new MapFragment();
// Create a new Fragment to be placed in the activity layout
ProfileFragment profileFragment = new ProfileFragment();
// Add the fragment to the 'fragment_container' FrameLayout
fragmentTransaction
.setCustomAnimations(R.animator.slide_in_up, R.animator.slide_out_up, R.animator.slide_in_down, R.animator.slide_out_down)
.add(R.id.fragment_container_bottom_large, profileFragment)
.add(R.id.fragment_container_top_small, mapFragment)
.commit();
}
}
そして、ここでは主な活動xmlファイルです。 3つの異なる容器に注意してください。 「fragment_container」は全画面フラグメント、「fragment_container_top_small」、「fragment_container_bottom_large」は複数のフラグメントを1ページにまとめたものです。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.parse.starter.ViewControllers.NavigationController"
android:background="@color/palette_darkwhite">
<include layout="@layout/tool_bar"
android:id="@+id/toolbar_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/toolbar_layout"
android:layout_above="@+id/bottom_navigation_navbar">
<RelativeLayout
android:id="@+id/fragment_container_top_small"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</RelativeLayout>
<FrameLayout
android:id="@+id/fragment_container_bottom_large"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar_layout"
android:layout_above="@+id/bottom_navigation_navbar">
</FrameLayout>
<FrameLayout
android:id="@+id/fragment_container_popup"
android:layout_width="275dp"
android:layout_height="275dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation_navbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/palette_lightwhite">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:background="@null"
app:srcCompat="@drawable/collpaseup" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<LinearLayout android:id="@+id/thumbnail2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_picture_navbar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
app:civ_border_color="#FF000000"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail2"
android:layout_toRightOf="@+id/thumbnail2"
android:textColor="@color/palette_primarycolor"
android:id="@+id/nameLabel"
android:text="Name"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameLabel"
android:layout_toRightOf="@+id/thumbnail2"
android:textColor="@color/palette_primarycolor"
android:id="@+id/locationLabel"
android:text="Location"
android:textSize="12sp"
android:textStyle="bold"/>
</RelativeLayout>
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
別の(新しい)アクティビティで2つのフラグメントをホストするほうが簡単ではありませんか? – Gotiasits
これは可能かもしれませんが、このアプリケーションの全体的なポイントは、複数のフラグメントで1つのアクティビティのみを使用することです。 –