具有协调器布局的 ViewPager fragment 中的 Android 设计支持库 FAB

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

我正在尝试从 ViewPager 内的 fragment 内的 Android 设计支持库获取 float 操作按钮。我有 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 没有按照设计规范工作。即工厂的隐藏和显示不起作用。此外,当 fragment 被激活时,FAB 不在它的初始位置。我在下面附上了屏幕截图以使其更加清晰。

在左图中,如您所见,FAB 不在屏幕上。当我滚动时,工具栏将隐藏(想想 Play Store 应用程序)并且选项卡保留,届时 FAB 将向上滚动。

enter image description here

这是设计支持库中的错误吗?或者我的布局不正确?另外,我只想在其中一个 fragment 中添加 FAB,因此添加 main_layout.xml 有点违背了这一目的。

最佳答案

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

这是因为:

app:layout_behavior="@string/appbar_scrolling_view_behavior"

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

您的 fragment 填充了 ViewPager 的整个大小,因此正确地将 FAB 对齐到 ViewPager 容器的右下角;但是因为 ViewPager 容器是偏移的,所以右下角在屏幕外。

在此上下文中使用 FloatingActionButton 的“正确”方法是让 Activity 显示它 - 如下所示:

<?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。界面。

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

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 上,一切都应该很好......

关于具有协调器布局的 ViewPager fragment 中的 Android 设计支持库 FAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32969663/

相关文章:

java - 在 ViewPager 的滑动上停止 Exoplayer

java - 如何将变量值放在另一个变量名中

Android给一个menuItem添加子菜单,addSubMenu()在哪里?

java - 一个按钮不会出现

android - drawable.setColorFilter 标记为已弃用

android - 将数据从 Activity 传递到另一个 Activity,然后传递到 ViewPager 中的 fragment

android - 静态方法中的 findViewById

android - 如何设置Gridview的文字颜色

android - 底部带有可隐藏 fragment 的 CoordinatorLayout

android - 方向更改后以编程方式截取屏幕截图