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

msa*_*dra 4 android fragment android-activity

我是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>
Run Code Online (Sandbox Code Playgroud)

app_container_查看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>
Run Code Online (Sandbox Code Playgroud)

工具栏 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>
Run Code Online (Sandbox Code Playgroud)

当我用事务替换片段时,它仅更改片段中的工具栏,其余部分继承自活动视图。我不希望活动选项卡和其他视图在片段 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"/> -->
Run Code Online (Sandbox Code Playgroud)

我用这种方式替换了片段

 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();
    }
Run Code Online (Sandbox Code Playgroud)

感谢拉胡尔,问题已经解决。在发布问题之前,我尝试通过将 setVisibility(View.GONE) 添加到 Attach() 并将 setVisibility(View.VISIBLE) 添加到 detach() 方法来解决此问题。它也有效,但活动视图加载缓慢。

Rah*_*rma 5

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

android:background="@color/white"
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)

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

  • 是的,它会正常工作。如果我的回答对您有帮助,请投票。 (2认同)