简体   繁体   English

RecyclerView scrollToPositionWithOffset与动画

[英]RecyclerView scrollToPositionWithOffset with animation

Im trying to animate a card view's position and resize it to fit screen at the same time. 我试图设置卡片视图的位置动画,并同时调整其大小以适合屏幕。 When the user clicks a button inside the cardview, the cardview should expand to the size of its container, and scroll up to become fully visible at the same time 当用户单击卡片视图内的按钮时,卡片视图应扩展到其容器的大小,并向上滚动以同时完全可见

in my custom animator class, im using the following function: 在我的自定义动画师类中,即时通讯使用以下功能:

    @Override
    protected void applyTransformation(float interpolatedTimeTransformation t) {
    int newHeight = (int) (startHeight + (targetHeight - startHeight) *interpolatedTime);

    view.getLayoutParams().height   = newHeight;
    ((LinearLayoutManager) MainPage.mainGroupRecycler.getLayoutManager()).scrollToPositionWithOffset(MainPage.mainGroupRecycler.getChildAdapterPosition((CardView) view.getParent()), 0);

    view.requestLayout();
    }

in the following case, the recyclerview instantly shows the cardview at the top of the visible area without animating the scrolling, then it animates the resize. 在以下情况下,recyclerview会立即在可见区域的顶部显示cardview而不显示滚动动画,然后为调整大小动画。 I need it to scroll at the same time the resize is happening. 我需要它在调整大小的同时滚动。

i tried calling another scroll function: 我试图调用另一个滚动功能:

           MainPage.mainGroupRecycler.scrollToPosition(MainPage.mainGroupRecycler.getChildAdapterPosition((CardView) view.getParent()));

But the problem with this is that the scrolling will only animate after the resize is complete. 但是,这样做的问题是,滚动仅在调整大小后才会动画。

I need the resizing and the scrolling to happen simultaneously. 我需要同时调整大小和滚动。

Any help will be appreciated 任何帮助将不胜感激

For the two effects to occur in tandem, you must start the animation of the view, and call scrollToPosition at the same time. 为了使两个效果同时发生,必须启动视图的动画,并同时调用scrollToPosition

Also, remove the call to " scrollToPosition " from within the animation's applyTransformation method. 同样,从动画的applyTransformation方法中删除对“ scrollToPosition ”的调用。 This is bad because it's telling the layout manager that it needs to scroll to a certain position on every iteration of the animation, which could occur hundreds of times. 这很不好,因为它告诉布局管理器它需要在动画的每次迭代中滚动到某个位置,这可能会发生数百次。 You only need to call scrollToPosition once. 您只需要调用一次scrollToPosition

Also, instead of scrollToPosition , use smoothScrollToPosition for a nice smooth scroll effect. 另外,可以使用smoothScrollToPosition而不是scrollToPosition来获得良好的平滑滚动效果。

I figured out the solution. 我想出了解决方案。

In my animator class: 在我的动画师课程中:

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t){

    calculatedOffset = (int) (tempOffset*interpolatedTime);
    tempOffset = tempOffset-calculatedOffset;
    recyclerView.scrollBy(0,calculatedOffset);
    int newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime);
    LinearLayout ll = (LinearLayout)view.findViewById(R.id.cardchild);
    ll.setLayoutParams(new CardView.LayoutParams(ll.getWidth(),newHeight));

}

A note to anyone using a similar approach, make sure to set recyclerView.setItemViewCacheSize(itemList.size) if u have a large number of items because if you dont, the cached objects will be reused thus resizing multiple items and not just the item u clicked 给使用类似方法的任何人的注释,如果您有大量项目,请确保设置recyclerView.setItemViewCacheSize(itemList.size),因为如果您不这样做,则缓存的对象将被重用,从而调整多个项目的大小,而不仅仅是项目u点击

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

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