Float Action Button does not get fixed at the footer with Collapsing Actionbar/Toolbar

Asked

Viewed 609 times

0

I have a Fragment with Coordinator layout, recyclerview and a Float Action Button, and in the view that calls this Fragment has a tablayout that hides and appears as you go up or down the Recycler view, I have tried several ways to leave Fab fixed in the footer, but anyway it always rises when slips the view Recycler

inserir a descrição da imagem aqui

follows below the code of Fragment:

<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"
android:id="@+id/frmLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.julioantonini.meuchat.ContatosFragment">


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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="70dp"
    android:layout_marginRight="10dp"
    android:layout_marginEnd="10dp"
    app:elevation="1dp"
    app:srcCompat="@drawable/ic_action_add"
    app:layout_anchor="@id/recyclerContatos"
    app:layout_anchorGravity="bottom|right|end"/>

and below the code of Activity:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:layout_scrollFlags="scroll|enterAlways"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />


    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        app:tabIndicatorColor="@color/colorTab"/>

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


<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_below="@id/appbar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

  • 1

    What is the xml of your Activity? Updates the question...

  • I added the code of acitivity

1 answer

0


If you have no problem putting your FAB in activity, you can do it this way:

...
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_below="@id/appbar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="70dp"
    android:layout_marginRight="10dp"
    android:layout_marginEnd="10dp"
    app:elevation="1dp"
    app:srcCompat="@drawable/ic_action_add"
    app:layout_anchor="@id/viewPager"
    app:layout_anchorGravity="bottom|right|end"/>

What changes here is that the layout_anchor of your FAB is no longer the RecyclerView and becomes the ViewPager. In addition to, of course, removing the FAB of the layout of your fragment and move on to the activity.

  • Is it the only way ? There are 3 tabs and it only appears in the first one, and after adding a user you need to give a reflesh in the Recycler view, then I will have to use a broadcast to do the reflesh right

  • I believe that if you are going to open one Activity from another and, depending on the result it returns, you are going to update something, the recommended is to use startActivityForResult() instead of BroadcastReceiver

  • In the matter of appearing the button only on the first tab, you can place a System in your Viewpager (setOnPageChangeListener()). And when your FAB is in first position, use the method show(), otherwise, the method hide().

  • When in hiding, that’s what I really imagined! Fab calls only a dialog to insert a new user and then updates the Adapter of the Recycler view, Fab being in Activity, which would be the best way to warn Fragment to update the Adapter of the Recycler view

  • In this case you can store the instance of your Fragment as a member of your Activity, open the dialog normally, still in Activity put a Listener in the dialog (is it native or have you created a custom?) in the action in which the user is added, and when your Systener is called you check if your Ragment instance is not null and call a method from your Ragment that can update recyclerview. If you have not understood it this way, I can update the answer, but you will need to update the question with the code of your dialog and your Activity

  • Thanks Danilo, I managed to do using the broadcast since the app uses the broadcast that listens to firebase and does other things, however, you could, pass me the example of the code of how I would call a function of Fragment through the view of this form of instance. I’m still crawling on Android and all knowledge is welcome!

  • You mean calling a Fragment function through Activity?

  • Yes, that way I wouldn’t have to send the broadcast on that Activity

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.