Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/Fix Animation #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ expandableLayout.setCollapseListener(new ExpandCollapseListener.CollapseListener
}
});
```

## Set animations
<img src="https://raw.githubusercontent.com/iammert/ExpandableLayout/master/art/animation1.gif"/>

```java
sectionLinearLayout.setExpandableAnimation(new ExpendableScaleAnimation(2000));
```

or create custom animation
```java
sectionLinearLayout.setExpandableAnimation(new ExpandableAnimation(2000) {
@Override
public Animation generateAnimation(View parentView, View childView, ExpandableState expandableState) {
return null;
}
});
```
## Add section or children
```java
Section<FruitCategory, Fruit> section = new Section<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import iammert.com.expandablelib.ExpandCollapseListener;
import iammert.com.expandablelib.ExpandableLayout;
import iammert.com.expandablelib.Section;
import iammert.com.expandablelib.animations.ExpendableScaleAnimation;

public class MainActivity extends AppCompatActivity {

Expand All @@ -27,7 +28,7 @@ protected void onCreate(Bundle savedInstanceState) {

EditText editText = (EditText) findViewById(R.id.edittext);
ExpandableLayout sectionLinearLayout = (ExpandableLayout) findViewById(R.id.el);

sectionLinearLayout.setExpandableAnimation(new ExpendableScaleAnimation(2000));
sectionLinearLayout.setRenderer(new ExpandableLayout.Renderer<FruitCategory, Fruit>() {
@Override
public void renderParent(View view, FruitCategory model, boolean isExpanded, int parentPosition) {
Expand Down
Binary file added art/animation1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package iammert.com.expandablelib;

import android.view.View;
import android.view.animation.Animation;

/**
* Created by T0173711 on 13/12/2017.
*/

public abstract class ExpandableAnimation {

private int mExpandableAnimationDuration;

public int getExpandableAnimationDuration() {
return mExpandableAnimationDuration;
}

public ExpandableAnimation(int expandableAnimationDuration){
this.mExpandableAnimationDuration=expandableAnimationDuration;
}

public abstract Animation generateAnimation(View parentView, View childView, ExpandableState expandableState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public boolean apply(Object obj) {

private Operation currentFilter = DEFAULT_FILTER;

public ExpandableAnimation getExpandableAnimation() {
return mExpandableAnimation;
}

public void setExpandableAnimation(ExpandableAnimation expandableAnimation) {
mExpandableAnimation = expandableAnimation;
}

private ExpandableAnimation mExpandableAnimation;

public ExpandableLayout(Context context) {
super(context);
init(context, null);
Expand Down Expand Up @@ -252,9 +262,14 @@ private <P> void expand(@NonNull P parent) {
for (int j = 1; j < sectionView.getChildCount(); j++) {
final View childView = sectionView.getChildAt(j);
final Object childType = sections.get(i).children.get(j - 1);
childView.setVisibility(currentFilter.apply(childType) ? View.VISIBLE : View.GONE);
sectionViewAnimateVisibility(sectionView,childView,currentFilter.apply(childType) ?
ExpandableState.EXPAND
: ExpandableState.COLLAPSE);
}
sections.get(i).expanded = true;

// re-render parent for "arrow" animation
notifyParentChanged(i);
if (expandListener != null)
expandListener.onExpanded(i, sections.get(i).parent, sectionView.getChildAt(0));
break;
Expand All @@ -269,13 +284,23 @@ private <P> void collapse(@NonNull P parent) {
sections.get(i).expanded = false;

for (int j = 1; j < sectionView.getChildCount(); j++) {
sectionView.getChildAt(j).setVisibility(View.GONE);
sectionViewAnimateVisibility(sectionView,sectionView.getChildAt(j),ExpandableState.COLLAPSE);
}

// re-render parent for "arrow" animation
notifyParentChanged(i);
if (collapseListener != null)
collapseListener.onCollapsed(i, sections.get(i).parent, sectionView.getChildAt(0));
break;
}
}
}

private void sectionViewAnimateVisibility(final View parentView,final View childView,final ExpandableState expandableState){
if (mExpandableAnimation !=null){
childView.startAnimation(mExpandableAnimation.generateAnimation(parentView,childView,expandableState));
}else {
childView.setVisibility(expandableState == ExpandableState.EXPAND ? VISIBLE : GONE);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package iammert.com.expandablelib;

/**
* Created by T0173711 on 13/12/2017.
*/

public enum ExpandableState {
EXPAND, COLLAPSE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package iammert.com.expandablelib.animations;

import android.view.View;
import android.view.animation.Animation;

import iammert.com.expandablelib.ExpandableAnimation;
import iammert.com.expandablelib.ExpandableState;

/**
* Created by T0173711 on 13/12/2017.
*/

public class ExpendableScaleAnimation extends ExpandableAnimation {
public ExpendableScaleAnimation(int duration) {
super(duration);
}

@Override
public Animation generateAnimation(View parentView, final View childView, ExpandableState expandableState) {
float startScale;
float endScale;
final int visibility;
switch (expandableState) {
case COLLAPSE:
startScale = 1.F;
endScale = 0.F;
visibility = View.GONE;
break;
case EXPAND:
startScale = 0.F;
endScale = 1.F;
visibility = View.VISIBLE;
// prevent from animation to be "GONE"
childView.setVisibility(visibility);
break;
default:
return null;
}
Animation mAnimation =new android.view.animation.ScaleAnimation(
1f, 1f, // Start and end values for the X axis scaling
startScale, endScale, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
Animation.ABSOLUTE, 1f);
mAnimation.setFillAfter(true); // Needed to keep the result of the animation
mAnimation.setDuration(1000);
mAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
childView.setVisibility(visibility);
}

@Override
public void onAnimationRepeat(Animation animation) {

}
});
return mAnimation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package iammert.com.expandablelib.animations;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout;

import iammert.com.expandablelib.ExpandableAnimation;
import iammert.com.expandablelib.ExpandableState;

import static iammert.com.expandablelib.ExpandableState.COLLAPSE;
import static iammert.com.expandablelib.ExpandableState.EXPAND;

/**
* Created by T0173711 on 13/12/2017.
*/

public class ExpendableScaleAnimationAdvanced extends ExpandableAnimation {

private int mExpendHeight;
private LinearLayout.LayoutParams mLayoutParams;

public ExpendableScaleAnimationAdvanced(int duration) {
super(duration);
}


@Override
public Animation generateAnimation(View parentView, final View childView, final ExpandableState expandableState) {
// FIXME little trick to save height don't work if childViews don't have same height
if (expandableState == COLLAPSE) {
mExpendHeight = childView.getHeight();
}
return new CustomAnimation(childView, expandableState);
}

class CustomAnimation extends Animation {
private View mView;
private ExpandableState mState;
private int mEndHeight;

CustomAnimation(final View childView, final ExpandableState expandableState) {
setDuration(getExpandableAnimationDuration());
mView = childView;
mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
mEndHeight = mView.getMeasuredHeight();
mState = expandableState;
mLayoutParams = ((LinearLayout.LayoutParams) childView.getLayoutParams());
if (mState == EXPAND) {
mLayoutParams.height = 0;
} else {
mLayoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
}
mView.setVisibility(View.VISIBLE);
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {

super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
if (mState == EXPAND) {
mLayoutParams.height = (int) (mEndHeight * (interpolatedTime));
} else {
mLayoutParams.height = (int) (mEndHeight * (1 - interpolatedTime));
}
mView.requestLayout();
} else {
if (mState == EXPAND) {
mLayoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
mView.requestLayout();
} else {
mView.setVisibility(View.GONE);
}
}
}
}
}