Android Design FAB Support Library in ViewPager Slice with Coordinator Layout

I am trying to get a floating Action button from an Android design support library inside a fragment that is inside the ViewPager. I have 4 tabs and I want the FAB to be in only one tab. My layout is as follows:

main_layout.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_scrollFlags="scroll|enterAlways" /> <android.support.design.widget.TabLayout android:id="@+id/tabs" app:tabIndicatorHeight="3dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

list_fragment_with_fab.xml

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:padding="5dip" android:background="@color/Transparent_White" android:fitsSystemWindows="false" android:layout_width="match_parent" android:layout_height="match_parent"> <org.lucasr.twowayview.widget.TwoWayView android:id="@+id/clip_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:twowayview_layoutManager="ListLayoutManager" /> <android.support.design.widget.FloatingActionButton android:id="@+id/add_list_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:layout_margin="16dp" android:src="@drawable/ic_list_add" app:layout_anchor="@+id/clip_recycler_view" app:layout_anchorGravity="bottom|right|end" /> 

Now the problem is that the FAB does not work according to the design specifications. those. hiding and showing fab does not work. In addition, FAB is not in its original place when the fragment is activated. I have added the screenshots below to make it more understandable.

In the left image, as you can see, FAB is off. When I scroll, the toolbar will hide (think of the Play Store App) and the tabs will remain, at this time the FAB will scroll up.

enter image description here

Is this a bug in the design support library? Or is my layout wrong? In addition, I want FAB to be only one of the fragments, so adding a kind to main_layout.xml hits this target.

+5
source share
1 answer

This is not strictly a mistake, but the way they implemented it.

Because of this:

 app:layout_behavior="@string/appbar_scrolling_view_behavior" 

This behavior does not affect the size of the ViewPager, it just pops it from the bottom of the screen when extending the AppBarLayout.

Your fragment fills the entire size of the ViewPager and therefore correctly aligns the FAB in the lower right corner of the ViewPager container; but since the ViewPager container is offset, the lower right screen.

The β€œright” way to use FloatingActionButton in this context is to show activity - for example:

 <?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:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="bubblebearapps.co.uk.nfcapi.MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/appbar_padding_top" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay"> </android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/container" 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="end|bottom" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout> 

Then you resize, the FloatingActionButton icon and the onClickBehaviour icon based on which page is displayed in the ViewPager using the OnPageChangeListener interface.

To make the FloatingActionButton scroll from the bottom, when you throw a RecyclerView, you must create a Behavior! This is the one I used in another project:

 public class ScrollOffBottomBehaviour extends CoordinatorLayout.Behavior<View> { private int mViewHeight; private ObjectAnimator mAnimator; public ScrollOffBottomBehaviour(Context context, AttributeSet attrs) { super(); } @Override public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) { mViewHeight = child.getHeight(); return super.onLayoutChild(parent, child, layoutDirection); } @Override public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { if(mAnimator == null || !mAnimator.isRunning()){ int totalScroll = (dyConsumed + dyUnconsumed); int targetTranslation = totalScroll > 0 ? mViewHeight : 0; mAnimator = ObjectAnimator.ofFloat(child, "translationY", targetTranslation); mAnimator.start(); } } @Override public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) { return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL; } } 

Install this on your FloatingActionButton using the app: layout_behaviour, and everything should be fine ...

+10
source

Source: https://habr.com/ru/post/1233046/


All Articles