Skip to content

Commit f861395

Browse files
committed
rename OnStateViewCreatedListener
1 parent abaa66a commit f861395

7 files changed

Lines changed: 231 additions & 37 deletions

File tree

README.md

Lines changed: 164 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# MultiStateLayout
22
[![](https://jitpack.io/v/andyxialm/MultiStateLayout.svg)](https://jitpack.io/#andyxialm/MultiStateLayout)
33

4+
(中文版本请参看[这里](#README_cn))
45

56
A customize multiple state layout for Android.
67

@@ -21,7 +22,7 @@ allprojects {
2122
##### Step 2. Add the dependency
2223
~~~ xml
2324
dependencies {
24-
compile 'com.github.andyxialm:MultiStateLayout:0.0.8'
25+
compile 'com.github.andyxialm:MultiStateLayout:0.1.0'
2526
}
2627
~~~
2728

@@ -41,7 +42,7 @@ dependencies {
4142
<dependency>
4243
<groupId>com.github.andyxialm</groupId>
4344
<artifactId>MultiStateLayout</artifactId>
44-
<version>0.0.8</version>
45+
<version>0.1.0</version>
4546
</dependency>
4647
~~~
4748

@@ -97,6 +98,24 @@ mMultiStateLayout.setState(MultiStateLayout.State.ERROR);
9798

9899
mMultiStateLayout.setState(MultiStateLayout.State.NETWORK_ERROR);
99100

101+
mMultiStateLayout.setOnStateViewCreatedListener(new OnStateViewCreatedListener() {
102+
@Override
103+
public void onViewCreated(View view, int state) {
104+
switch (state) {
105+
case MultiStateLayout.State.NETWORK_ERROR:
106+
view.findViewById(R.id.btn_reload).setOnClickListener(new View.OnClickListener() {
107+
@Override
108+
public void onClick(View view) {
109+
}
110+
});
111+
break;
112+
...
113+
default:
114+
break;
115+
}
116+
}
117+
});
118+
100119
```
101120

102121
##### How to add customize state view?
@@ -122,6 +141,149 @@ mStateLayout.setTransitionAnimator(new TransitionAnimatorLoader() {
122141
});
123142
```
124143

144+
------------------------------
145+
## <a name="README_cn">MultiStateLayout</a>
146+
147+
[![](https://jitpack.io/v/andyxialm/MultiStateLayout.svg)](https://jitpack.io/#andyxialm/MultiStateLayout)
148+
149+
可支持自定义状态的多状态视图组件。
150+
151+
![](https://github.com/andyxialm/MultiStateLayout/blob/master/art/screenshot.gif?raw=true)
152+
### 引入方式
153+
154+
#### Gradle
155+
##### Step 1. Add the JitPack repository to your build file
156+
~~~ xml
157+
allprojects {
158+
repositories {
159+
...
160+
maven { url "https://jitpack.io" }
161+
}
162+
}
163+
~~~
164+
165+
##### Step 2. Add the dependency
166+
~~~ xml
167+
dependencies {
168+
compile 'com.github.andyxialm:MultiStateLayout:0.1.0'
169+
}
170+
~~~
171+
172+
#### Maven
173+
##### Step 1. Add the JitPack repository to your build file
174+
~~~ xml
175+
<repositories>
176+
<repository>
177+
<id>jitpack.io</id>
178+
<url>https://jitpack.io</url>
179+
</repository>
180+
</repositories>
181+
~~~
182+
183+
##### Step 2. Add the dependency
184+
~~~ xml
185+
<dependency>
186+
<groupId>com.github.andyxialm</groupId>
187+
<artifactId>MultiStateLayout</artifactId>
188+
<version>0.1.0</version>
189+
</dependency>
190+
~~~
191+
192+
##### 布局文件中增加 MultiStateLayout,包裹内容布局:
193+
194+
~~~ xml
195+
<cn.refactor.multistatelayout.MultiStateLayout
196+
android:id="@+id/multi_state_layout"
197+
xmlns:android="http://schemas.android.com/apk/res/android"
198+
xmlns:state="http://schemas.android.com/apk/res-auto"
199+
android:layout_width="match_parent"
200+
android:layout_height="match_parent"
201+
state:layout_network_error="@layout/layout_custom_network_error"
202+
state:animEnable="true"
203+
state:animDuration="500">
204+
205+
<!-- content layout -->
206+
<TextView
207+
android:layout_width="wrap_content"
208+
android:layout_height="wrap_content"
209+
android:layout_gravity="center"
210+
android:text="Hello World!"/>
211+
212+
</cn.refactor.multistatelayout.MultiStateLayout>
213+
~~~
214+
215+
##### 配置公共属性
216+
```java
217+
public class App extends Application {
218+
@Override
219+
public void onCreate() {
220+
super.onCreate();
221+
222+
MultiStateConfiguration.Builder builder = new MultiStateConfiguration.Builder();
223+
builder.setCommonEmptyLayout(R.layout.layout_empty)
224+
.setCommonErrorLayout(R.layout.layout_error)
225+
.setCommonLoadingLayout(R.layout.layout_loading);
226+
MultiStateLayout.setConfiguration(builder);
227+
}
228+
}
229+
```
230+
231+
##### 如何切换状态?
232+
```java
233+
234+
mMultiStateLayout.setState(MultiStateLayout.State.CONTENT);
235+
236+
mMultiStateLayout.setState(MultiStateLayout.State.EMPTY);
237+
238+
mMultiStateLayout.setState(MultiStateLayout.State.LOADING);
239+
240+
mMultiStateLayout.setState(MultiStateLayout.State.ERROR);
241+
242+
mMultiStateLayout.setState(MultiStateLayout.State.NETWORK_ERROR);
243+
244+
mMultiStateLayout.setOnStateViewCreatedListener(new OnStateViewCreatedListener() {
245+
@Override
246+
public void onViewCreated(View view, int state) {
247+
switch (state) {
248+
case MultiStateLayout.State.NETWORK_ERROR:
249+
view.findViewById(R.id.btn_reload).setOnClickListener(new View.OnClickListener() {
250+
@Override
251+
public void onClick(View view) {
252+
}
253+
});
254+
break;
255+
...
256+
default:
257+
break;
258+
}
259+
}
260+
});
261+
262+
```
263+
264+
##### 如何添加自定义的状态视图?
265+
```java
266+
View customStateView = LayoutInflater.from(this).inflate(R.layout.layout_custom_notice, mStateLayout, false);
267+
mStateLayout.putCustomStateView(KEY_CUSTOM_STATE, customStateView);
268+
```
269+
##### 切换至自定义状态
270+
```java
271+
mStateLayout.setCustomState(KEY_CUSTOM_STATE);
272+
```
273+
274+
##### 如何自定义切换状态时的过渡动画?
275+
```java
276+
mStateLayout.setTransitionAnimator(new TransitionAnimatorLoader() {
277+
@Override
278+
public ObjectAnimator loadAnimator(View targetView) {
279+
ObjectAnimator customAnimator = ObjectAnimator.ofFloat(targetView, "alpha", 0.0f, 1.0f)
280+
.setDuration(500);
281+
customAnimator.setInterpolator(new AccelerateInterpolator());
282+
return customAnimator;
283+
}
284+
});
285+
```
286+
125287
### License
126288

127289
Copyright 2017 andy (https://github.com/andyxialm)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016 andy
2+
* Copyright 2017 andy (https://github.com/andyxialm)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 andy
2+
Copyright 2017 andy (https://github.com/andyxialm)
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ public class MultiStateLayout extends FrameLayout {
6363

6464
private ObjectAnimator mAlphaAnimator;
6565
private TransitionAnimatorLoader mTransitionAnimatorLoader;
66-
private OnViewCreatedListener mOnViewCreatedListener;
66+
private OnStateViewCreatedListener mOnStateViewCreatedListener;
6767

6868
@IntDef({State.CONTENT, State.EMPTY, State.LOADING, State.ERROR, State.NETWORK_ERROR})
6969
@Retention(RetentionPolicy.SOURCE)
@@ -144,13 +144,13 @@ public static void setConfiguration(MultiStateConfiguration.Builder builder) {
144144
}
145145

146146
/**
147-
* set OnViewCreatedListener
147+
* set OnStateViewCreatedListener
148148
*
149-
* @param l OnViewCreatedListener
149+
* @param l OnStateViewCreatedListener
150150
*/
151151
@SuppressWarnings("unused")
152-
public void setOnViewCreatedListener(OnViewCreatedListener l) {
153-
mOnViewCreatedListener = l;
152+
public void setOnStateViewCreatedListener(OnStateViewCreatedListener l) {
153+
mOnStateViewCreatedListener = l;
154154
}
155155

156156
/**
@@ -679,13 +679,13 @@ private void showNetworkErrorView() {
679679
private int getCommonLayoutResIdByState(@State int state) {
680680
switch (state) {
681681
case State.EMPTY:
682-
return mCommonConfiguration == null ? -1 : mCommonConfiguration.getCommonEmptyLayout();
682+
return null == mCommonConfiguration ? -1 : mCommonConfiguration.getCommonEmptyLayout();
683683
case State.LOADING:
684-
return mCommonConfiguration == null ? -1 : mCommonConfiguration.getCommonLoadingLayout();
684+
return null == mCommonConfiguration ? -1 : mCommonConfiguration.getCommonLoadingLayout();
685685
case State.ERROR:
686-
return mCommonConfiguration == null ? -1 : mCommonConfiguration.getCommonErrorLayout();
686+
return null == mCommonConfiguration ? -1 : mCommonConfiguration.getCommonErrorLayout();
687687
case State.NETWORK_ERROR:
688-
return mCommonConfiguration == null ? -1 : mCommonConfiguration.getCommonNetworkErrorLayout();
688+
return null == mCommonConfiguration ? -1 : mCommonConfiguration.getCommonNetworkErrorLayout();
689689
case State.CONTENT:
690690
return -1;
691691
}
@@ -698,7 +698,7 @@ private int getCommonLayoutResIdByState(@State int state) {
698698
* @return animEnable
699699
*/
700700
private boolean isCommonAnimEnable() {
701-
return mCommonConfiguration != null && mCommonConfiguration.isAnimEnable();
701+
return null != mCommonConfiguration && mCommonConfiguration.isAnimEnable();
702702
}
703703

704704
/**
@@ -707,7 +707,7 @@ private boolean isCommonAnimEnable() {
707707
* @return animDuration
708708
*/
709709
private int getCommonAnimDuration() {
710-
return mCommonConfiguration == null ? DEFAULT_ANIM_DURATION : mCommonConfiguration.getAnimDuration();
710+
return null == mCommonConfiguration ? DEFAULT_ANIM_DURATION : mCommonConfiguration.getAnimDuration();
711711
}
712712

713713
/**
@@ -717,8 +717,8 @@ private int getCommonAnimDuration() {
717717
* @param state state
718718
*/
719719
private void callViewCreated(View view, int state) {
720-
if (mOnViewCreatedListener != null) {
721-
mOnViewCreatedListener.onViewCreated(view, state);
720+
if (null != mOnStateViewCreatedListener) {
721+
mOnStateViewCreatedListener.onViewCreated(view, state);
722722
}
723723
}
724724

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2017 andy (https://github.com/andyxialm)
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.
15+
*/
16+
17+
package cn.refactor.multistatelayout;
18+
19+
import android.view.View;
20+
21+
/**
22+
* Created by andy (https://github.com/andyxialm)
23+
* Creation time: 17/3/23 22:23
24+
* Description: OnStateViewCreatedListener
25+
*/
26+
public interface OnStateViewCreatedListener {
27+
28+
/**
29+
* Called on state view created.
30+
* @param view state view
31+
* @param state state
32+
*/
33+
void onViewCreated(View view, int state);
34+
}

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/**
2+
* Copyright 2017 andy (https://github.com/andyxialm)
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.
15+
*/
16+
117
package cn.refactor.multistatelayout;
218

319
import android.animation.ObjectAnimator;

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

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

1414
import cn.refactor.multistatelayout.MultiStateLayout;
15-
import cn.refactor.multistatelayout.OnViewCreatedListener;
15+
import cn.refactor.multistatelayout.OnStateViewCreatedListener;
1616

1717
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
1818

@@ -46,7 +46,7 @@ private void setupViews() {
4646
mStateLayout.putCustomStateView(KEY_CUSTOM_NOTICE, customNoticeMsgView);
4747

4848
// set view created listener
49-
mStateLayout.setOnViewCreatedListener(new OnViewCreatedListener() {
49+
mStateLayout.setOnStateViewCreatedListener(new OnStateViewCreatedListener() {
5050
@Override
5151
public void onViewCreated(View view, int state) {
5252
switch (state) {

0 commit comments

Comments
 (0)