我尝试了这个主题中的各种解决方案(参见Android: Expand/collapse animation),但它们都没有真正为我工作。我相信票数最高的那些是好的,而它不能工作的原因是我不理解某些东西。
我的问题的主要点是,当我点击元素“relativeZaplon”时,它可以很好地展开,但当我想要折叠它时,它并没有响应。 MainActivity
我的问题的主要点是,当我点击元素“relativeZaplon”时,它可以很好地展开,但当我想要折叠它时,它并没有响应。 MainActivity
public class MainActivity extends AppCompatActivity {
private boolean isVisible = false;
private RelativeLayout mRelativeZaplon;
private RelativeLayout mRelativeToSlide;
private ExpandOrCollapse mAnimationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAnimationManager = new ExpandOrCollapse();
mRelativeZaplon = (RelativeLayout) findViewById(R.id.relativeZaplon);
mRelativeToSlide = (RelativeLayout) findViewById(R.id.relativevToSlide);
mRelativeZaplon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isVisible) {
mAnimationManager.collapse(mRelativeToSlide, 1000, 200);
isVisible = false;
} else if (!isVisible){
mAnimationManager.expand(mRelativeToSlide, 1000, 200);
isVisible = true;
}
}
});
}
}
展开/折叠类
public class ExpandOrCollapse {
public static void expand(final View v, int duration, int targetHeight) {
int prevHeight = v.getHeight();
v.setVisibility(View.VISIBLE);
ValueAnimator valueAnimator = ValueAnimator.ofInt(prevHeight, targetHeight);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
v.getLayoutParams().height = (int) animation.getAnimatedValue();
v.requestLayout();
}
});
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.setDuration(duration);
valueAnimator.start();
}
public static void collapse(final View v, int duration, int targetHeight) {
int prevHeight = v.getHeight();
ValueAnimator valueAnimator = ValueAnimator.ofInt(prevHeight, targetHeight);
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
v.getLayoutParams().height = (int) animation.getAnimatedValue();
v.requestLayout();
}
});
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.setDuration(duration);
valueAnimator.start();
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:background="@color/colorPrimaryDark"
android:id="@+id/relativevToSlide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeZaplon"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_below="@+id/relativevToSlide"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</RelativeLayout>
</LinearLayout>