перекрытие фрагментов и активность - PullRequest
0 голосов
/ 23 мая 2018

С левой стороны моей страницы MainActivity появилась панель навигации, а затем все действия внутри панели навигации отображаются в fragment.Когда я нажимаю на любое действие, fragment перекрывает мою MainActivity страницу.

Screenshot

Вы можете видеть, что есть text и buttonв теме.Как мне преодолеть эту проблему? Код

public void selectedItemDrawer(MenuItem menuItem){
    Fragment myFragment = null;
    Class fragment = null;

    switch(menuItem.getItemId()){

        case R.id.checkIn:
            //Not yet complete
            break;

        case R.id.checkOut:
            //Not yet complete
            break;

        case R.id.applyOff:
            break;

        case R.id.reportBug:
            break;

        case R.id.manageProfile:
            fragment = fragment_manageProfile.class;
            /*
             * Here is the complete 1
             */
            break;

        case R.id.logout:
            break;

        default:
            break;
    }


    try{
        myFragment = (Fragment)fragment.newInstance();
    }catch(Exception e){
        e.printStackTrace();
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentManager.beginTransaction()
            .replace(R.id.viewPage, myFragment).addToBackStack(null).commit();



    menuItem.setChecked(true);
    drawer.closeDrawers();
}

First_layout xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewPage"
android:background="@drawable/login_background">

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true">


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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleTextAppearance="@android:color/transparent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />


        </RelativeLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


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


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

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



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

</FrameLayout>

2nd_layout 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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

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

В моем методе onCreate я использую второй макет для просмотра макета setContentView(R.layout.2nd_layout);

Ответы [ 3 ]

0 голосов
/ 23 мая 2018

Перекрытие может происходить из-за тега include в вашем first_layout.xml:

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

удалите эту строку и посмотрите, прекратилось ли перекрытие.

0 голосов
/ 24 мая 2018

добавьте это android:background="@android:color/holo_red_dark" в ваш макет фрагмента

0 голосов
/ 23 мая 2018

Попробуйте установить цвет фона для вашего NavitagionView.В ваш xml сделайте что-то вроде этого

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@android:color/red"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

NOTE

FrameLayout позволяет перекрывать дочерние элементы.Это его поведение.Если вы не хотите этого, вы должны использовать другой макет.Посмотрите на там

...