您可以根据CompoundButton.OnCheckedChangeListener
您的Checkbox
. 与输入参数一样,提供对View
您希望通过平滑过渡折叠/展开的引用。
/**
* Allows a view to be collapsed and Gone with a smooth animation
* @param v view to be collapsed and gone
*/
public static void collapseView(final View v) {
final int initialHeight = v.getMeasuredHeight();
final Animation a = new Animation(){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(500);
v.startAnimation(a);
}
/**
* Allows a collapsed and Gone view to be expanded and Visible
* @param v view to be expanded and Visible
*/
public static void expandView(final View v) {
v.measure(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
final int targtetHeight = v.getMeasuredHeight();
v.getLayoutParams().height = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation(){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? LayoutParams.WRAP_CONTENT
: (int)(targtetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(500);
v.startAnimation(a);
}