Skip to content

Commit dffcb75

Browse files
committed
add OnViewCreatedListener
1 parent 5b23ad3 commit dffcb75

5 files changed

Lines changed: 100 additions & 34 deletions

File tree

multistatelayout/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ group='com.github.andyxialm'
44

55
android {
66
compileSdkVersion 25
7-
buildToolsVersion '25.0.1'
7+
buildToolsVersion '25.0.2'
88

99
defaultConfig {
1010
minSdkVersion 14
@@ -25,6 +25,6 @@ android {
2525

2626
dependencies {
2727
compile fileTree(include: ['*.jar'], dir: 'libs')
28-
compile 'com.android.support:support-annotations:25.0.1'
28+
compile 'com.android.support:support-annotations:25.3.0'
2929
}
3030

multistatelayout/src/main/java/cn/refactor/multistatelayout/MultiStateLayout.java

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
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

1717
package 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
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.refactor.multistatelayout;
2+
3+
import android.view.View;
4+
5+
/**
6+
* Created by andy (https://github.com/andyxialm)
7+
* Creation time: 17/3/23 22:23
8+
* Description: OnViewCreatedListener
9+
*/
10+
public interface OnViewCreatedListener {
11+
12+
/**
13+
* Called on state view created.
14+
* @param view state view
15+
* @param state state
16+
*/
17+
void onViewCreated(View view, int state);
18+
}

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 25
5-
buildToolsVersion '25.0.1'
5+
buildToolsVersion '25.0.2'
66
defaultConfig {
77
applicationId "cn.refactor.multistatelayoutdemo"
88
minSdkVersion 14
@@ -27,7 +27,7 @@ dependencies {
2727
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2828
exclude group: 'com.android.support', module: 'support-annotations'
2929
})
30-
compile 'com.android.support:appcompat-v7:25.0.1'
30+
compile 'com.android.support:appcompat-v7:25.3.0'
3131
testCompile 'junit:junit:4.12'
3232
compile project(':multistatelayout')
3333
}

sample/src/main/java/cn/refactor/multistatelayoutdemo/MainActivity.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.widget.Toast;
1313

1414
import cn.refactor.multistatelayout.MultiStateLayout;
15+
import cn.refactor.multistatelayout.OnViewCreatedListener;
1516

1617
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
1718

@@ -40,20 +41,28 @@ private void setupViews() {
4041
// setup toolbar
4142
setupToolbar();
4243

43-
// setup network error view
44-
View networkErrorView = mStateLayout.getNetworkErrorView();
45-
if (null != mStateLayout.getNetworkErrorView()) {
46-
networkErrorView.findViewById(R.id.btn_reload).setOnClickListener(new View.OnClickListener() {
47-
@Override
48-
public void onClick(View view) {
49-
Toast.makeText(MainActivity.this, getString(R.string.reload), Toast.LENGTH_SHORT).show();
50-
}
51-
});
52-
}
53-
5444
// setup custom notice message view
5545
View customNoticeMsgView = LayoutInflater.from(this).inflate(R.layout.layout_custom_notice, mStateLayout, false);
5646
mStateLayout.putCustomStateView(KEY_CUSTOM_NOTICE, customNoticeMsgView);
47+
48+
// set view created listener
49+
mStateLayout.setOnViewCreatedListener(new OnViewCreatedListener() {
50+
@Override
51+
public void onViewCreated(View view, int state) {
52+
switch (state) {
53+
case MultiStateLayout.State.NETWORK_ERROR:
54+
view.findViewById(R.id.btn_reload).setOnClickListener(new View.OnClickListener() {
55+
@Override
56+
public void onClick(View view) {
57+
Toast.makeText(MainActivity.this, getString(R.string.reload), Toast.LENGTH_SHORT).show();
58+
}
59+
});
60+
break;
61+
default:
62+
break;
63+
}
64+
}
65+
});
5766
}
5867

5968
private void setupToolbar() {

0 commit comments

Comments
 (0)