我有一个方法,它执行一个片段添加动画,一个新片段填满所有屏幕:
public void addFragmentWithAnimation(Fragment fragment, boolean addToBackStack){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right, R.anim.slide_in_left, R.anim.slide_out_right);
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.add(R.id.lay_fragments, fragment);
commitTransaction(transaction);
}
但是当动画播放用户仍然允许点击下面视图上的按钮并进行其他ui动作时,这可能会导致不可预测的结果。有没有办法阻止应用程序中的所有用户操作,直到动画结束?
答案 0 :(得分:6)
Beloo对非支持片段的回答的修改使代码封装得更好一些。只需使用监听器的开始和停止来设置和清除标志......
@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
@Override
public void onAnimationEnd(Animator animator) {
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});
}
return animator;
}
答案 1 :(得分:3)
由于@yedidyak建议我添加了片段动画的监听器,如下所示: 在基本父片段中:
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
Animation animation;
if (nextAnim == 0 ){
animation = super.onCreateAnimation(transit, enter, nextAnim);
} else {
animation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
// additional functionality
}
});
}
return animation;
}
在活动中添加片段方法我添加了阻止用户触摸事件的代码:
public void addFragmentWithAnimation(Fragment fragment, boolean addToBackStack, @MagicConstant FragmentAnimation animation){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
FragmentTransaction transaction = ...
}
FLAG_NOT_TOUCHABLE被添加到代码的片段事务部分,因为用户可以在它和onAnimationStart调用之间调度一些事件。
如果有人知道一种简单的方法,那将会很感激。