Bug with anchored FloatingActionButton in support

2019-04-04 14:50发布

I have been having this issue since 24.2.0, but now I'm using 24.2.1 and the bug is still here, it only works well <= 24.1.1.

I have an anchored FloatingActionButton done like this:

<ScrollView
    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.support.design.widget.CoordinatorLayout
        android:id="@+id/contact_coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context="es.cocode.baseapp.contact.ContactFragment">

        <FrameLayout
            android:id="@+id/map_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="198dp"
            android:layout_marginBottom="210dp">

            <fragment
                android:id="@+id/map_fragment"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </FrameLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_fullscreen_map"
            android:src="@drawable/ic_fullscreen_white_48dp"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:layout_margin="@dimen/fab_margin"
            app:layout_anchor="@id/map_fragment_container"
            app:layout_anchorGravity="bottom|end" />

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

</ScrollView>

It usually works like this:

FAB working well

But sometimes the bug appears and the anchor doesn't work:

FAB not working

Is there a way to solve this, or should I wait until this bug gets fixed and use 24.1.1 instead?

5条回答
戒情不戒烟
2楼-- · 2019-04-04 15:23

As for 5/2/2017 (25.3.1) the bug still hasn't been fixed. After trying all solutions, the only one that worked for me was:

yourView.post(new Runnable() { 
    @Override 
    public void run() { 
        yourView.requestLayout(); 
    } 
}); 

Where yourView is the anchored view.

查看更多
何必那么认真
3楼-- · 2019-04-04 15:26

Bug reported and hopefully fixed soon. Please star it.

查看更多
混吃等死
4楼-- · 2019-04-04 15:27

Well my friend there is another way to it using latest support libraries and not using any other thing .

Just use this -

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:src="@drawable/ic_add"
        android:layout_gravity="center_vertical|center_horizontal"
        app:layout_anchor="@+id/appBar"
        app:layout_anchorGravity="bottom|right" />
查看更多
太酷不给撩
5楼-- · 2019-04-04 15:28

Marked as Fixed Issue is fixed and released in 26.0.0 version of support library.

查看更多
做个烂人
6楼-- · 2019-04-04 15:32

The solution was to set the FloatingActionButton visibility to GONE in the XML and when the map is ready set it back to VISIBLE.

@Override
public void onMapReady(GoogleMap googleMap) {
    . . .
    yourFAB.setVisibility(View.VISIBLE);
}

EDIT: This is a workaround for this bug.

查看更多
登录 后发表回答