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);
}
});