add initial state attribute to FoldableLinearLayout

This commit is contained in:
Vincent Breitmoser 2017-02-14 16:18:15 +01:00
parent 0b405575c8
commit 623d8a0bf5
2 changed files with 42 additions and 31 deletions

View file

@ -56,6 +56,7 @@ public class FoldableLinearLayout extends LinearLayout {
private String mFoldedLabel; private String mFoldedLabel;
private String mUnFoldedLabel; private String mUnFoldedLabel;
private boolean mInitializeFolded;
public FoldableLinearLayout(Context context) { public FoldableLinearLayout(Context context) {
super(context); super(context);
@ -84,6 +85,7 @@ public class FoldableLinearLayout extends LinearLayout {
R.styleable.FoldableLinearLayout, 0, 0); R.styleable.FoldableLinearLayout, 0, 0);
mFoldedLabel = a.getString(R.styleable.FoldableLinearLayout_foldedLabel); mFoldedLabel = a.getString(R.styleable.FoldableLinearLayout_foldedLabel);
mUnFoldedLabel = a.getString(R.styleable.FoldableLinearLayout_unFoldedLabel); mUnFoldedLabel = a.getString(R.styleable.FoldableLinearLayout_unFoldedLabel);
mInitializeFolded = a.getBoolean(R.styleable.FoldableLinearLayout_initializeFolded, true);
a.recycle(); a.recycle();
} }
// If any attribute isn't found then set a default one // If any attribute isn't found then set a default one
@ -102,6 +104,10 @@ public class FoldableLinearLayout extends LinearLayout {
initialiseInnerViews(); initialiseInnerViews();
if (!mInitializeFolded) {
toggleFolded();
}
super.onFinishInflate(); super.onFinishInflate();
} }
@ -149,42 +155,46 @@ public class FoldableLinearLayout extends LinearLayout {
foldableControl.setOnClickListener(new View.OnClickListener() { foldableControl.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
mFolded = !mFolded; toggleFolded();
if (mFolded) {
mFoldableIcon.setImageResource(R.drawable.ic_expand_less_black_24dp);
mFoldableContainer.setVisibility(View.VISIBLE);
AlphaAnimation animation = new AlphaAnimation(0f, 1f);
animation.setDuration(mShortAnimationDuration);
mFoldableContainer.startAnimation(animation);
mFoldableTextView.setText(mUnFoldedLabel);
} else {
mFoldableIcon.setImageResource(R.drawable.ic_expand_more_black_24dp);
AlphaAnimation animation = new AlphaAnimation(1f, 0f);
animation.setDuration(mShortAnimationDuration);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// making sure that at the end the container is completely removed from view
mFoldableContainer.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mFoldableContainer.startAnimation(animation);
mFoldableTextView.setText(mFoldedLabel);
}
} }
}); });
} }
private void toggleFolded() {
mFolded = !mFolded;
if (mFolded) {
mFoldableIcon.setImageResource(R.drawable.ic_expand_less_black_24dp);
mFoldableContainer.setVisibility(View.VISIBLE);
AlphaAnimation animation = new AlphaAnimation(0f, 1f);
animation.setDuration(mShortAnimationDuration);
mFoldableContainer.startAnimation(animation);
mFoldableTextView.setText(mUnFoldedLabel);
} else {
mFoldableIcon.setImageResource(R.drawable.ic_expand_more_black_24dp);
AlphaAnimation animation = new AlphaAnimation(1f, 0f);
animation.setDuration(mShortAnimationDuration);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// making sure that at the end the container is completely removed from view
mFoldableContainer.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mFoldableContainer.startAnimation(animation);
mFoldableTextView.setText(mFoldedLabel);
}
}
/** /**
* Adds provided child view to foldableContainer View * Adds provided child view to foldableContainer View
* *

View file

@ -8,6 +8,7 @@
<declare-styleable name="FoldableLinearLayout"> <declare-styleable name="FoldableLinearLayout">
<attr name="foldedLabel" format="string" /> <attr name="foldedLabel" format="string" />
<attr name="unFoldedLabel" format="string" /> <attr name="unFoldedLabel" format="string" />
<attr name="initializeFolded" format="boolean" />
</declare-styleable> </declare-styleable>
<!-- Taken from Matt Allen Password Strength View <!-- Taken from Matt Allen Password Strength View