4141 */
4242public class MultiStateLayout extends FrameLayout {
4343
44+ private static final int DEFAULT_ANIM_DURATION = 300 ;
4445 private static MultiStateConfiguration .Builder mCommonConfiguration ;
45- private static final int DEFAULT_ANIM_DURATION = 400 ;
46- private static final boolean DEFAULT_ANIM_ENABLE = true ;
47-
4846 private View mContentView ;
4947 private View mLoadingView ;
5048 private View mEmptyView ;
@@ -63,7 +61,7 @@ public class MultiStateLayout extends FrameLayout {
6361
6462 @ IntDef ({State .CONTENT , State .EMPTY , State .LOADING , State .ERROR , State .NETWORK_ERROR })
6563 @ Retention (RetentionPolicy .SOURCE )
66- public @interface State {
64+ public @interface State {
6765 int CONTENT = 0 ;
6866 int EMPTY = 1 ;
6967 int LOADING = 2 ;
@@ -99,8 +97,8 @@ private void init(AttributeSet attrs) {
9997 mLoadingResId = ta .getResourceId (R .styleable .MultiStateLayout_layout_loading , getCommonLayoutResIdByState (State .LOADING ));
10098 mNetworkErrorResId = ta .getResourceId (R .styleable .MultiStateLayout_layout_network_error , getCommonLayoutResIdByState (State .NETWORK_ERROR ));
10199
102- mAnimEnable = ta .getBoolean (R .styleable .MultiStateLayout_animEnable , DEFAULT_ANIM_ENABLE );
103- mAnimDuration = ta .getInt (R .styleable .MultiStateLayout_animDuration , DEFAULT_ANIM_DURATION );
100+ mAnimEnable = ta .getBoolean (R .styleable .MultiStateLayout_animEnable , isCommonAnimEnable () );
101+ mAnimDuration = ta .getInt (R .styleable .MultiStateLayout_animDuration , getCommonAnimDuration () );
104102 ta .recycle ();
105103
106104 mInflater = LayoutInflater .from (getContext ());
@@ -130,6 +128,7 @@ protected void onDetachedFromWindow() {
130128 *
131129 * @param builder MultiStateConfiguration.Builder
132130 */
131+ @ SuppressWarnings ("unused" )
133132 public static void setConfiguration (MultiStateConfiguration .Builder builder ) {
134133 mCommonConfiguration = builder ;
135134 }
@@ -324,6 +323,41 @@ public View getNetworkErrorView() {
324323 return mNetworkErrorView ;
325324 }
326325
326+ /**
327+ * Open/close optional animation
328+ * @param animEnable open/close
329+ */
330+ @ SuppressWarnings ("unused" )
331+ public void setAnimEnable (boolean animEnable ) {
332+ mAnimEnable = animEnable ;
333+ }
334+
335+ /**
336+ * Get animation status
337+ * @return enable
338+ */
339+ @ SuppressWarnings ("unused" )
340+ public boolean isAnimEnable () {
341+ return mAnimEnable ;
342+ }
343+
344+ /**
345+ * Set animation duration
346+ * @param duration duration
347+ */
348+ @ SuppressWarnings ("unused" )
349+ public void setAnimDuration (int duration ) {
350+ mAnimDuration = duration ;
351+ }
352+
353+ /**
354+ * Get animation duration
355+ */
356+ @ SuppressWarnings ("unused" )
357+ public int getAnimDuration () {
358+ return mAnimDuration ;
359+ }
360+
327361 private void clearTargetViewAnimation () {
328362 if (null != mAlphaAnimator && mAlphaAnimator .isRunning ()) {
329363 mAlphaAnimator .cancel ();
@@ -492,16 +526,24 @@ private void showNetworkErrorView() {
492526 private int getCommonLayoutResIdByState (@ State int state ) {
493527 switch (state ) {
494528 case State .EMPTY :
495- return mCommonConfiguration .getCommonEmptyLayout ();
529+ return mCommonConfiguration == null ? - 1 : mCommonConfiguration .getCommonEmptyLayout ();
496530 case State .LOADING :
497- return mCommonConfiguration .getCommonLoadingLayout ();
531+ return mCommonConfiguration == null ? - 1 : mCommonConfiguration .getCommonLoadingLayout ();
498532 case State .ERROR :
499- return mCommonConfiguration .getCommonErrorLayout ();
533+ return mCommonConfiguration == null ? - 1 : mCommonConfiguration .getCommonErrorLayout ();
500534 case State .NETWORK_ERROR :
501- return mCommonConfiguration .getCommonNetworkErrorLayout ();
535+ return mCommonConfiguration == null ? - 1 : mCommonConfiguration .getCommonNetworkErrorLayout ();
502536 case State .CONTENT :
503537 return -1 ;
504538 }
505539 return 0 ;
506540 }
541+
542+ private boolean isCommonAnimEnable () {
543+ return mCommonConfiguration != null && mCommonConfiguration .isAnimEnable ();
544+ }
545+
546+ private int getCommonAnimDuration () {
547+ return mCommonConfiguration == null ? DEFAULT_ANIM_DURATION : mCommonConfiguration .getAnimDuration ();
548+ }
507549}
0 commit comments