我正在开发一个应用程序,它的一部分是 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;
}
这是我在这里用其他问题/答案或我在网上找到的一些例子所能达到的程度,但它不起作用。所以我的问题是,如何利用 TransitionSet
,或者也许还有更好的方法?
暂无答案!
目前还没有任何答案,快来回答吧!