检测具有多个色谱柱的RecyclerView顶部?

时间:2017-01-11 06:36:54

标签: android android-recyclerview android-appbarlayout gridlayoutmanager

我上面有RecyclerView,我有一个AppBarLayout,其高度大于255像素。当用户滚动RecyclerView时,AppBarLayout会出现问题。为了避免这种情况,我决定手动扩展AppBarLayout。我的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);
                }
            }
        }
    });

但问题是,现在我无法向上滚动Recyclerview

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是使用动画滚动应用栏。

 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();
        }
    }

检查应用栏是否已折叠

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