问题描述
我一直在尝试在fragmentStatePagerAdapter内切换片段,但是即使我能够在相同位置从片段C-D进行更改;我无法为过渡设置动画.
I've been trying to switch fragments inside a fragmentStatePagerAdapter, but even though I was able to change from fragment C-D in the same position; I have not been able to animate the transition.
对于实现此效果的任何建议,我将不胜感激
I would appreciate any suggestions to achieve this effect:
A-B-C | ->来回翻转切换 D
A - B - C | -> Flip transition back and forth D
ABC或ABD具有正常的动画过渡,但是在C中,如果我单击一个按钮,我需要将片段D替换为翻转动画,并且如果我正在看D则将其翻转回C.
ABC or ABD have the normal animation transition, but when in C if I click a button I need to swap the fragment D with a flip animation and if I'm looking at D flip back to C.
推荐答案
培训文档详细介绍了如何创建翻牌动画.要实现您所描述的功能,您需要做的就是将ViewPager
的最后两个片段("C"和"D"片段)嵌套在一个片段中.然后将动画应用于过渡:
The training documentation details how to create a card-flip animation. To implement this as you described, all you need to do is nest the last two fragments (the 'C' and 'D' fragments) of the ViewPager
in a single fragment; then apply the animation to the transition:
public class CardFlipFragment extends Fragment {
private boolean mShowingBack = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_card_flip, container, false);
if(savedInstanceState == null) {
getChildFragmentManager()
.beginTransaction()
.add(R.id.container, new FrontFragment())
.commit();
}
return view;
}
public void onFlip(View view) {
if(mShowingBack) {
getChildFragmentManager().popBackStack();
} else {
mShowingBack = true;
getChildFragmentManager()
.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(R.id.container, new BackFragment())
.addToBackStack(null)
.commit();
}
}
}
这篇关于使用翻转动画在视图寻呼机中切换片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!