我正在开发App,其中一部分是RecyclerView
。其中的每个项目都有3种布局,默认情况下,其中一种始终为VISIBLE
,另外两种为GONE
。单击主(可见)布局/视图后应显示一个,而单击按钮即是主布局/视图的一部分,则应显示另一个。另外,我希望在整个RecyclerView
中一次只能看到一个隐藏的布局。
为使它看起来更加“用户友好”,我想使此显示/隐藏功能具有动画效果,因此看起来新视图正从该项目掉下来(如某些子菜单)或又消失在它后面。
我能够使单个Transition
工作,但是在某些情况下,我需要连续制造两个Transitions
,因为我TransitionManager.beginDelayedTransition
不是在这种情况下工作。我尝试了不同的方法,在我看来,最好的方法是使用TransitionSet
,但我无法使其正常工作。
这是我上次尝试的代码:
// called in onClick methods
private void setNewActiveLayout(ConstraintLayout newActiveLayout, int position){
TransitionSet set = new TransitionSet();
// activeLayout is currently visible view, if they are all GONE, it is set to null..
if (activeLayout == null){
set.addTransition(setActiveLayout(newActiveLayout, position));
} else if (activeLayoutPosition == position){
set.addTransition(hideOldActiveLayout());
if (activeLayout != newActiveLayout){
set.addTransition(setActiveLayout(newActiveLayout, position));
} else {
setNoActiveLayout();
}
} else {
set.addTransition(hideOldActiveLayout());
set.addTransition(setActiveLayout(newActiveLayout, position));
}
TransitionManager.beginDelayedTransition((ViewGroup) newActiveLayout.getParent(), set);
}
private void setNoActiveLayout (){
activeLayout = null;
activeLayoutPosition = -1;
}
private Transition setActiveLayout(ConstraintLayout newActiveLayout, int position){
Transition transition = slideViewDownToShow(newActiveLayout);
activeLayout = newActiveLayout;
activeLayoutPosition = position;
return transition;
}
private Transition hideOldActiveLayout(){
return slideViewUpToHide(activeLayout);
}
private Transition slideViewUpToHide(final View view){
Transition transition = new Slide(Gravity.TOP);
transition.setDuration(400);
transition.setStartDelay(100);
transition.addTarget(view);
transition.addListener(new TransitionListenerAdapter(){
@Override
public void onTransitionEnd(@NonNull Transition transition) {
view.setVisibility(View.GONE);
}
});
return transition;
}
private Transition slideViewDownToShow(final View view){
Transition transition = new Slide(Gravity.TOP);
transition.setStartDelay(250);
transition.setDuration(1000);
transition.addTarget(view);
transition.addListener(new TransitionListenerAdapter(){
@Override
public void onTransitionEnd(@NonNull Transition transition) {
view.setVisibility(View.VISIBLE);
}
});
return transition;
}
这是我能够使用此处的其他问题/答案或在Internet上找到的一些示例所能获得的效果,但无法正常工作。所以我的问题是,如何使用TransitionSet
使它工作,或者也许还有另一种更好的方法?