【问题标题】:Show/Hide FloatingActionButton on ViewPager swipe在 ViewPager 滑动时显示/隐藏 FloatingActionButton
【发布时间】:2016-05-13 16:33:55
【问题描述】:

我有一个包含 3 个标签的活动。每个标签页都是一个显示 RecyclerView 的片段。其中一个片段中有 FloatingActionButton。我正在片段的布局中实现这个按钮。我还在 Fragment 的右下角将其设为静态。

片段布局:

- CoordinatorLayout
    - RecyclerView
    - FAB (without any behavior)

在 Activity 布局中,我有:

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

现在的问题是,当 Toolbar 展开时 FAB 在视图中是半隐藏的,但在 Toolbar 折叠时完全显示。尽管如果我在 Activity 本身中实现 ​​FAB 按钮,则不会发生这种情况。但我不想在所有片段中都有按钮。我只是把它放在第一个 Fragment 中。

这是我制作的一个 gif 来更清楚地解释这一点。

用于 Activity 布局的 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 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 时保持可见?

【问题讨论】:

    标签: android android-layout android-design-library floating-action-button


    【解决方案1】:

    最好的选择是将 FloatingActionButton 放在 Activity 中,然后在 ViewPager.OnPageChangeListener 中调用 show()hide()。这样你就可以得到符合Material Design guidelines的漂亮enter/exit animations

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

    结果:

    【讨论】:

    • 我想我会使用您的解决方案,因为它提供了更多功能。感谢您的建议!
    • @AimanB 还有一件事,一定要使用最新版本的支持设计库,他们在最新版本中修复了许多问题,其中一个是默认动画显示为show() 和@987654330 @方法。
    【解决方案2】:

    添加

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    

    也到 RecyclerView。

    【讨论】:

    • 这根本没有任何作用。
    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2015-10-15
    • 1970-01-01
    • 2016-06-17
    • 2013-12-16
    • 2016-01-17
    相关资源
    最近更新 更多