在.replace()事务中按下Overview按钮时,片段重叠

时间:2018-06-10 13:54:17

标签: android android-fragments

您好,感谢您的提前时间。我有以下问题:

我有一个BottomNavigationView可以在这样的片段之间切换:

MainActivity.java

final FragmentManager fragmentManager = getSupportFragmentManager();

private final SearchFragment searchFragment = new SearchFragment();
private final ContactsFragment contactsFragment = new ContactsFragment();
private final JobsFragment jobsFragment = new JobsFragment();

private BottomNavigationView.OnNavigationItemSelectedListener navigationListener
        = item -> {
    switch (item.getItemId()) {
        case R.id.navigation_search:
            fragmentManager.beginTransaction()
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .replace(R.id.frame_layout, searchFragment).commit();
            break;
        case R.id.navigation_contacts:
            fragmentManager.beginTransaction()
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .replace(R.id.frame_layout, contactsFragment).commit();
            break;
        case R.id.navigation_jobs:
            fragmentManager.beginTransaction()
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .replace(R.id.frame_layout, jobsFragment).commit();
            break;
    }
    return true;
};

底部导航视图按钮非常接近概览按钮,有时我会将它们按在一起,并将两个片段重叠在另一个上面。

这是片段容器所在的 main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<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/container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="55dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@color/colorPrimary"
    app:itemIconTint="@animator/bottom_navigation_colo`enter code here`rs"
    app:itemTextColor="@animator/bottom_navigation_colors"
    app:menu="@menu/navigation"/>

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

最后这里是片段xml布局,以防万一:

search_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="42dp"
    android:elevation="16dp"
    android:fitsSystemWindows="true"
    android:visibility="visible">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        app:layout_scrollFlags="scroll|enterAlways"
        android:layout_height="match_parent">

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

            <AutoCompleteTextView
                android:id="@+id/search_auto_complete"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="@string/home_toolbar_text"
                android:imeOptions="actionSearch"
                android:inputType="text"
                android:maxLines="1"
                android:padding="8dp"
                android:textColor="@android:color/white"
                android:textColorHint="@android:color/white"/>

            <ImageButton
                android:id="@+id/search_button"
                style="@style/Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/search_white"/>

            <ImageView
                android:id="@+id/close_search_button"
                style="@style/Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:visibility="gone"
                app:srcCompat="@drawable/close"
                tools:visibility="visible"/>

        </LinearLayout>

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

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

<ProgressBar
    android:id="@+id/home_progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/search_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout="@id/app_bar"/>

<ExpandableListView
    android:id="@+id/expandable_list"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:groupIndicator="@android:color/transparent"
    tools:visibility="gone"/>

<ListView
    android:id="@+id/search_result_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:visibility="gone"/>
</android.support.design.widget.CoordinatorLayout>

contact_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="42dp">
    <TextView
        android:textSize="18sp"
        android:layout_gravity="center"
        android:padding="8dp"
        android:textColor="@android:color/white"
        android:text="Agenda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/contact_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<LinearLayout
    android:visibility="gone"
    android:id="@+id/empty_placeholder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:layout_width="64dp"
        android:layout_height="64dp"
        app:srcCompat="@drawable/favorite"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="8dp"
        android:text="@string/add_person_of_trust"/>

</LinearLayout>

</LinearLayout>

jobs_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="42dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Contratos"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="18sp"/>
</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:id="@+id/empty_placeholder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:layout_width="64dp"
        android:layout_height="64dp"
        app:srcCompat="@drawable/sofa"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="8dp"
        android:text="Aun no hay profesionales contratados..."/>

</LinearLayout>

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/jobs_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="4dp"/>

</LinearLayout>

再次感谢你的时间,我希望我不要错过任何东西!

0 个答案:

没有答案