用另一个childFragment替换childFragment将视图放在顶部

时间:2018-12-12 14:20:28

标签: android android-fragments

我有一个片段,其中有另一个片段,即子片段,它将仅在屏幕的下半部分显示,而父片段是全屏视图。我想用另一个子片段替换我的子片段。我遵循标准的片段替换过程,但是它向我显示了旧子片段之上的新片段。 我不确定自己在做什么错。下面是我的XML文件,其中包含父片段。

  <android.support.constraint.ConstraintLayout 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:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.HomeActivity">

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="56dp"
            android:text="@string/title_mirror"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:itemIconTint="@drawable/bottom_nav_icon_color_selector"
            app:itemTextColor="@drawable/bottom_nav_icon_color_selector"
            app:menu="@menu/navigation" />

    </android.support.constraint.ConstraintLayout>

Below is my parent fragment xml file containing child fragment:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.smartmirror.emedsim.ui.mirror.DraggingPanel
        android:id="@+id/outer_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:id="@+id/main_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/queen_button"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                android:background="@color/transparent_black"
                android:weightSum="3">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/up_arrow"/>
            </LinearLayout>

            <FrameLayout
                android:id="@+id/frame_video"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </FrameLayout>


        </LinearLayout>
    </com.smartmirror.emedsim.ui.mirror.DraggingPanel>
</RelativeLayout>

下面是我的文件,它从父片段中添加了子片段:

 private void initViews(View view) {
        mDraggingPanel = view.findViewById(R.id.outer_layout);
        mMainLayout = view.findViewById(R.id.main_layout);
        Fragment childFragment = new DiscoveryFragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_video, childFragment).commit();
    }

成功添加了第一个子片段。 现在,我尝试将以下子片段替换为我的适配器类中的新子片段,如下所示:

private void proceedFurther(int position, View v) {
        FragmentManager fragmentManager = ((AppCompatActivity) mContext).getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
       Fragment childFragment = new VideoIntroScreen();
        childFragment.setArguments(args);
        transaction.replace(R.id.frame_video, childFragment);
        transaction.commit();
    }

2 个答案:

答案 0 :(得分:0)

我设法得到答案的解决方案。我是在回答是否有人需要的情况。

我刚刚在片段布局文件中添加了?android:windowBackground 并解决了我的视图重叠问题。

将此添加到您的父级和子级片段根布局中。

答案 1 :(得分:0)

您需要确保使用正确的FragmentManager。在您的initViews()方法中,您正在使用父级FragmentManager proceedFurther()Fragment. In the活动method, you're using the FragmentManager`(托管父级)的子级分段)。

所以你有

Activity
  -> Parent Fragment (in Activity FragmentManager)
     -> Child Fragment 1 (in Parent Fragment Child FragmentManager)

为了正确替换“子片段1”,您需要使用“父片段”中的子FragmentManager

相关问题