如何在Fragment中隐藏活动视图

4

我是一名Android开发新手,正在尝试开发一个简单的新闻应用。但是在使用Fragment时遇到了问题。

这是我的活动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>

当我使用事务(transaction)替换片段时,它只会改变片段中的工具栏(toolbar),而其他部分都继承自活动(activity)视图。我不希望活动(activity)标签(tab)和其他视图出现在片段(fragment)界面上。
片段(fragment) 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,问题已经解决。在发布问题之前,我尝试通过在attach()方法中添加setVisibility(View.GONE),在detach()方法中添加setVisibility(View.VISIBLE)来解决此问题。这种方法也起作用,但是活动视图加载缓慢。

1个回答

5

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

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

背景颜色与您的片段背景颜色相同。这样当您打开片段时,便无法看到活动视图。希望这能帮到您。


1
哇,这个确实有效。它是隐藏了活动视图吗?也就是说,如果我向片段添加一些内容,它能正常工作吗? - msadra
2
是的,它会正常工作。如果我的答案对您有帮助,请投票支持我。 - Rahul Sharma
1
我只是想确认一下片段内容。非常感谢。 - msadra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接