diff --git a/README.md b/README.md index 56a8fb2..94826f7 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,22 @@ expandableLayout.setListener(new ExpandableLayoutListener() { public void onClosed() { } }); +``` +* `ExpandableLayoutListenerAdapter` + - You can set listeners only you need. + +```java + +expandableLayout.setListener(new ExpandableLayoutListenerAdapter() { + @Override + public void onPreOpen() { + } + + @Override + public void onPreClose() { + } +}); + ``` ### Attributes @@ -166,6 +182,8 @@ expandableLayout.setListener(new ExpandableLayoutListener() { |:-:|:-:| |ael_duration|The length of the expand or collapse animation| |ael_expanded|The layout is expanded if you set true| +|ael_defaultChildIndex|The layout is expanded at index of child view. (Only `ExpandableRelativeLayout`)| +|ael_defaultPosition|The layout is expanded at the position. (Only `ExpandableRelativeLayout`)| |ael_orientation|The orientation of animation(horizontal \| vertical)| |ael_interpolator|Sets [interpolator](#interpolator)| @@ -211,7 +229,7 @@ buildscript { } dependencies { - compile 'com.github.aakira:expandable-layout:1.3.0@aar' + compile 'com.github.aakira:expandable-layout:1.4.0@aar' } ``` diff --git a/gradle.properties b/gradle.properties index 55d8708..57e0c46 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,8 +2,8 @@ COMPILE_SDK_VERSION=23 BUILD_TOOLS_VERSION=22.0.1 MIN_SDK_VERSION=11 TARGET_SDK_VERSION=23 -VERSION_CODE=4 -VERSION_NAME=1.3.0 +VERSION_CODE=5 +VERSION_NAME=1.4.0 SUPPORT_APP_COMPAT_VERSION=23.0.1 diff --git a/library/src/androidTest/java/jp/android/aakira/expandablelayout/ApplicationTest.java b/library/src/androidTest/java/jp/android/aakira/expandablelayout/ApplicationTest.java deleted file mode 100644 index 4340eb0..0000000 --- a/library/src/androidTest/java/jp/android/aakira/expandablelayout/ApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package jp.android.aakira.expandablelayout; - -import android.app.Application; -import android.test.ApplicationTestCase; - -/** - * Testing Fundamentals - */ -public class ApplicationTest extends ApplicationTestCase { - public ApplicationTest() { - super(Application.class); - } -} \ No newline at end of file diff --git a/library/src/main/java/com/github/aakira/expandablelayout/ExpandableLayout.java b/library/src/main/java/com/github/aakira/expandablelayout/ExpandableLayout.java index 306491c..54a1c3a 100644 --- a/library/src/main/java/com/github/aakira/expandablelayout/ExpandableLayout.java +++ b/library/src/main/java/com/github/aakira/expandablelayout/ExpandableLayout.java @@ -51,8 +51,10 @@ public interface ExpandableLayout { /** * Initializes this layout. + * + * @param isMaintain The state of expanse is maintained if you set true. */ - void initLayout(); + void initLayout(final boolean isMaintain); /** * Sets the expandable layout listener. diff --git a/library/src/main/java/com/github/aakira/expandablelayout/ExpandableLayoutListenerAdapter.java b/library/src/main/java/com/github/aakira/expandablelayout/ExpandableLayoutListenerAdapter.java new file mode 100644 index 0000000..174f1e0 --- /dev/null +++ b/library/src/main/java/com/github/aakira/expandablelayout/ExpandableLayoutListenerAdapter.java @@ -0,0 +1,40 @@ +package com.github.aakira.expandablelayout; + +public abstract class ExpandableLayoutListenerAdapter implements ExpandableLayoutListener { + /** + * {@inheritDoc} + */ + @Override + public void onAnimationStart() { + } + + /** + * {@inheritDoc} + */ + public void onAnimationEnd() { + } + + /** + * {@inheritDoc} + */ + public void onPreOpen() { + } + + /** + * {@inheritDoc} + */ + public void onPreClose() { + } + + /** + * {@inheritDoc} + */ + public void onOpened() { + } + + /** + * {@inheritDoc} + */ + public void onClosed() { + } +} \ No newline at end of file diff --git a/library/src/main/java/com/github/aakira/expandablelayout/ExpandableRelativeLayout.java b/library/src/main/java/com/github/aakira/expandablelayout/ExpandableRelativeLayout.java index 35a373b..7c3de98 100644 --- a/library/src/main/java/com/github/aakira/expandablelayout/ExpandableRelativeLayout.java +++ b/library/src/main/java/com/github/aakira/expandablelayout/ExpandableRelativeLayout.java @@ -24,6 +24,14 @@ public class ExpandableRelativeLayout extends RelativeLayout implements Expandab private boolean isExpanded; private TimeInterpolator interpolator = new LinearInterpolator(); private int orientation; + /** + * You cannot define {@link #isExpanded}, {@link #defaultChildIndex} + * and {@link #defaultChildPosition} at the same time. + * {@link #defaultChildPosition} has priority over {@link #isExpanded} + * and {@link #defaultChildIndex} if you set them at the same time. + */ + private int defaultChildIndex; + private int defaultChildPosition; /** * The close position is width from left of layout if orientation is horizontal. * The close position is height from top of layout if orientation is vertical. @@ -65,6 +73,10 @@ private void init(final Context context, final AttributeSet attrs, final int def duration = a.getInteger(R.styleable.expandableLayout_ael_duration, DEFAULT_DURATION); isExpanded = a.getBoolean(R.styleable.expandableLayout_ael_expanded, DEFAULT_EXPANDED); orientation = a.getInteger(R.styleable.expandableLayout_ael_orientation, VERTICAL); + defaultChildIndex = a.getInteger(R.styleable.expandableLayout_ael_defaultChildIndex, + Integer.MAX_VALUE); + defaultChildPosition = a.getInteger(R.styleable.expandableLayout_ael_defaultPosition, + Integer.MIN_VALUE); final int interpolatorType = a.getInteger(R.styleable.expandableLayout_ael_interpolator, Utils.LINEAR_INTERPOLATOR); interpolator = Utils.createInterpolator(interpolatorType); @@ -107,11 +119,19 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (isArranged) { return; } + if (isExpanded) { setLayoutSize(layoutSize); } else { setLayoutSize(closePosition); } + final int childNumbers = childPositionList.size(); + if (childNumbers > defaultChildIndex && childNumbers > 0) { + moveChild(defaultChildIndex, 0, null); + } + if (defaultChildPosition > 0 && layoutSize >= defaultChildPosition && layoutSize > 0) { + move(defaultChildPosition, 0, null); + } isArranged = true; if (savedState == null) { @@ -168,7 +188,8 @@ public void expand() { if (isAnimating) { return; } - createExpandAnimator(getCurrentPosition(), layoutSize).start(); + createExpandAnimator(getCurrentPosition(), layoutSize, + duration, interpolator).start(); } /** @@ -179,17 +200,18 @@ public void collapse() { if (isAnimating) { return; } - createExpandAnimator(getCurrentPosition(), closePosition).start(); + createExpandAnimator(getCurrentPosition(), closePosition, + duration, interpolator).start(); } /** * {@inheritDoc} */ @Override - public void initLayout() { + public void initLayout(final boolean isMaintain) { closePosition = 0; layoutSize = 0; - isArranged = false; + isArranged = isMaintain; isCalculatedSize = false; savedState = null; @@ -214,6 +236,7 @@ public void setDuration(final int duration) { @Override public void setExpanded(boolean expanded) { isExpanded = expanded; + isArranged = false; requestLayout(); } @@ -233,31 +256,53 @@ public void setInterpolator(@NonNull final TimeInterpolator interpolator) { this.interpolator = interpolator; } + /** + * @param position + * @see #move(int, long, TimeInterpolator) + */ + public void move(int position) { + move(position, duration, interpolator); + } + /** * Moves to position * * @param position + * @param duration + * @param interpolator */ - public void move(int position) { + public void move(int position, long duration, TimeInterpolator interpolator) { if (isAnimating) { return; } if (0 > position || layoutSize < position) { return; } - createExpandAnimator(getCurrentPosition(), position).start(); + createExpandAnimator(getCurrentPosition(), position, + duration, interpolator).start(); } /** - * Moves to bottom(VERTICAL) or right(HORIZONTAL) of child view - * * @param index child view index + * @see #moveChild(int, long, TimeInterpolator) */ public void moveChild(int index) { + moveChild(index, duration, interpolator); + } + + /** + * Moves to bottom(VERTICAL) or right(HORIZONTAL) of child view + * + * @param index index child view index + * @param duration + * @param interpolator + */ + public void moveChild(int index, long duration, TimeInterpolator interpolator) { if (isAnimating) { return; } - createExpandAnimator(getCurrentPosition(), childPositionList.get(index)).start(); + createExpandAnimator(getCurrentPosition(), childPositionList.get(index), + duration, interpolator).start(); } /** @@ -325,10 +370,6 @@ public void setClosePositionIndex(final int childIndex) { this.closePosition = getChildPosition(childIndex); } - private void updateLayout() { - super.requestLayout(); - } - private boolean isVertical() { return orientation == VERTICAL; } @@ -348,9 +389,12 @@ private void setLayoutSize(int size) { * * @param from * @param to + * @param duration + * @param interpolator * @return */ - private ValueAnimator createExpandAnimator(final int from, final int to) { + private ValueAnimator createExpandAnimator( + final int from, final int to, final long duration, final TimeInterpolator interpolator) { final ValueAnimator valueAnimator = ValueAnimator.ofInt(from, to); valueAnimator.setDuration(duration); valueAnimator.setInterpolator(interpolator); @@ -362,7 +406,7 @@ public void onAnimationUpdate(final ValueAnimator animator) { } else { getLayoutParams().width = (int) animator.getAnimatedValue(); } - updateLayout(); + requestLayout(); } }); valueAnimator.addListener(new AnimatorListenerAdapter() { diff --git a/library/src/main/java/com/github/aakira/expandablelayout/ExpandableWeightLayout.java b/library/src/main/java/com/github/aakira/expandablelayout/ExpandableWeightLayout.java index 885eca1..7d8cdd5 100644 --- a/library/src/main/java/com/github/aakira/expandablelayout/ExpandableWeightLayout.java +++ b/library/src/main/java/com/github/aakira/expandablelayout/ExpandableWeightLayout.java @@ -175,9 +175,9 @@ public void collapse() { * {@inheritDoc} */ @Override - public void initLayout() { + public void initLayout(final boolean isMaintain) { layoutWeight = 0; - isArranged = false; + isArranged = isMaintain; isCalculatedSize = false; savedState = null; diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index b4e8ef0..fbd6abe 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -3,6 +3,8 @@ + + diff --git a/sample/src/main/java/jp/android/aakira/sample/expandablelayout/examplerecyclerview/RecyclerViewRecyclerAdapter.java b/sample/src/main/java/jp/android/aakira/sample/expandablelayout/examplerecyclerview/RecyclerViewRecyclerAdapter.java index 70bd52c..3b1719f 100644 --- a/sample/src/main/java/jp/android/aakira/sample/expandablelayout/examplerecyclerview/RecyclerViewRecyclerAdapter.java +++ b/sample/src/main/java/jp/android/aakira/sample/expandablelayout/examplerecyclerview/RecyclerViewRecyclerAdapter.java @@ -12,7 +12,7 @@ import android.widget.TextView; import com.github.aakira.expandablelayout.ExpandableLayout; -import com.github.aakira.expandablelayout.ExpandableLayoutListener; +import com.github.aakira.expandablelayout.ExpandableLayoutListenerAdapter; import com.github.aakira.expandablelayout.ExpandableRelativeLayout; import com.github.aakira.expandablelayout.Utils; @@ -49,37 +49,21 @@ public void onBindViewHolder(final ViewHolder holder, final int position) { holder.expandableLayout.setBackgroundColor(resource.getColor(item.colorId2)); holder.expandableLayout.setInterpolator(item.interpolator); holder.expandableLayout.setExpanded(expandState.get(position)); - holder.expandableLayout.setListener(new ExpandableLayoutListener() { - @Override - public void onAnimationStart() { - } - - @Override - public void onAnimationEnd() { - - } - + holder.expandableLayout.setListener(new ExpandableLayoutListenerAdapter() { @Override public void onPreOpen() { createRotateAnimator(holder.buttonLayout, 0f, 180f).start(); + expandState.put(position, true); } @Override public void onPreClose() { createRotateAnimator(holder.buttonLayout, 180f, 0f).start(); - } - - @Override - public void onOpened() { - expandState.put(position, true); - } - - @Override - public void onClosed() { expandState.put(position, false); } }); + holder.buttonLayout.setRotation(expandState.get(position) ? 180f : 0f); holder.buttonLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { @@ -116,4 +100,4 @@ public ObjectAnimator createRotateAnimator(final View target, final float from, animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR)); return animator; } -} +} \ No newline at end of file diff --git a/sample/src/main/java/jp/android/aakira/sample/expandablelayout/expandablelayout/ExpandableLayoutActivity.java b/sample/src/main/java/jp/android/aakira/sample/expandablelayout/expandablelayout/ExpandableLayoutActivity.java index 5a766f6..2256284 100644 --- a/sample/src/main/java/jp/android/aakira/sample/expandablelayout/expandablelayout/ExpandableLayoutActivity.java +++ b/sample/src/main/java/jp/android/aakira/sample/expandablelayout/expandablelayout/ExpandableLayoutActivity.java @@ -8,6 +8,7 @@ import android.widget.Button; import com.github.aakira.expandablelayout.ExpandableRelativeLayout; + import jp.android.aakira.sample.expandablelayout.R; public class ExpandableLayoutActivity extends AppCompatActivity implements View.OnClickListener { @@ -33,8 +34,8 @@ protected void onCreate(Bundle savedInstanceState) { mExpandButton = (Button) findViewById(R.id.expandButton); mMoveChildButton = (Button) findViewById(R.id.moveChildButton); mMoveChildButton2 = (Button) findViewById(R.id.moveChildButton2); - mMoveTopButton = (Button)findViewById(R.id.moveTopButton); - mSetCloseHeihgtButton = (Button) findViewById(R.id.setCloseHeightButton); + mMoveTopButton = (Button) findViewById(R.id.moveTopButton); + mSetCloseHeihgtButton = (Button) findViewById(R.id.setCloseHeightButton); mExpandLayout = (ExpandableRelativeLayout) findViewById(R.id.expandableLayout); mExpandButton.setOnClickListener(this); mMoveChildButton.setOnClickListener(this);