与 RecyclerView + AppBarLayout 一起玩 [英] Flinging with RecyclerView + AppBarLayout

查看:24
本文介绍了与 RecyclerView + AppBarLayout 一起玩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 AppBarLayout 和 CollapsingToolbarLayout 的新 CoordinatorLayout.在 AppBarLayout 下面,我有一个包含内容列表的 RecyclerView.

I am using the new CoordinatorLayout with AppBarLayout and CollapsingToolbarLayout. Below AppBarLayout, I have a RecyclerView with a list of content.

我已经验证,当我向上和向下滚动列表时,滚动滚动在 RecyclerView 上有效.但是,我也希望 AppBarLayout 在扩展过程中平滑滚动.

I have verified that fling scrolling works on the RecyclerView when I am scrolling up and down the list. However, I would also like the AppBarLayout to smoothly scroll during expansion.

当向上滚动以展开 CollaspingToolbarLayout 时,一旦将手指从屏幕上移开,滚动就会立即停止.如果您快速向上滚动,有时 CollapsingToolbarLayout 也会重新折叠.RecyclerView 的这种行为似乎与使用 NestedScrollView 时的行为大不相同.

When scrolling up to expand the CollaspingToolbarLayout, scrolling immediately stops once lifting your finger off the screen. If you scroll up in a quick motion, sometimes the CollapsingToolbarLayout re-collapses as well. This behavior with the RecyclerView seems to function much differently than when using a NestedScrollView.

我尝试在 recyclerview 上设置不同的滚动属性,但我一直无法弄清楚.

I've tried to set different scroll properties on the recyclerview but I haven't been able to figure this out.

这是一个视频,展示了一些滚动问题.https://youtu.be/xMLKoJOsTAM

Here is a video showing some of the scrolling issues. https://youtu.be/xMLKoJOsTAM

以下示例显示了 RecyclerView (CheeseDetailActivity) 的问题.https://github.com/tylerjroach/cheesesquare

Here is an example showing the issue with the RecyclerView (CheeseDetailActivity). https://github.com/tylerjroach/cheesesquare

这是使用 Chris Banes 的 NestedScrollView 的原始示例.https://github.com/chrisbanes/cheesesquare

Here is the original example that uses a NestedScrollView from Chris Banes. https://github.com/chrisbanes/cheesesquare

推荐答案

Kirill Boyarshinov 的回答几乎是正确的.

The answer of Kirill Boyarshinov was almost correct.

主要问题是 RecyclerView 有时会给出不正确的方向,因此如果您将以下代码添加到他的答案中,它可以正常工作:

The main problem is that the RecyclerView sometimes is giving incorrect fling direction, so if you add the following code to his answer it works correctly:

public final class FlingBehavior extends AppBarLayout.Behavior {
    private static final int TOP_CHILD_FLING_THRESHOLD = 3;
    private boolean isPositive;

    public FlingBehavior() {
    }

    public FlingBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
        if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive) {
            velocityY = velocityY * -1;
        }
        if (target instanceof RecyclerView && velocityY < 0) {
            final RecyclerView recyclerView = (RecyclerView) target;
            final View firstChild = recyclerView.getChildAt(0);
            final int childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild);
            consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD;
        }
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        isPositive = dy > 0;
    }
}

我希望这会有所帮助.

这篇关于与 RecyclerView + AppBarLayout 一起玩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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