简体   繁体   English

浮动操作按钮在片段中重复

[英]Floating Action Button is duplicated in fragment

I had a FAB in one of my activities, and everything was good.我的一项活动中有一个 FAB,一切都很好。 Then I added a Fragment in that activity and I moved my FAB inside that Fragment .然后我在该活动中添加了一个Fragment并将我的 FAB 移动到该Fragment It seemed to be alright but when I tried to show a Snack Bar , I saw that there are two Floating Action Button s instead of one in that area, One of them was pushed up by Snack Bar and the other one didn't move.看起来没问题,但是当我尝试显示Snack Bar ,我看到该区域有两个Floating Action Button而不是一个,其中一个被Snack Bar推了起来,另一个没有移动。 In XML, I just have one FAB but when I run my app, there are two FABs but I can't see both unless I show an Snack Bar .在 XML 中,我只有一个 FAB,但是当我运行我的应用程序时,有两个 FAB,但除非我显示Snack Bar否则我看不到两者。

What I exactly want is to find the bug and remove the extra FAB.我真正想要的是找到错误并删除额外的 FAB。

Activity XML:活动 XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fl_main_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl_main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/tb_main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:minHeight="?android:attr/actionBarSize"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

        <TextView
            android:id="@+id/tv_main_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@color/white"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <fragment
        android:id="@+id/f_main_memory"
        android:name="ir.ali_kh_y.project.fragment.MemoryFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        android:layout_marginBottom="56dp"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bnv_main_nav"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/bottom_nav_item_color_state"
        app:itemTextColor="@color/bottom_nav_item_color_state"
        app:labelVisibilityMode="unlabeled"
        app:menu="@menu/bottom_nav"/>
</FrameLayout>

Fragment XML:片段 XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:id="@+id/cl_memory_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.MemoryFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl_memory_memories"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_memory_memories"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ECEFF1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <ImageView
            android:id="@+id/iv_memory_smile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/empty_list"/>

        <TextView
            android:id="@+id/tv_memory_empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="---"
            android:textAlignment="center"
            android:textColor="@color/emptyListContent"
            android:textSize="30dp"
            app:layout_constraintBottom_toTopOf="@+id/iv_memory_smile"
            app:layout_constraintHorizontal_bias="0.504"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_memory_write"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"
        android:focusable="true"
        app:fabSize="normal"
        app:layout_anchor="@+id/cl_memory_memories"
        app:layout_anchorGravity="left|bottom"
        app:rippleColor="@color/gray"
        app:srcCompat="@drawable/ic_write"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

The code I use in Activity to show Snack Bar :我在Activity用来显示Snack Bar的代码:

val snackBar = Snackbar.make(cl_memory_root, "", Snackbar.LENGTH_INDEFINITE)
ViewCompat.setLayoutDirection(snackBar.view, ViewCompat.LAYOUT_DIRECTION_RTL)
snackBar.setAction("") {
   //...
}
snackBar.show()

As Mike M said, I was loading two Fragment s, one in my code and one in the XML layout.正如Mike M所说,我正在加载两个Fragment ,一个在我的代码中,一个在 XML 布局中。 So I removed the one in my code and the problem was solved.所以我删除了代码中的那个,问题就解决了。

<androidx.coordinatorlayout.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:id="@+id/cl_memory_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" 
android:clickable="true"
tools:context=".fragment.MemoryFragment">

make background as white and make it clickable in the fragment and voila..使背景为白色,并使其在片段中可点击,瞧..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM