如何隐藏片段内的活动视图

时间:2017-04-13 08:07:27

标签: android android-activity fragment

我是android开发的新手,并试图开发简单的新闻应用程序。但是在使用片段时我遇到了问题。

这是我的活动xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<include layout="@layout/app_container_view"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

<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:menu="@menu/drawer_view"
    app:headerLayout="@layout/nav_header">

</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

app_container_ view xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/coordinatorLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <include
        layout="@layout/toolbar" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabTextColor="@android:color/white"
        app:tabSelectedTextColor="@android:color/white"
        app:tabIndicatorHeight="6dp"
        app:tabMaxWidth="0dp"
        app:tabMode="scrollable"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>

<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.support.design.widget.CoordinatorLayout>

工具栏xml

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
   app:layout_scrollFlags="scroll|enterAlways">

<TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="EastNews"
    style="@style/Toolbar.TitleText"
    android:layout_gravity="center"
    />

</android.support.v7.widget.Toolbar>

当我用事务替换片段时,它只更改片段中的工具栏,而rest则从activity视图继承。我不希望活动标签和其他视图在片段UI上变得可见。

片段xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<include
    layout="@layout/toolbar" />

<!-- <android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerIraq"
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/> -->

我已经用这种方式替换了片段

 boolean fragmentPopped = fragmentManager.popBackStackImmediate(fragment.getClass().getName(),0);

    if(!fragmentPopped && fragmentManager.findFragmentByTag(fragment.getClass().getName()) == null) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.content_frame, fragment, fragment.getClass().getName());
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.addToBackStack(fragment.getClass().getName());
        transaction.commit();
    }

感谢Rahul问题已经解决。在发布问题之前,我试图通过将setVisibility(View.GONE)添加到attach()和setVisibility(View.VISIBLE)来解决这个问题。它也有效,但活动视图加载缓慢。

1 个答案:

答案 0 :(得分:3)

fragment.xml 的父 RelativeLayout 中添加以下两行:

android:background="@color/white"
android:clickable="true"

背景颜色是您的片段背景颜色。这样,当您打开片段时,您将无法看到活动视图。希望它会帮助你。