【发布时间】:2017-08-11 14:51:52
【问题描述】:
我有一个控制多个片段的活动(导航控制器)。用户看到的初始屏幕是放置在一个容器中的单个片段。当我单击另一个片段的按钮时,我需要将第一个片段添加到后台堆栈,并将两个新片段添加到与第一个片段不同的容器中。这两个片段是顶部的地图片段和底部的个人资料详细信息片段这甚至可能吗?
代码如下。
导航控制器主页片段(这是应用启动时的主屏幕):
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 文件。注意三个不同的容器。 “fragment_container”用于全屏片段,“fragment_container_top_small”、“fragment_container_bottom_large”用于一页上的多个片段。
<?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>
【问题讨论】:
-
在另一个(新)活动中托管两个片段不是更容易吗?
-
可能是这样,但这个应用程序的全部意义在于只使用具有多个片段的单个活动。
标签: android android-fragments android-fragmentactivity fragmentmanager