Skip to content

Instantly share code, notes, and snippets.

@omegasoft7
Created March 2, 2016 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omegasoft7/513604f60a08342d885e to your computer and use it in GitHub Desktop.
Save omegasoft7/513604f60a08342d885e to your computer and use it in GitHub Desktop.
Make expandable Vertical LinearLayout.Height of view can be wrap content.open/close/toggle can be animated or without animation
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
/**
* Created by farhad on 16.2.3.
* this is an expandable linear Layout with orientation of Vertivally
* it will handle whole calculations and animations for height of View
* Your view can have wrap content, it will calculate exact height after adding views to the layout
*/
public class ExpandableVerticalLinearLayout extends LinearLayout {
private int mHeight = 0;
private boolean listenMeasure = false;
private int mCurrentStatus = 0;
public ExpandableVerticalLinearLayout(Context context) {
super(context);
init();
}
public ExpandableVerticalLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ExpandableVerticalLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setOrientation(VERTICAL);
}
@Override
public void addView(View child) {
listenMeasure = true;
super.addView(child);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
checkHeight();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
checkHeight();
}
private void checkHeight() {
if (listenMeasure && getMeasuredHeightAndState() != 0) {
listenMeasure = false;
mHeight = getMeasuredHeightAndState();
if (mCurrentStatus != 0) {
if (mCurrentStatus == 1) open(true);
if (mCurrentStatus == -1) close(true);
}
}
}
public void toggle() {
if (getVisibility() == GONE) {
open();
} else {
close();
}
}
public void close(boolean byForce) {
//Check if it is open
if (!byForce && mCurrentStatus == -1) return;
mCurrentStatus = -1;
if (listenMeasure) return;
if (byForce) {
getLayoutParams().height = 0;
requestLayout();
} else {
HYResizeAnimation resizeAnimation = new HYResizeAnimation(this);
resizeAnimation.setTargetSize(-1, 0);
resizeAnimation.setDuration(500);
startAnimation(resizeAnimation);
}
}
public void close() {
close(false);
}
public void open() {
open(false);
}
public void open(boolean byForce) {
//Check if it is close
if (!byForce && mCurrentStatus == 1) return;
mCurrentStatus = 1;
if (listenMeasure) return;
if (byForce) {
getLayoutParams().height = mHeight;
requestLayout();
} else {
HYResizeAnimation resizeAnimation = new HYResizeAnimation(this);
resizeAnimation.setTargetSize(-1, mHeight);
resizeAnimation.setDuration(500);
startAnimation(resizeAnimation);
}
}
public boolean isOpen() {
return mCurrentStatus != -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment