【问题标题】:Hide FAB in NestedScrollView when scrolling滚动时在 NestedScrollView 中隐藏 FAB
【发布时间】:2016-04-06 06:32:15
【问题描述】:

我有一个嵌套滚动视图,其中包含一些线性布局和文本视图等内容。 由于某些原因,我也在使用浮动操作按钮库。所以我不能使用任何行为。 我不知道我应该如何处理来自滚动视图的滚动更改侦听器,以像行为一样动态隐藏和显示工厂。

关于如何在滚动时隐藏和显示 fab 有什么建议吗?

【问题讨论】:

  • 检查这些解决方案:stackoverflow.com/questions/32038332/…
  • 已经有了。没有任何效果。
  • 仔细阅读:plus.google.com/+IanLake/posts/haQL1mnTzaw 并查看 GitHub 上的 cheesesquare
  • @piotrek1543 请注意,我为不支持行为的浮动操作按钮使用了不同的库,所以如果我尝试添加“app:layout_behavior="package”,应用程序崩溃。cheesesquare 正在使用默认的浮动操作按钮。
  • 对不起,我忘记了。所以问这个库的作者,我的意思是在 Github 页面上创建问题

标签: android scrollview floating-action-button nestedscrollview


【解决方案1】:

简单地将下面的代码添加到您的 NestedScrollView ScrollChangeListener:

NestedScrollView nsv = v.findViewById(R.id.nsv);
    nsv.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            if (scrollY > oldScrollY) {
                fab.hide();
            } else {
                fab.show();
            }
        }
    });

【讨论】:

  • 这需要 API 级别 23 或更高。
  • 你确定吗?我在 API 级别 19 中使用它。
  • 我很确定。有a look here。您会在右上角找到一条文本说 “Added in API level 23”,您的 IDE 也应该指出这一点。
  • 你需要导入这个:android.support.v4.widget.NestedScrollView;
  • 确实如此。它确实以这种方式工作,但它只适用于NestedScrollView
【解决方案2】:

创建 FabScrollBehavior 类

public class FabScrollBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {
    private int toolbarHeight;

    public FabScrollBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.toolbarHeight = AppUtil.getToolbarHeight(context);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
        return dependency instanceof AppBarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
        if (dependency instanceof AppBarLayout) {
            CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            int fabBottomMargin = lp.bottomMargin;
            int distanceToScroll = fab.getHeight() + fabBottomMargin;
            float ratio = (float)dependency.getY()/(float)toolbarHeight;
            fab.setTranslationY(-distanceToScroll * ratio);
        }
        return true;
    }
}

AppUtil.getToolbarHeight(context) 在哪里 -

public static int getToolbarHeight(Context context) {
        final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
                new int[]{R.attr.actionBarSize});
        int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
        styledAttributes.recycle();

        return toolbarHeight;
    }

然后在你的布局中添加到 FloatingActionButton layout_behavior:

   <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_task_accept"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_accepted"
        app:layout_behavior="pass.to.your.FabScrollBehavior.Class"
        app:theme="@style/Widget.AppTheme.Fab"/>

整个布局看起来像

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Widget.AppTheme.AppBarOverlay">

        <include
            layout="@layout/include_layout_toolbar_scroll"/>

    </android.support.design.widget.AppBarLayout>


    <include layout="@layout/include_layout_content_with_nestedscroll"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_task_accept"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_accepted"
        app:layout_behavior="pass.to.FabScrollBehavior.Class"
        app:theme="@style/Widget.AppTheme.Fab"/>


</android.support.design.widget.CoordinatorLayout>

https://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling(part1)/实现

【讨论】:

  • 很棒,但 layout_behavior 只使用“pass.to.FabScrollBehavior”
【解决方案3】:

在 Activity 或片段中定义变量类型 int 以从 ScrollView 设置上一个 Scroll 然后使用此方法在 ScrollView 类中侦听更改滚动

 scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {

    // previousScrollY this variable is define in your Activity or Fragment
            if (scrollView.getScrollY() > previousScrollY && floatingActionButton.getVisibility() == View.VISIBLE) {
                floatingActionButton.hide();
            } else if (scrollView.getScrollY() < previousScrollY && floatingActionButton.getVisibility() != View.VISIBLE) {
                floatingActionButton.show();
            }
            previousScrollY = scrollView.getScrollY();
        }
    });

所有版本的android都可以工作

【讨论】:

  • 这不可靠。它偶尔会起作用。此外,当滚动结束时,FAB 应该显示更好的用户体验,这是不可能的,因为状态无法读取。
【解决方案4】:

花了这么长时间后,我找到了解决方案。 它可能适用于所有情况。虽然这是一个 hack 不是正确的解决方案,但你可以应用它来使这个东西工作。

我们知道setOnScrollChangeListener 仅在最低 api 23 时才有效,那么如果我的最低 api 级别低于 23 怎么办。

所以我从堆栈溢出中找到了我们可以使用getViewTreeObserver().addOnScrollChangedListener 的解决方案,因此这将是所有设备的兼容解决方案。

现在让我们转到最终解决方案“在嵌套滚动视图滚动时隐藏 fab 按钮和在嵌套滚动视图处于理想状态时显示 fab 按钮”

因此,我们可以使用HandlerpostDelayed 来解决这个问题。

  1. 在你的上下文中定义变量private int previousScrollY = 0;

  2. 然后像这样在嵌套滚动视图中使用getViewTreeObserver().addOnScrollChangedListener

NESTEDSCROLLVIEW.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { new Handler().postDelayed(new Runnable() { @Override public void run() { if (NESTEDSCROLLVIEW.getScrollY() == previousScrollY) { FABBUTTON.setVisibility(View.VISIBLE); } else { FABBUTTON.setVisibility(View.INVISIBLE); } } }, 10); previousScrollY = NESTEDSCROLLVIEW.getScrollY(); } });

  1. 现在你准备好了....

【讨论】:

    【解决方案5】:
     nestedScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                    if (nestedScrollView != null) {
                        if (nestedScrollView.getChildAt(0).getBottom() <= (nestedScrollView.getHeight() + nestedScrollView.getScrollY())) {
                            fab.setVisibility(View.INVISIBLE);
                        } else {
                            fab.setVisibility(View.VISIBLE);
                        }
                    }
                }
            });
    

    【讨论】:

    • 纯代码答案是低质量的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2019-10-03
    • 2018-03-28
    • 2015-11-05
    • 1970-01-01
    相关资源
    最近更新 更多