【问题标题】:Android Design Support Library FAB in a ViewPager Fragment with Coordinator Layout带有协调器布局的 ViewPager 片段中的 Android 设计支持库 FAB
【发布时间】:2016-01-03 08:28:09
【问题描述】:

我正在尝试从 ViewPager 内的 Fragment 中的 Android 设计支持库中获取浮动操作按钮。我有 4 个选项卡,我只希望在其中一个选项卡中使用 FAB。我的布局如下:

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" />

现在的问题是,FAB 不能按照设计规范工作。即工厂的隐藏和显示不起作用。此外,当片段被激活时,FAB 不在其初始位置。为了更清楚,我在下面附上了屏幕截图。

在左图中,如您所见,FAB 不在屏幕上。当我滚动时,工具栏将隐藏(想想 Play 商店应用程序)并且标签仍然存在,那时 FAB 将向上滚动。

这是设计支持库中的错误吗?还是我的布局不正确?另外,我只希望 FAB 出现在其中一个片段中,因此添加 main_layout.xml 有点违背了这个目的。

【问题讨论】:

  • 您可以在布局中将可见性设置为gone,并在片段中设置fab.setVisibility(View.VISIBLE) 以显示fab

标签: android android-fragments android-viewpager floating-action-button android-coordinatorlayout


【解决方案1】:

这不是严格意义上的错误,而是他们实现它的方式。

因为这样:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

此行为不会操纵 ViewPager 的大小,它只是在 AppBarLayout 展开时将其推离屏幕底部。

您的片段正在填充 ViewPager 的整个大小,因此正确地将 FAB 与 ViewPager 容器的右下角对齐;但是因为ViewPager容器是偏移的,所以右下角是离屏的。

在这种情况下使用 FloatingActionButton 的“正确”方式是让活动显示它 - 像这样:

<?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>

然后,您可以使用 OnPageChangeListener 接口根据 ViewPager 中显示的页面更改 FloatingActionButton 的大小、图标和 onClickBehaviour。

要让 FloatingActionButton 在您滑动 RecyclerView 时滚动到底部,您必须创建一个 Behaviour!这是我在另一个项目中使用的:

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;


    }


}

使用 app:layout_behaviour 将它设置到您的 FloatingActionButton 上,一切都会好起来的...

【讨论】:

  • 谢谢乔。我对布局进行了更改,类似于您所写的。在活动中,我根据所选页面处理 FAB show() 和 hide() 方法。但是,在 ViewPager 中滚动 RecyclerView 时,FAB 不会隐藏。有什么想法吗?
  • 我为你添加了我的答案
  • 太棒了,但我猜你的意思是 app:layout_behavior ;)
猜你喜欢
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 2020-08-22
  • 2015-11-02
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多