【问题标题】:Android RecyclerView smooth scroll to view that's animating their heightAndroid RecyclerView 平滑滚动以查看动画其高度
【发布时间】:2015-03-17 12:07:55
【问题描述】:

我有一个带有可扩展子视图的 RecyclerView,当单击子 ViewGroup 时,它会膨胀一定数量的视图,将 ViewGroup 高度从 0 设置为测量的视图组高度,如下图所示:

问题是:我在recyclerView上调用smoothScrollToPosition,它平滑滚动到视图位置,但它考虑了当前视图高度,它仍然没有扩展,在上面的gif中我正在触摸下面的视图recyclerview,它不会滚动到位置,因为视图已经可见,但是当我再次触摸(再次调用 smoothscrolltoposition)时,它会将视图滚动到正确的位置,因为视图已经展开。

是否有任何方法可以将视图滚动到屏幕顶部或仅滚动以使内容可见?

参考: 这是为视图膨胀而调用的方法:

collapsible_content.removeAllViews();
for(int i = 0; i < 5; i++) {
        View link_view = getLayoutInflater().inflate(R.layout.list_item_timeline_step_link, collapsible_content, false);
        TextView text = (TextView) link_view.findViewById(R.id.step_link_text);
        text.setText("Test");
        collapsible_content.addView(link_view);
    }

这是我的扩展方法:

public void toggle() {
        collapsible_content.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        Animation a;
        if (mExpanded) {
            a = new ExpandAnimation(collapsible_content.getLayoutParams().height, 0);
        } else {
            a = new ExpandAnimation(collapsible_content.getLayoutParams().height, getMeasuredHeight());
        }
        a.setDuration(mAnimationDuration);
        collapsible_content.startAnimation(a);
        mExpanded = !mExpanded;
    }

还有动画:

private class ExpandAnimation extends Animation {
        private final int mStartHeight;
        private final int mDeltaHeight;

        public ExpandAnimation(int startHeight, int endHeight) {
            mStartHeight = startHeight;
            mDeltaHeight = endHeight - startHeight;
        }

        @Override
        protected void applyTransformation(float interpolatedTime,
                                           Transformation t) {
            final int newHeight = (int) (mStartHeight + mDeltaHeight *
                    interpolatedTime);
            collapsible_content getLayoutParams().height = newHeight;

            if (newHeight <= 0) {
                collapsible_content setVisibility(View.GONE);
            } else {
                collapsible_content setVisibility(View.VISIBLE);
            }
            collapsible_content requestLayout();
        }


        @Override
        public boolean willChangeBounds() {
            return true;
        }
    }

【问题讨论】:

    标签: java android android-animation android-recyclerview android-scroll


    【解决方案1】:

    我的解决方案是在 applyTransformation 方法中不断检查视图底部,并将其与 RecyclerView 高度进行比较,如果底部高于 RV 高度,我会滚动差异值:

    final int bottom = collapsible_content.getBottom();
    final int listViewHeight = mRecyclerView.getHeight();
    if (bottom > listViewHeight) {
        final int top = collapsible_content.getTop();
        if (top > 0) {
            mRecyclerView.smoothScrollBy(0, Math.min(bottom - listViewHeight + mRecyclerView.getPaddingBottom(), top));
        }
    }
    

    诀窍是使用 Math.min 来获取视图顶部,因此它不会向上滚动,从而使顶部不可见。

    基于ListViewAnimations的解决方案

    【讨论】:

      【解决方案2】:

      添加一个animationlistener,在展开动画结束后开始recyclerview的滚动。

      【讨论】:

      • 我这样做了,但是我想在视图动画时滚动,我的方法是添加一个动画侦听器来检查视图的底部 Y 是否超出了 RecyclerView,然后调用 smoothscrollby 给像素的值,但我没有管理如何做到这一点
      • 好的,所以你想要并行动画
      • 我认为你必须“伪造”视图的高度来假装更高的视图
      • 假视图高度是什么意思?
      • 也许在展开时给视图一个固定的高度而不是 wrap_content
      猜你喜欢
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 2017-03-09
      • 2011-01-05
      • 2017-04-23
      • 1970-01-01
      相关资源
      最近更新 更多