FloatingActionButton片段中的隐藏工具栏上的崩溃 [英] FloatingActionButton in Fragment hidden on Toolbar collapsing
问题描述
我有一个有3个标签的活动。每个标签页是显示RecyclerView片段。一片段的具有FloatingActionButton在它。我实施这个按钮在片段的布局。我也使得静态的片段的右下角。
I have an activity that has 3 tabs. Each tabs page is a fragment which displays a RecyclerView. One of the fragment has FloatingActionButton in it. I'm implementing this Button in the Fragment's layout. I'm also making it static at the bottom right of the Fragment.
片段布局:
- CoordinatorLayout
- RecyclerView
- FAB (without any behavior)
在活动的布局,我有:
- CoordinatorLayout
- AppBarLayout
- Toolbar
- TabLayout (SmartTabLayout)
- ViewPager
现在的问题是FAB从视图半隐藏工具栏时展开,但是当工具栏被折叠尽显。但如果我在活动实施FAB按钮本身不会发生这种情况。但我不希望在所有片段的按钮。我只是把它在第一个片段。
The problem now is the FAB is half-hidden from the view when Toolbar is expanded, but fully shown when Toolbar is collapsed. Though this does not happen if I implement the FAB button in Activity itself. But I don't want to have the button in all of the Fragments. I'm only putting it in first Fragment.
下面是我做出了解释得更为清晰的GIF。
Here is a gif I made to explain this clearer.
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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@color/color_primary"
app:layout_scrollFlags="scroll|enterAlways" />
<com.ogaclejapan.smarttablayout.SmartTabLayout
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height"
android:background="@color/color_primary" />
</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" />
</android.support.design.widget.CoordinatorLayout>
XML片段为布局:
XML for Fragment layout:
<?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"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/card_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_plus_white_48dp"
app:layout_anchor="@id/card_list"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
我的问题是我怎么做,这样,当recyclerview滚动按钮仍然显示?
My question is how do I make so that the button stays visible when recyclerview is scrolled?
推荐答案
最好的办法是干脆把FloatingActionButton的活动,并呼吁显示()
和隐藏()
在 ViewPager.OnPageChangeListener
。这样,你得到很好的<一个href=\"https://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0B6Okdz75tqQsVlhxNGJCNTEzNFU/components-buttons-fab-behavior_04_xhdpi_009.webm\"符合相对=nofollow>进入/退出动画以<一个href=\"https://www.google.com/design/spec/components/buttons-floating-action-button.html#buttons-floating-action-button-floating-action-button\"相对=nofollow>材料设计准则。
The best option would be to just put the FloatingActionButton in the Activity, and call show()
and hide()
in a ViewPager.OnPageChangeListener
. This way you get nice enter/exit animations that conform to Material Design guidelines.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
fabAdd.show();
} else {
fabAdd.hide();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
这篇关于FloatingActionButton片段中的隐藏工具栏上的崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!