从 viewpager 中的特定片段隐藏工具栏和标签布局
Hiding toolbar and tablayout from a particular fragment in a viewpager
我使用一个 activity 和四个 Fragments
。片段 1、2 和 3 是 Viewpager Fragments
和片段 4 是一个 Detail Fragment
,它会在单击列表项时替换片段 3。不幸的是,当细节片段替换主片段时,它会在现有工具栏和 Tablayout
之外添加一个 Toolbar
和后退按钮。但是,我想用这个替换现有的工具栏和标签布局。
问题:如果不创建新的 activity,我该如何实现?
主片段。具有所需的工具栏和标签布局。
详情片段。同时具有新工具栏(需要)和原始工具栏和标签布局(不需要)
MainActivity.XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@color/black_process_2"/>
</android.support.design.widget.AppBarLayout>
DetailFragment.XML
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.allison.viewpagermasterdetail.ItemDetailActivity"
tools:ignore="MergeRootFrame">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="@+id/detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/item_detail"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textIsSelectable="true"/>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/stat_notify_chat"
app:layout_anchor="@+id/item_detail_container"
app:layout_anchorGravity="top|end" />
当您打开详细信息片段调用时:
toolbar.setVisibility(View.GONE);
tabLayout.setVisibility(View.GONE);
和
@Override
public void onBackPressed() {
if (currentFragment instanceOf DetailFragment) {
toolbar.setVisibility(View.VISIBILE);
tabLayout.setVisibility(View.VISIBLE);
}
...
}
好吧,我个人认为最好的解决方案是在单独的 activity 中打开带有自己工具栏的详细信息。在我看来,这个解决方案是最简单和最干净的。
如果您想要详细片段,我建议您通过 setSupportActionBar() 在 activity 中设置工具栏,并使用 getSupportActionBar() 方法在片段中修改它。
我使用一个 activity 和四个 Fragments
。片段 1、2 和 3 是 Viewpager Fragments
和片段 4 是一个 Detail Fragment
,它会在单击列表项时替换片段 3。不幸的是,当细节片段替换主片段时,它会在现有工具栏和 Tablayout
之外添加一个 Toolbar
和后退按钮。但是,我想用这个替换现有的工具栏和标签布局。
问题:如果不创建新的 activity,我该如何实现?
主片段。具有所需的工具栏和标签布局。
详情片段。同时具有新工具栏(需要)和原始工具栏和标签布局(不需要)
MainActivity.XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@color/black_process_2"/>
</android.support.design.widget.AppBarLayout>
DetailFragment.XML
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.allison.viewpagermasterdetail.ItemDetailActivity"
tools:ignore="MergeRootFrame">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="@+id/detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/item_detail"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textIsSelectable="true"/>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/stat_notify_chat"
app:layout_anchor="@+id/item_detail_container"
app:layout_anchorGravity="top|end" />
当您打开详细信息片段调用时:
toolbar.setVisibility(View.GONE);
tabLayout.setVisibility(View.GONE);
和
@Override
public void onBackPressed() {
if (currentFragment instanceOf DetailFragment) {
toolbar.setVisibility(View.VISIBILE);
tabLayout.setVisibility(View.VISIBLE);
}
...
}
好吧,我个人认为最好的解决方案是在单独的 activity 中打开带有自己工具栏的详细信息。在我看来,这个解决方案是最简单和最干净的。
如果您想要详细片段,我建议您通过 setSupportActionBar() 在 activity 中设置工具栏,并使用 getSupportActionBar() 方法在片段中修改它。