Card flip animation in FragmentStatePagerAdapter - android

I am using FragmentStatepagerAdapter.My fragment is just atttached to a viewpager and is not inside a special container.the problem with setCustomAnimation is that if I want to use the flip-card effect, then I have to specify a container that contains the fragment which is to replaced with the animation.Possible solution??

Related

how to apply shared element animation in ViewPager fragments?

I want to animate a view with SharedElement transition from one Fragment to another fragment in ViewPager.
I've 3 fragments in a ViewPager, each fragment is having a single TextView with same transition name in XML file.
Now, when ViewPager slides another Fragment, I want to see SharedElement in action.
I’ve had the same issue. Standard shared element transitions are not working between ViewPager pages.
So I wrote a Shared Element View Pager library. It works both for transitions caused by onClick() events (such as viewPager.setCurrentItem(x) ) and page slides caused by user. The animation is bound to finger movement.
It's not an android transition so there could be some obstacles. Take a look though.
SharedElementPageTransformer demo
Usage
Add all fragments from your ViewPager to the List in the same order.
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(hello_fragment);
fragments.add(small_picture_fragment);
Create SharedElementPageTransformer presumably in Activity.onCreate().
this refers to activity:
SharedElementPageTransformer transformer =
new SharedElementPageTransformer(this, fragments);
Add shared transition by passing pairs of view ids, that need to be linked together
transformer.addSharedTransition(R.id.smallPic_image_cat, R.id.bigPic_image_cat);
Set our transformer to ViewPager's pageTransformer AND onPageChangeListener.
viewPager.setPageTransformer(false, transformer);
viewPager.addOnPageChangeListener(transformer);
it's been maybe a too long time but could be helpful though.
It's totally possible to have shared elements transitions between 2 viewPagers since i already did it myself.
I think the thing you are maybe missing is that transition name has to be unique !
So it has to be set programmatically and not in XML ! It's very important in ViewPager since same view could be inflated many times.
imageView.setTransitionName("unique_name");
This way the transition knows on what element it should be played;)

Regarding to the contradiction of using ViewPager

My question is in my activity or fragment, there are TabLayout and ViewPager. If I was to disable swipe of ViewPager by using a class called NonSwipeableViewPager. Then why should I bother to using ViewPager right from the start, I could have just use TabLayout to add tabs which is much more simpler.
What are the other advantages of using ViewPager even though the primary function of it which is Creating Swipe Views with Tabs is not needed?
Do Fragment layout must use framelayout? Which means can I use relative or liner layout instead of framelayout?
Last but not least, do I allowed to use ViewPager with the swipe disabled?

How can I animate a Fragment inside a viewpager?

I have got a viewpager that hosts two Fragments. Now I would like to replace one of the Fragments on a button click. How can I animate this replacement? (e.g. using the flip-card effect)
Since it is difficult two put all my code in here, this is what I basically use
http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
You can animate it using a PageTransformer
Or, if you want the animation on replacing a fragment, you can set a custom animation on the FragmentTransaction

Mixing layout values each other when use same layout for different fragments in ViewPager

In my Android application there are two fragments called FragmentA and FragmentB. I added both fragments to a ViewPager using a FragmentAdapter So when I run the application I'm getting a unexpected layout (both fragment layouts are mixed together). Whenever I tried changing the currentItem of ViewPager by swiping for each time the layout is mixing each other.
My question: Is there any problem if we use same layout to different fragments in a ViewPager?
I found the answer.
There is no problem if you use getView() function of Fragment to refer the current root view of a fragment. So that you can get exact views in the Fragment. You can use like this(getView().findViewByid(...);
But there is a problem if you use activity reference to refer views in the fragment. Because all views in each fragment which is using same layout xml has same id. So if you change the value of a view in a fragment that will reflect in other fragments which are using the same layout in ViewPager. One more thing, think like this, when the activity created, all the fragments in the ViewPager of that activity will also be created and runs in background. So if you change the value of a view, android will return 1st view which is having the same id. Android knows only id of views. So always refer views of fragment using it root view (getView()).

FragmentPagerAdapter inside Fragment

I'm having a bit of trouble implementing a design based around multiple ViewPagers.
At a high level, I have a FragmentActivity with just a FrameLayout as it's content. I have 3 different Fragments that I want to display. All 3 are full screen and only 1 will be used at a time.
Fragment 1 is a basic fragment with some TextViews and ImageViews.
Fragment 2 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments.
Fragment 3 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments (that are different from Fragment 2)
In my FragmentActivity onCreate() I get the FragmentManager and begin a transaction to replace whatever is in my FrameLayout with a new instance of Fragment 2.
At this point everything is working as expected. My ViewPager in Fragment 2 works perfectly.
Now I have a menu option that replaces the Fragment 2 in my FrameLayout with a new instance of Fragment 3. This also works fine.
The problem arises when I try to put Fragment 2 back into the FrameLayout with another replace transaction. I see my PagerIndicater at the top, but my pages are blank.
I've tried just creating a new instance of my Fragment 2 and calling a replace transaction. I've also tried setting a tag on my Fragments when I call replace and adding a findFragmentByTag check before my replace instead of creating a new instance. Both gave me the same result of blank pages after my second replace.
For reference
My first design was simply a FragmentActivity with a ViewPager and a ViewIndicater. I only had Fragment 2 and Fragment 3 from my description above and a menu option to switch between them. To switch I had 2 different FragmentPagerAdapters defined and just called ViewPager.setAdapter to set the selected FragmentPagerAdapter. This was working perfectly, but now I need a new top level Fragment that isn't using ViewPager at all. This is why I decided to move my ViewPagers out into their own Fragments. My idea being that I would just swap in my fragments to a FrameLayout.
I don't have my code in front of me right now so I can't post any, but I'll add some code to my question tomorrow to help facilitate answers.
This question is a possible duplicate of Navigating back to FragmentPagerAdapter -> fragments are empty.
If your app can handle it (API 17), use getChildFragmentManager(). This problem seems to occur when using a Fragment to host your ViewPager and using FragmentPagerAdapter. Changing to FragmentStatePagerAdapter seemed to fix the problem as well, but I still think using getChildFragmentManager() is the smartest thing to do.
brockoli you can used not "good way". But for my it's worked.
You can use in view 2 layouts, where you bind fragments. First - for fragment with fragments. Second - for other fragments.
On replace fragment with fragments do not replace, but only change visibility first layout to gone and add new fragment to second layout. On back, remove fragment from second layout and set visibility for first layout to visible.

Categories

Resources