4

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

Zay
  • 115
  • 1
  • 9
  • why are you calling the scroll method during applyTransformation? This will tell the LayoutManager to scroll to the child's position on every iteration of the animation, possibly hundreds of times... – Gil Moshayof Aug 19 '15 at 14:37
  • @Gil Moshayof I am having a similar difficulty as OP (although I'm not using an animation, which maybe I should), and I believe the scroll method is called inside the animation because it is the overload that includes the offset. Since there is not a working "smooth scroll" to top of an item position, this overload is required if you want to scroll to the top of the item. The OP's marked answer though seems a bit hackish with the comment about the cache. It sounds like we are no longer animating the single card, but all of the cards. RecyclerView + CardView animation seems pretty buggy. –  Oct 01 '15 at 13:05
  • Possible duplicate of [RecyclerView - How to smooth scroll to top of item on a certain position?](https://stackoverflow.com/questions/31235183/recyclerview-how-to-smooth-scroll-to-top-of-item-on-a-certain-position) – Alireza Noorali Dec 24 '18 at 12:56

2 Answers2

0

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.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
0

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

Zay
  • 115
  • 1
  • 9
  • 2
    Could you post/point me to your complete solution? I have almost the exact same requirement of CardView + RecyclerView, but I am not understanding how you're applying your animation. Also, do you mean that you are setting the cache size to the size of the entire item list? Does this not effectively remove the usefulness of RecyclerView's ViewHolder pattern? –  Oct 01 '15 at 13:07
  • 1
    Please share the complete solution. Thanks! – Juan Saravia Oct 09 '15 at 12:51