简体   繁体   English

检测到RecyclerView的顶部有多于一列?

[英]Detect top of RecyclerView which has more than one column?

I have RecyclerView above that i have an AppBarLayout whose height is larger than 255 px. 我在上面的RecyclerView中有一个高度大于255像素的AppBarLayout。 When user scrolls RecyclerView, AppBarLayout has an fling issue. 当用户滚动RecyclerView时,AppBarLayout有一个问题。 To avoid that i decided to expand AppBarLayout manually. 为了避免这种情况,我决定手动扩展AppBarLayout。 My RecyclerView made of GridLayoutManager with span of 3. I used below code to listen RecyclerView top reach 我的RecyclerView由GridLayoutManager组成,跨度为3。我使用下面的代码来监听RecyclerView的顶部

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                int firstVisiblePosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
                if (firstVisiblePosition == 0) {
                    appBarLayout.setExpanded(true, true);
                }
            }
        }
    });

But issue is that now i can't scroll up the recyclerview 但问题是,现在我无法向上滚动recyclerview

one possible solution is to scroll app bar with animation. 一种可能的解决方案是滚动带有动画的应用栏。

 if (mIsAppBarExpanded) {
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
        final AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
        if (behavior != null) {
            ValueAnimator valueAnimator = ValueAnimator.ofInt();
            valueAnimator.setInterpolator(new DecelerateInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    behavior.setTopAndBottomOffset((Integer) animation.getAnimatedValue());
                    mAppBarLayout.requestLayout();
                }
            });
            valueAnimator.setIntValues(0, -mAppBarLayout.getTotalScrollRange());
            valueAnimator.setDuration(400);
            valueAnimator.start();
        }
    }

TO check app bar already collapsed or not 检查应用栏是否已折叠

 mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            mIsAppBarExpanded = Math.abs(verticalOffset) != appBarLayout.getTotalScrollRange();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM