在折叠AppBarLayout之前,防止RecyclerView在AppBarLayout下滚动 [英] Prevent RecyclerView from scrolling under AppBarLayout before AppBarLayout is collapsed

本文介绍了在折叠AppBarLayout之前,防止RecyclerView在AppBarLayout下滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建带有标题的RecyclerView,其中,当您向上滚动RecyclerView时,标题会折叠.我可以使用透明的AppBarLayout和标头MyCoolView在下面的布局中非常紧密地实现此目标.视差效果很好.

I'm creating a RecyclerView with header where the header collapses as you scroll up the RecyclerView. I can achieve this very closely with the layout below, with a transparent AppBarLayout, and MyCoolView which is the header. The parallax effect works great.

但是,如果标题仍然可见并且我按下RecyclerView,则RV缓慢滚动到顶部,并且某些项目在工具栏下方,直到RV到达视图顶部.我一直在玩scrollFlags,但没有取得理想的结果.关于如何改善逃跑体验的任何建议,以免物品被剪掉?

However, if the header is still visible and I fling the RecyclerView, the RV scrolls slowly to the top and some of the items are under the Toolbar until the RV reaches the top of the view. I've been playing around with the scrollFlags but haven't achieved a desirable result. Any suggestions on how to improve the fling experience so the items don't get clipped?

观看视频并观看它何时出现--- https://www.dropbox.com/s/jppd6m7zo41k23z/20160609_151309.mp4?dl=0

View the video and watch when its flinged --- https://www.dropbox.com/s/jppd6m7zo41k23z/20160609_151309.mp4?dl=0

<android.support.design.widget.CoordinatorLayout>

     <android.support.design.widget.AppBarLayout
         android:background="#00000000">

         <android.support.design.widget.CollapsingToolbarLayout
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

             <com.android.myapp.MyCoolView
                app:layout_collapseMode="parallax"/>

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

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

    <android.support.v7.widget.RecyclerView/>

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

推荐答案

可能的解决方案(未经测试).将OnOffsetChangedListener添加到AppBarLayout,并记下偏移值.首先,声明此字段:

Possible solution (untested). Add an OnOffsetChangedListener to your AppBarLayout, and keep note of the offset value. First, declare this field:

private boolean shouldScroll = false;

然后,onCreate:

Then, onCreate:

AppBarLayout appbar = findViewById(...);
appbar.addOnOffsetChangedListener(new OnOffsetChangedListener() {
    @Override
    void onOffsetChanged(AppBarLayout appbar, int offset) {
        // Allow recycler scrolling only if we started collapsing.
        this.shouldScroll = offset != 0;
    }
});

现在,将滚动侦听器添加到RecyclerView.每当它尝试滚动时,如果AppBarLayout仍处于展开状态,请还原滚动:

Now, add a scroll listener to your RecyclerView. Whenever it tries to scroll, revert the scroll if the AppBarLayout is still expanded:

RecyclerView recycler = findViewById(...);
recycler.addOnScrollListener(new OnScrollListener() {
    @Override
    void onScrolled(RecyclerView recycler, int dx, int dy) {
        // If AppBar is fully expanded, revert the scroll.
        if (!shouldScroll) {
            recycler.scrollTo(0,0);
        }
    }
});

这可能需要一些调整.我看到两个问题:

This might need some tweaking though. I see two issues:

  • 如果scrollTo()回调onScrolled(),则可能的堆栈溢出.可以使用布尔值或通过删除/添加滚动侦听器来解决
  • 可能您不仅希望在AppBarLayout完全展开时阻止滚动,而且还希望在没有折叠AppBarLayout时阻止滚动.这意味着您不必检查offset != 0,而是检查offset == appBarLayout.getTotalScrollRange().我想.
  • Possible stack overflow if scrollTo() calls onScrolled() back. Can be solved with a boolean or by removing/adding the scroll listener
  • Possibly you want to prevent scrolling not only when AppBarLayout is fully expanded, but more generally when AppBarLayout is not collapsed. This means you don’t have to check for offset != 0, but rather for offset == appBarLayout.getTotalScrollRange(). I think.

这篇关于在折叠AppBarLayout之前,防止RecyclerView在AppBarLayout下滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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