简体   繁体   中英

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. 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.

Also, remove the call to " scrollToPosition " from within the animation's applyTransformation method. 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.

Also, instead of scrollToPosition , use smoothScrollToPosition for a nice smooth scroll effect.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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