Android RecyclerView平滑滚动以查看其动画高度 [英] Android RecyclerView smooth scroll to view that's animating their height

查看:463
本文介绍了Android RecyclerView平滑滚动以查看其动画高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有可扩展子视图的RecyclerView,当单击子ViewGroup时,它会将ViewGroup高度的视图数量从0增加到测量的视图组高度,如下面的gif所示:

I have a RecyclerView with Expandable Child Views, when the child ViewGroup is clicked it inflates an amount of views animating the ViewGroup height from 0 to the measured viewgroup height, like the following gif:

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

The problem is: I'm calling smoothScrollToPosition on recyclerView, it smooth scroll to the view position, but it considers the current view height, which is still not expanded, in the above gif i'm touching on the under view of the recyclerview, which dont scroll to position because the view is already visible, but when i touch again (calling the smoothscrolltoposition again) it scroll the view to the correct position, because the view is already expanded.

有没有办法将视图滚动到屏幕顶部或只是滚动以使内容可见?

Is there any approach to scroll the view to the top of screen or just scroll to make content visible?

对于引用:
这是一个名为的方法夸大观点:

For references: This is the method called to inflate the views:

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


推荐答案

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

My solution was to constant check for view bottom within applyTransformation method, and compare it with RecyclerView height, if the bottom get higher than the RV height, i scroll by the diff values:

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来获取查看顶部,因此不向上滚动使顶部不可见。

The trick was to use Math.min to get the view top, so it don't scroll up making the top not visible.

基于 ListViewAnimations

这篇关于Android RecyclerView平滑滚动以查看其动画高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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