如何允许滚动 FAB 上方 recyclerview 的最后一项? [英] How to allow scrolling the last item of recyclerview above of the FAB?

查看:36
本文介绍了如何允许滚动 FAB 上方 recyclerview 的最后一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RecyclerView,右下角有一个 FAB.

I have a RecyclerView with a FAB on top at the right bottom.

滚动时,我希望允许用户进一步向上滚动,以便最后一个项目完全可见并且不被 FAB 覆盖.在显示背景的底部允许额外的 48 像素就可以了.

When scrolling I would like to allow the user to scroll further up such that the last item is fully visible and not covered by the FAB. It would be ok to allow an additional 48px at the bottom showing the background.

我尝试添加一个透明的页脚项目来解决这个问题.但是,将排序添加到列表后,透明页脚项在排序过程中会产生一些 UI 故障(分隔线显示在正常项下方).

I tried adding a transparent footer item which does the trick. However, after adding sorting to the list, the transparent footer item creates some UI glitches during sorting (the divider is shown below the normal items).

我尝试添加边距,但是空间一直在浪费.当用户查看顶部的第一个项目时,FAB 覆盖最后一个项目是可以的.只有当最后一项很重要时,才不能涵盖该项目.

I tried adding margin, but then the space is wasted all the time. It is ok that the FAB covers the last item when user views the first items at the top. Only when that last item is important than the item must not be covered.

这是布局xml:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <android.support.v7.widget.RecyclerView
     android:id="@+id/recycler_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="center"
     android:clipToPadding="false"
     android:scrollbars="vertical"/>
 <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    style="@style/floating_action_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/fab_margin"
    android:src="@drawable/ic_add_white_24dp"
    app:layout_anchorGravity="bottom|center"/>
</android.support.design.widget.CoordinatorLayout>

更新使用项目装饰是这里的正确答案.

Update Using item decoration is the correct answer here.

推荐答案

我认为更好的解决方案应该是计算滚动范围并隐藏 fab,如果内容向下滚动.

I think better solution should calculate scroll range and hide the fab, if content was scroll down.

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy){
        if (dy > 0) {
            fab.hide();
            return;
        }
        if (dy < 0) {
            fab.show();
        }
    }
});

但如果你真的想在底部添加边距,你应该使用 ItemDecoratorion.

But if you really want to add margin at bottom you should use ItemDecoratorion.

这篇关于如何允许滚动 FAB 上方 recyclerview 的最后一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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