所以我终于想出了如何为一个循环视图做一个爆炸过渡。我想做的是在过渡完成后改变布局边距。如果我只是在转换开始后添加这一行,它就不起作用了。那么,如何设置在转换完成后执行的操作呢?下面是代码:
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Set enormous padding
int prevPreviousBottomPadding = recyclerView.getPaddingBottom();
int prevPreviousTopPadding = recyclerView.getPaddingTop();
recyclerView.setPadding(0,2000,0,2000);
// save rect of view in screen coordinates
final Rect viewRect = new Rect();
//v.getGlobalVisibleRect(viewRect);
//create Explode transition with epicenter
Transition explode = new Explode();
explode.setEpicenterCallback(new Transition.EpicenterCallback() {
@Override
public Rect onGetEpicenter(Transition transition) {
return viewRect;
}
});
explode.setDuration(800);
TransitionManager.beginDelayedTransition(recyclerView, explode);
// remove all views from Recycler View
recyclerView.setAdapter(null);
//I need this line to be executed after the transition is completely over.
recyclerView.setPadding(0,prevPreviousTopPadding,0,prevPreviousBottomPadding);
return false;
}
});
明白了!!在开始动画之前,我们应该添加一个侦听器并覆盖所有必要的方法。下面是代码:
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (isAnimating) {
return false;
}
// save rect of view in screen coordinates
final Rect viewRect = new Rect();
//v.getGlobalVisibleRect(viewRect);
//Set enormous padding
int prevPreviousBottomPadding = recyclerView.getPaddingBottom();
int prevPreviousTopPadding = recyclerView.getPaddingTop();
recyclerView.setPadding(0, 2000, 0, 2000);
//create Explode transition with epicenter
Transition explode = makeInExplodeTransition();
explode.setEpicenterCallback(new Transition.EpicenterCallback() {
@Override
public Rect onGetEpicenter(Transition transition) {
return viewRect;
}
});
//Here it is - adding a listener
explode.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionStart(Transition transition) {
isAnimating = true;
}
@Override
public void onTransitionEnd(Transition transition) {
isAnimating = false;
recyclerView.setPadding(0, prevPreviousTopPadding, 0, prevPreviousBottomPadding);
}
@Override
public void onTransitionCancel(Transition transition) {
}
@Override
public void onTransitionPause(Transition transition) {
}
@Override
public void onTransitionResume(Transition transition) {
}
});
TransitionManager.beginDelayedTransition(recyclerView, explode);
//Deleting everything from our adapter
recyclerView.setAdapter(null);
return false;
}
});