This is regarding the issue of coordinating the behaviour of a Floating Action Button in an Activity which has 5 fragments in it.
In my app, I have an Activity which houses a ViewPager and an xml defined with FloatingActionButton. The ViewPager contains Fragments, and inside each Fragment there is a RecyclerView.On clicking floating action button i have implemented scrollToPosition(0). As i have implemented Floating ActionButton in the xml file of main activity ,scrollToPosition() works only in the last fragment on clicking floating action button.How do i overcome this so that on clicking floating button in any fragment the recycler view scrolls to top?
I have tried implementing floatingactionbutton in fragment layout file and it works completely fine.But the problem with this is floating action button wil be moving when i swipe from one fragment to another fragment.So is there any way so that i can implement floatingactionbutton in the main activity xml file and still be able to achieve scrollToPosotion(0) in every fragment on clicking FloatingActionButton? Many thanks for any suggestions.
The following is the xml file which contains floating action button.
<?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:background="#f4f4f4"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/tb_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="0dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
<com.myapp.now.extras.SlidingTabLayout
android:id="@+id/sl_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
</com.myapp.now.extras.SlidingTabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_marginBottom="@dimen/fab_margin_bottom"
android:layout_marginEnd="@dimen/fab_margin_right"
android:layout_marginRight="@dimen/fab_margin_right"
android:clickable="true"
android:src="@mipmap/top_scroll"
app:backgroundTint="@color/colorFAB"
app:borderWidth="0dp"
app:fabSize="normal" />
The following is an xml file of a fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl_swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:scrollbars="vertical"
android:fadeScrollbars="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:visibility="gone"
android:layout_gravity="center"
android:id="@+id/pbar"
android:layout_width="40dp"
android:layout_height="40dp" />
The following is the implementation of floating action button in each fragment
fab = (android.support.design.widget.FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
llm.scrollToPositionWithOffset(0, 0);
}
});
Not sure if this'll work with SlidingTabLayout since I use ViewPager.
Have the FAB onClick
call a method (e.g. onFABClick()
) in your activity and define it as:
public void onFABClick(View view) {
if (fragments.get(mViewPager.getCurrentItem()) instanceof FragmentA) {
if (fragmentA!= null) {
fragmentA.fabOnClick();
}
} else if (fragments.get(mViewPager.getCurrentItem()) instanceof FragmentB) {
if (fragmentB!= null) {
fragmentB.fabOnClick();
}
}
}
Here fragments
is the array that holds fragment
objects. It is the array that is passed to the adapter of the ViewPager
. I guess you'll have to change mViewPager.getCurrentItem()
with the equivalent get index of current fragment method of SlidingTabLayout
.
Lastly, the fabOnClick()
method is implemented in each fragment. (You'll need to have all your fragments implement an interface (e.g. FABActionInterface) and add the method signature there.
Here you are basically catching the FAB onclick and directing it to call a method on the current fragment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With