[英]How can I put a container, containing a fragment, inside a ViewPager in order to animate with flip-card effect
我有一个带有片段的ViewPager。 每个片段都由一个ImageView组成,该ImageView的正面带有一个“卡”。 刷卡显示下一张(或上一张)卡。 单击一个按钮即可随机获得其他卡片。 单击另一个按钮将导致显示卡片背面的翻牌动画,或者如果已经显示背面,则将显示倒装动画(同一张卡片)。 为了获得翻页动画,要进行动画处理的片段必须位于容器中,而不是直接位于viewpager中。 但是,当我尝试在viewpager中放入一个片段容器(也是一个片段)时,整个viewpage机制就搞砸了。 无法再正常刷卡。
之前曾问过相同/类似的问题: https : //stackoverflow.com/questions/22171069/how-can-i-animate-a-fragment-inside-a-viewpager? 。 接受的答案不是回答我的问题,而是在评论中表明有更多人在寻找该答案。
我浏览了很多互联网,并且我会继续,但是我决定在这里发布我的问题,因为我发现的大多数问题答案都来自您,stackoverflow-users!
编辑以澄清注释中的问题我要使用的动画与pe中指定的容器一起工作:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:context="nl.xxx.xxx.xxxActivity"
tools:ignore="MergeRootFrame">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
/>
</FrameLayout>
在动画之前,将带有imageView的片段添加到容器(FrontFragment)。 该片段的布局:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imageDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
tools:context="nl.xxx.xxx.xxxActivity" />
动画是(代码的一部分:)
mShowingBack = true;
// Create and commit a new fragment transaction that adds the fragment for the back of
// the card, uses custom animations, and is part of the fragment manager's back stack.
BackFragment achterkant= BackFragment.newInstance("blabla");
getFragmentManager()
.beginTransaction()
// Replace the default fragment animations with animator resources representing
// rotations when switching to the back of the card, as well as animator
// resources representing rotations when flipping back to the front (e.g. when
// the system Back button is pressed).
.setCustomAnimations(
R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_left_in, R.animator.card_flip_left_out)
// Replace any fragments currently in the container view with a fragment
.replace(R.id.container, achterkant)
// Add this transaction to the back stack, allowing users to press Back
// to get to the front of the card.
.addToBackStack(null)
// Commit the transaction.
.commit();
动画在xml文件中给出,例如:card_flip_left_in.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Before rotating, immediately set the alpha to 0. -->
<objectAnimator
android:valueFrom="1.0"
android:valueTo="0.0"
android:propertyName="alpha"
android:duration="0" />
<!-- Rotate. -->
<objectAnimator
android:valueFrom="-180"
android:valueTo="0"
android:propertyName="rotationY"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="@integer/card_flip_time_full" />
<!-- Half-way through the rotation (see startOffset), set the alpha to 1. -->
<objectAnimator
android:valueFrom="0.0"
android:valueTo="1.0"
android:propertyName="alpha"
android:startOffset="@integer/card_flip_time_half"
android:duration="1" />
上面的代码是从Internet复制的,并且在非viewpager情况下可以正常工作。
我自己找到了答案。 我将带有容器的Rootfragments添加到ViewPager。 我在getChildFragmentManager()和上面提到的objectanimators的帮助下替换/设置了容器中的片段的动画。
提示是,我必须将android.support.v13库(FragmentStatePagerAdapter)与android.app.Fragment和android.app.FragmentTransaction结合使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.