4

My app does the following:

  • There are a set of "cards" in a ViewPager. The user can swipe through the cards/pages, with a PageTransformer animation as the user swipes left and right.

  • The user can flip between viewing the front and back of the cards with a card flip animation (as described in this Android tutorial)

  • I need to support API 14+

I have tried various approaches, but each has a problem.

Approach 1: Nested fragments

I have a ViewPager at the top level, which swipes between "card container" fragments. Each card container fragment is responsible for handling the flipping, by replacing the contents of a FrameLayout with either a front/back card fragment.

Problem: This works fine but only on API 17+, I need to support API 14+.

Approach 2: Nested fragments with v4 support library

Changing all fragments, activities etc. to v4 alternatives eliminates the compiler warning about requiring API 17, but the card flip animation no longer works and I receive an error "java.lang.RuntimeException: Unknown animation name: objectAnimator"

I'd rather avoid using the support library classes anyway (except for the ViewPager from v13 support), since the rest of the app uses native fragments and I would rather keep it consistent.

Approach 3: Avoiding nested fragments and instead just replacing the fragment inside the ViewPager

This is my preferred approach. There is a single top level ViewPager, and the FragmentStatePagerAdapter decides which fragment to return based on whether we are viewing front/back.

When the flip button is pressed, the pages in the ViewPager are invalidated and the FragmentStatePagerAdapter is forced to call getItem() again, returning a new fragment for the opposite side of the card. I got this to work with a little help from this post.

This is nearly working, but I do not know how to apply the card flip animation since the ViewPager replaces the fragment using magic and I don't have access to the fragment transaction or container view.

So my main question is, how can I apply a custom animation when replacing (not swiping) a page in a ViewPager using public Fragment getItem(int i) { ... }?

Thanks in advance for any help!

Community
  • 1
  • 1
Matt Wilson
  • 8,159
  • 8
  • 33
  • 55

0 Answers0