在ViewPager上滑动显示/隐藏FloatingActionButton [英] Show/Hide FloatingActionButton on ViewPager swipe

查看:105
本文介绍了在ViewPager上滑动显示/隐藏FloatingActionButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有3个标签的活动.每个选项卡页面都是一个片段,其中显示一个RecyclerView.其中一个片段具有FloatingActionButton.我正在Fragment的布局中实现此Button.我也在Fragment的右下角将其设为静态.

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)

在活动"布局中,我有:

In Activity layout, I have:

- CoordinatorLayout
    - AppBarLayout
        - Toolbar
        - TabLayout (SmartTabLayout)
    - ViewPager

现在的问题是,展开工具栏时,FAB在视图中处于半隐藏状态,而在工具栏折叠时则在视图中完全显示.虽然如果我在Activity本身中实现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.

这是我用来解释这个更清晰的图像.

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放在Activity中,并在ViewPager.OnPageChangeListener中调用show()hide().这样一来,您会变得很不错没有提示/exit动画符合

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) {

    }
});

结果:

这篇关于在ViewPager上滑动显示/隐藏FloatingActionButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆