1- /**
2- * Copyright 2016 andy
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
1+ /*
2+ Copyright 2016 andy
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
1515 */
1616
1717package cn .refactor .multistatelayout ;
@@ -63,14 +63,15 @@ public class MultiStateLayout extends FrameLayout {
6363
6464 private ObjectAnimator mAlphaAnimator ;
6565 private TransitionAnimatorLoader mTransitionAnimatorLoader ;
66+ private OnViewCreatedListener mOnViewCreatedListener ;
6667
6768 @ IntDef ({State .CONTENT , State .EMPTY , State .LOADING , State .ERROR , State .NETWORK_ERROR })
6869 @ Retention (RetentionPolicy .SOURCE )
6970 public @interface State {
7071 int CONTENT = 0 ;
71- int EMPTY = 1 ;
72+ int EMPTY = 1 ;
7273 int LOADING = 2 ;
73- int ERROR = 3 ;
74+ int ERROR = 3 ;
7475 int NETWORK_ERROR = 4 ;
7576 }
7677
@@ -142,6 +143,16 @@ public static void setConfiguration(MultiStateConfiguration.Builder builder) {
142143 mCommonConfiguration = builder ;
143144 }
144145
146+ /**
147+ * set OnViewCreatedListener
148+ *
149+ * @param l OnViewCreatedListener
150+ */
151+ @ SuppressWarnings ("unused" )
152+ public void setOnViewCreatedListener (OnViewCreatedListener l ) {
153+ mOnViewCreatedListener = l ;
154+ }
155+
145156 /**
146157 * set state
147158 *
@@ -154,7 +165,8 @@ public void setState(@State int state) {
154165
155166 /**
156167 * set state
157- * @param state State
168+ *
169+ * @param state State
158170 * @param displayContentLayout display or conceal content layout
159171 */
160172 @ SuppressLint ("Assert" )
@@ -187,6 +199,7 @@ public void setCustomState(int customStateKey, boolean displayContentLayout) {
187199
188200 /**
189201 * Put customise state view by customise key
202+ *
190203 * @param customStateKey key
191204 * @param stateView view
192205 */
@@ -396,6 +409,7 @@ public View getNetworkErrorView() {
396409
397410 /**
398411 * open/close optional animation
412+ *
399413 * @param animEnable open/close
400414 */
401415 @ SuppressWarnings ("unused" )
@@ -405,6 +419,7 @@ public void setAnimEnable(boolean animEnable) {
405419
406420 /**
407421 * get animation status
422+ *
408423 * @return enable
409424 */
410425 @ SuppressWarnings ("unused" )
@@ -414,6 +429,7 @@ public boolean isAnimEnable() {
414429
415430 /**
416431 * set animation duration
432+ *
417433 * @param duration duration
418434 */
419435 @ SuppressWarnings ("unused" )
@@ -436,6 +452,7 @@ public int getAnimDuration() {
436452 public void setTransitionAnimator (TransitionAnimatorLoader animatorLoader ) {
437453 mTransitionAnimatorLoader = animatorLoader ;
438454 }
455+
439456 /**
440457 * cancel animation
441458 */
@@ -447,7 +464,8 @@ private void clearTargetViewAnimation() {
447464
448465 /**
449466 * hide target view
450- * @param state state
467+ *
468+ * @param state state
451469 * @param displayContentLayout display content layout
452470 */
453471 private void hideViewByState (@ State int state , boolean displayContentLayout ) {
@@ -485,7 +503,8 @@ private void hideViewByState(@State int state, boolean displayContentLayout) {
485503
486504 /**
487505 * hide target view
488- * @param customStateKey custom state key
506+ *
507+ * @param customStateKey custom state key
489508 * @param displayContentLayout display content layout
490509 */
491510 private void hideCustomViewByState (int customStateKey , boolean displayContentLayout ) {
@@ -548,6 +567,7 @@ private void showCustomViewByState(int customStateKey) {
548567
549568 /**
550569 * start alpha animation
570+ *
551571 * @param targetView target view
552572 */
553573 private void execAlphaAnimation (View targetView ) {
@@ -581,6 +601,7 @@ private void showEmptyView() {
581601 if (null == mEmptyView && mEmptyResId > -1 ) {
582602 mEmptyView = mInflater .inflate (mEmptyResId , this , false );
583603 addView (mEmptyView , mEmptyView .getLayoutParams ());
604+ callViewCreated (mEmptyView , State .EMPTY );
584605 }
585606 if (null != mEmptyView ) {
586607 mEmptyView .setVisibility (VISIBLE );
@@ -599,6 +620,7 @@ private void showLoadingView() {
599620 if (null == mLoadingView && mLoadingResId > -1 ) {
600621 mLoadingView = mInflater .inflate (mLoadingResId , this , false );
601622 addView (mLoadingView , mLoadingView .getLayoutParams ());
623+ callViewCreated (mLoadingView , State .LOADING );
602624 }
603625 if (null != mLoadingView ) {
604626 mLoadingView .setVisibility (VISIBLE );
@@ -617,6 +639,7 @@ private void showErrorView() {
617639 if (null == mErrorView && mErrorResId > -1 ) {
618640 mErrorView = mInflater .inflate (mErrorResId , this , false );
619641 addView (mErrorView , mErrorView .getLayoutParams ());
642+ callViewCreated (mErrorView , State .ERROR );
620643 }
621644 if (null != mErrorView ) {
622645 mErrorView .setVisibility (VISIBLE );
@@ -635,6 +658,7 @@ private void showNetworkErrorView() {
635658 if (null == mNetworkErrorView && mNetworkErrorResId > -1 ) {
636659 mNetworkErrorView = mInflater .inflate (mNetworkErrorResId , this , false );
637660 addView (mNetworkErrorView , mNetworkErrorView .getLayoutParams ());
661+ callViewCreated (mNetworkErrorView , State .NETWORK_ERROR );
638662 }
639663 if (null != mNetworkErrorView ) {
640664 mNetworkErrorView .setVisibility (VISIBLE );
@@ -648,6 +672,7 @@ private void showNetworkErrorView() {
648672
649673 /**
650674 * get common layout resource id by state except CONTENT(Content)
675+ *
651676 * @param state state
652677 * @return resource id
653678 */
@@ -669,6 +694,7 @@ private int getCommonLayoutResIdByState(@State int state) {
669694
670695 /**
671696 * get anim status from common settings
697+ *
672698 * @return animEnable
673699 */
674700 private boolean isCommonAnimEnable () {
@@ -677,10 +703,23 @@ private boolean isCommonAnimEnable() {
677703
678704 /**
679705 * get anim duration from common settings
706+ *
680707 * @return animDuration
681708 */
682709 private int getCommonAnimDuration () {
683710 return mCommonConfiguration == null ? DEFAULT_ANIM_DURATION : mCommonConfiguration .getAnimDuration ();
684711 }
685712
713+ /**
714+ * called it on view created
715+ *
716+ * @param view state view
717+ * @param state state
718+ */
719+ private void callViewCreated (View view , int state ) {
720+ if (mOnViewCreatedListener != null ) {
721+ mOnViewCreatedListener .onViewCreated (view , state );
722+ }
723+ }
724+
686725}
0 commit comments