【发布时间】:2016-04-16 18:00:10
【问题描述】:
我有一个用例,我从网络下载一些数据并填充一个列表,当用户点击它们时,就会出现另一个列表(再次包含来自网络的数据)。当用户单击第二个列表中的项目时,我想显示一个新屏幕。
到目前为止,我所做的是一个带有两个片段(每个列表一个)的 Activity。这两个片段都在CoordinatorLayout 内和工具栏下方。但是我要显示的新屏幕不应该包含工具栏。因此我不能使用用于其他两个片段的FrameLayout。
而且因为我希望用户在启动这个新屏幕时能够使用后退按钮移回以前的状态(加载并滚动第二个列表直到用户单击一个项目)我认为不可能创建一个新的Activity因为保持状态并不容易。
有没有办法在之前的和Toolbar之上创建一个新的片段,如果我只是创建这个片段不会有任何性能成本,因为操作系统必须绘制两个同时碎片(但只显示一个)?请注意,第二个片段中已经加载了很多图像。
我的 xml 布局现在看起来像这样:
<android.support.v4.widget.DrawerLayout
android:id="@+id/dl_container"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar android:id="@+id/tb_toolbar"
android:gravity="center"
android:background="@color/white" android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/toolbar_title" android:layout_gravity="center"
android:textSize="22sp" android:textColor="@color/toolbat_title_color"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/fl_content" android:layout_marginTop="?android:attr/actionBarSize"
android:layout_width="match_parent" android:layout_height="match_parent" />
<include android:id="@+id/bottom_sheet"
layout="@layout/bottom_sheet_video" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nv_navigation"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:layout_gravity="start"
/>
【问题讨论】:
-
((MainActivity)getActivity()).getActionBar().hide();在你的片段 onCreateView 方法中
-
@Mes,在之前的片段 xml 背景中添加白色背景