【问题标题】:Detect top of RecyclerView which has more than one column?检测具有多于一列的 RecyclerView 顶部?
【发布时间】:2017-01-11 06:36:53
【问题描述】:

我有上面的 RecyclerView,我有一个高度大于 255 像素的 AppBarLayout。当用户滚动 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);
                }
            }
        }
    });

但问题是现在我无法向上滚动回收站视图

【问题讨论】:

    标签: android android-recyclerview android-appbarlayout gridlayoutmanager


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 2015-04-20
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 2016-08-10
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多