├── .gitignore
├── .idea
├── codeStyles
│ └── Project.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── rzj
│ │ └── stateview
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── rzj
│ │ │ └── stateview
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ ├── ic_launcher_background.xml
│ │ ├── img1.jpeg
│ │ └── img2.jpeg
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ ├── state_data_error.xml
│ │ ├── state_dlrb.xml
│ │ ├── state_empty.xml
│ │ ├── state_loading.xml
│ │ ├── state_net_error.xml
│ │ └── state_yfh.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── rzj
│ └── stateview
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
├── state_view
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── rzj
│ │ └── view
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── rzj
│ │ │ └── view
│ │ │ └── EasyStateView.java
│ └── res
│ │ └── values
│ │ ├── attrs.xml
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── rzj
│ └── view
│ └── ExampleUnitTest.java
└── stateview
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
├── androidTest
└── java
│ └── com
│ └── rzj
│ ├── stateview
│ └── ExampleInstrumentedTest.java
│ └── view
│ └── ExampleInstrumentedTest.java
├── main
├── AndroidManifest.xml
├── java
│ └── com
│ │ └── rzj
│ │ └── view
│ │ └── EasyStateView.java
└── res
│ └── values
│ ├── attrs.xml
│ └── strings.xml
└── test
└── java
└── com
└── rzj
├── stateview
└── ExampleUnitTest.java
└── view
└── ExampleUnitTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches/build_file_checksums.ser
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | .DS_Store
9 | /build
10 | /captures
11 | .externalNativeBuild
12 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://www.jianshu.com/u/a5102f480695)
2 | #### 点击上方图片进入我的博客
3 |
4 | # EasyStateView
5 |
6 | [](https://www.jitpack.io/#MarkRaoAndroid/EasyStateView)
7 |
8 | To get a Git project into your build:
9 |
10 | Step 1. Add the JitPack repository to your build file
11 | Add it in your root build.gradle at the end of repositories:
12 |
13 | ```
14 | allprojects {
15 | repositories {
16 | maven { url 'https://www.jitpack.io' }
17 | }
18 | }
19 | ```
20 | Step 2. Add the dependency
21 | ```
22 | dependencies {
23 | implementation 'com.github.MarkRaoAndroid:EasyStateView: latest-release'
24 | }
25 | ```
26 |
27 | 
28 | ## 控件使用示例
29 | ```
30 |
40 |
41 |
45 |
46 |
47 | ```
48 | 可以通过自定义属性来设置你的布局,中间放你的主题内容 View
49 | 还可以在代码里添加自定义的布局,这里控件内本身自定义了五种布局,通过 esv_viewState 来设置第一个显示在用户的 View
50 |
1,内容 View
51 |
2,加载 View
52 |
3,数据异常( 数据异常指原本应该是有数据,但是服务器返回了错误的、不符合格式的数据 ) View
53 |
4,网络异常 View
54 |
5,数据为空 View
55 |
也可以监听 View 状态的变化
56 | ```
57 | public class MainActivity extends AppCompatActivity implements EasyStateView.StateViewListener {
58 |
59 | private com.rzj.view.EasyStateView mStateView;
60 |
61 | private static final int YFH = 1;
62 |
63 | private static final int DLRB = 2;
64 |
65 | private int mStateId;
66 |
67 | @Override
68 | protected void onCreate(Bundle savedInstanceState) {
69 | super.onCreate(savedInstanceState);
70 | setContentView(R.layout.activity_main);
71 | initViews();
72 | }
73 |
74 |
75 | private void initViews() {
76 |
77 | mStateView = findViewById(R.id.state_view);
78 | // 获取当前显示的 view 状态
79 | mStateId = mStateView.getCurrentState();
80 | // 代码动态添加 View , 注意 tag 值必须大于 0
81 | mStateView.addUserView(YFH, R.layout.state_yfh);
82 |
83 | View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.state_dlrb, mStateView, false);
84 |
85 | mStateView.addUserView(DLRB, view);
86 | // 监听 StateView 状态变化
87 | mStateView.setStateChangedListener(this);
88 | }
89 |
90 | @Override
91 | public void onStateChanged(int state) {
92 | Toast.makeText(MainActivity.this, String.valueOf(state), Toast.LENGTH_LONG).show();
93 | }
94 | }
95 | ```
96 | ### 文档地址:
97 | ### Control description address:
98 | https://www.jianshu.com/p/eaf71002e38f
99 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion project.ext.sdk
5 | defaultConfig {
6 | applicationId "com.rzj.stateview"
7 | minSdkVersion 15
8 | targetSdkVersion project.ext.sdk
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | compileOptions {
20 | sourceCompatibility JavaVersion.VERSION_1_8
21 | targetCompatibility JavaVersion.VERSION_1_8
22 | }
23 | }
24 |
25 |
26 | dependencies {
27 | implementation fileTree(include: ['*.jar'], dir: 'libs')
28 | implementation 'com.android.support:appcompat-v7:28.0.0'
29 | testImplementation 'junit:junit:4.12'
30 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
31 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
32 | implementation project(':state_view')
33 | }
34 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/rzj/stateview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.rzj.stateview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.rzj.stateview", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/rzj/stateview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.rzj.stateview;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.widget.AdapterView;
8 | import android.widget.ArrayAdapter;
9 | import android.widget.Button;
10 | import android.widget.ImageView;
11 | import android.widget.RadioGroup;
12 | import android.widget.Spinner;
13 | import android.widget.Toast;
14 |
15 | import com.rzj.view.EasyStateView;
16 | import com.rzj.view.R;
17 |
18 | public class MainActivity extends AppCompatActivity implements EasyStateView.StateViewListener {
19 |
20 | private com.rzj.view.EasyStateView mStateView;
21 | private Button mBtn;
22 | private Button mLoadingBtn;
23 | private Spinner mSpinner;
24 | private RadioGroup mRadioAnimation;
25 | private int mStateId;
26 | private static final int YFH = 1;
27 | private static final int DLRB = 2;
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_main);
33 | initViews();
34 | initListener();
35 | }
36 |
37 | private void initListener() {
38 | mBtn.setOnClickListener(new View.OnClickListener() {
39 | @Override
40 | public void onClick(View v) {
41 | mStateView.showViewState(mStateId);
42 | }
43 | });
44 | mLoadingBtn.setOnClickListener(new View.OnClickListener() {
45 | @Override
46 | public void onClick(View v) {
47 | mStateView.afterLoadingState(mStateId);
48 | }
49 | });
50 | mStateView.getStateView(DLRB).findViewById(R.id.iv_dlrb)
51 | .setOnClickListener(new View.OnClickListener() {
52 | @Override
53 | public void onClick(View v) {
54 | Toast.makeText(MainActivity.this, "迪丽热巴被点击", Toast.LENGTH_SHORT).show();
55 | }
56 | });
57 | mRadioAnimation.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
58 | @Override
59 | public void onCheckedChanged(RadioGroup group, int checkedId) {
60 | if (checkedId == R.id.use_anima) {
61 | mStateView.setUseAnim(true);
62 | } else {
63 | mStateView.setUseAnim(false);
64 | }
65 | }
66 | });
67 | //适配器
68 | ArrayAdapter arrAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
69 | getResources().getStringArray(R.array.spinner));
70 | //设置样式
71 | arrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
72 | mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
73 | @Override
74 | public void onItemSelected(AdapterView> parent, View view, int position, long id) {
75 | switch (position) {
76 | case 0:
77 | mStateId = EasyStateView.VIEW_EMPTY;
78 | break;
79 | case 1:
80 | mStateId = EasyStateView.VIEW_ERROR_DATA;
81 | break;
82 | case 2:
83 | mStateId = EasyStateView.VIEW_ERROR_NET;
84 | break;
85 | case 3:
86 | mStateId = EasyStateView.VIEW_LOADING;
87 | break;
88 | case 4:
89 | mStateId = EasyStateView.VIEW_CONTENT;
90 | break;
91 | case 5:
92 | mStateId = YFH;
93 | break;
94 | case 6:
95 | mStateId = DLRB;
96 | break;
97 | }
98 | }
99 |
100 | @Override
101 | public void onNothingSelected(AdapterView> parent) {
102 |
103 | }
104 | });
105 | mSpinner.setAdapter(arrAdapter);
106 | }
107 |
108 | private void initViews() {
109 | mBtn = findViewById(R.id.btn_view_state);
110 | mLoadingBtn = findViewById(R.id.btn_after_loading);
111 | mSpinner = findViewById(R.id.spinner_state_view);
112 | mStateView = findViewById(R.id.state_view);
113 | mRadioAnimation = findViewById(R.id.radio_animation);
114 | mStateId = mStateView.getCurrentState();
115 | mStateView.addUserView(YFH, R.layout.state_yfh);
116 | View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.state_dlrb, mStateView, false);
117 | mStateView.addUserView(DLRB, view);
118 | mStateView.setStateChangedListener(this);
119 | }
120 |
121 | @Override
122 | public void onStateChanged(int state) {
123 | Toast.makeText(MainActivity.this, String.valueOf(state), Toast.LENGTH_SHORT).show();
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/img1.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/drawable/img1.jpeg
--------------------------------------------------------------------------------
/app/src/main/res/drawable/img2.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/drawable/img2.jpeg
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
15 |
20 |
27 |
33 |
38 |
43 |
44 |
45 |
50 |
55 |
65 |
66 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/state_data_error.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/state_dlrb.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/state_empty.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/state_loading.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/state_net_error.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/state_yfh.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | EasyStateView
3 |
4 | - emptyView
5 | - errorDataView
6 | - errorNetView
7 | - loadingView
8 | - contentView
9 | - 俞飞鸿
10 | - 迪丽热巴
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/rzj/stateview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.rzj.stateview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 |
2 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
3 |
4 | buildscript {
5 |
6 | repositories {
7 | google()
8 | jcenter()
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.2.1'
12 | // NOTE: Do not place your application dependencies here; they belong
13 | // in the individual module build.gradle files
14 | classpath 'com.novoda:bintray-release:0.9'
15 | }
16 |
17 | }
18 | tasks.withType(Javadoc) {
19 | options{
20 | encoding "UTF-8"
21 | charSet 'UTF-8'
22 | links "http://docs.oracle.com/javase/7/docs/api"
23 | }
24 | }
25 | allprojects {
26 | repositories {
27 | google()
28 | jcenter()
29 | }
30 | project.ext {
31 | supportLibraryVersion = "27.1.1"
32 | buildTools = "28.0.0"
33 | sdk = 28
34 | }
35 | repositories {
36 | jcenter()
37 | }
38 | tasks.withType(Javadoc) {
39 | options.addStringOption('Xdoclint:none', '-quiet')
40 | options.addStringOption('encoding', 'UTF-8')
41 | }
42 | }
43 |
44 | task clean(type: Delete) {
45 | delete rootProject.buildDir
46 | }
47 |
48 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1024m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
16 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkRaoAndroid/EasyStateView/d93c47bd1522e013b3cf7f1939d5d7e6262f4c2f/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':state_view'
2 |
--------------------------------------------------------------------------------
/state_view/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/state_view/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.novoda.bintray-release'
3 | android {
4 |
5 | compileSdkVersion 28
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion 28
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 |
24 | }
25 |
26 |
27 | dependencies {
28 | implementation fileTree(include: ['*.jar'], dir: 'libs')
29 | implementation 'com.android.support:appcompat-v7:28.0.0'
30 | testImplementation 'junit:junit:4.12'
31 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
32 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
33 |
34 | }
35 |
36 | publish {
37 | repoName = 'MyMaven'//远程仓库名字,不指明,默认是上传到maven
38 | userOrg = 'markraostudio' //bintray.com用户名
39 | groupId = 'com.rzj' //jcenter上的路径
40 | artifactId = 'EasyStateView' //项目名称
41 | publishVersion = '1.0.0'//版本号
42 | desc = 'desc'//描述,自由填写
43 | website = 'https://github.com/MarkRaoAndroid' // 网址,自由填写
44 | }
45 |
46 |
--------------------------------------------------------------------------------
/state_view/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/state_view/src/androidTest/java/com/rzj/view/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.rzj.view;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.rzj.stateview.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/state_view/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/state_view/src/main/java/com/rzj/view/EasyStateView.java:
--------------------------------------------------------------------------------
1 | package com.rzj.view;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorListenerAdapter;
5 | import android.animation.ObjectAnimator;
6 | import android.content.Context;
7 | import android.content.res.TypedArray;
8 | import android.os.Parcel;
9 | import android.os.Parcelable;
10 | import android.util.AttributeSet;
11 | import android.util.SparseArray;
12 | import android.view.LayoutInflater;
13 | import android.view.View;
14 | import android.view.ViewGroup;
15 | import android.widget.FrameLayout;
16 |
17 | import com.rzj.stateview.R;
18 |
19 | public class EasyStateView extends FrameLayout {
20 |
21 | // 内容 View
22 | public static final int VIEW_CONTENT = 0;
23 | // 加载 View
24 | public static final int VIEW_LOADING = -1;
25 | // 数据异常( 数据异常指原本应该是有数据,但是服务器返回了错误的、不符合格式的数据 ) View
26 | public static final int VIEW_ERROR_DATA = -2;
27 | // 网络异常 View
28 | public static final int VIEW_ERROR_NET = -3;
29 | // 数据为空 View
30 | public static final int VIEW_EMPTY = -4;
31 | // View 的 Tag 标签值
32 | private static final int VIEW_TAG = -5;
33 | // 用来存放 View
34 | private SparseArray mViews;
35 | // 是否使用过渡动画
36 | private boolean mUseAnim;
37 | // 是否处于动画中
38 | private boolean isAniming;
39 | // 当前显示的 ViewTag
40 | private int mCurrentState;
41 | private Context mContext;
42 | private StateViewListener mListener;
43 | // content View 是否被添加到队列
44 | private boolean isAddContent;
45 |
46 | public interface StateViewListener {
47 | void onStateChanged(int state);
48 | }
49 |
50 | public void setStateChangedListener(StateViewListener listener) {
51 | this.mListener = listener;
52 | }
53 |
54 | public EasyStateView(Context context) {
55 | this(context, null);
56 | }
57 |
58 | public EasyStateView(Context context, AttributeSet attrs) {
59 | this(context, attrs, 0);
60 | }
61 |
62 | public EasyStateView(Context context, AttributeSet attrs, int defStyleAttr) {
63 | super(context, attrs, defStyleAttr);
64 | init(context, attrs);
65 | }
66 |
67 | private void init(Context context, AttributeSet attrs) {
68 | mContext = context;
69 | mViews = new SparseArray<>();
70 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EasyStateView);
71 | mCurrentState = typedArray.getInt(R.styleable.EasyStateView_esv_viewState, VIEW_CONTENT);
72 | int emptyResId = typedArray.getResourceId(R.styleable.EasyStateView_esv_emptyView, VIEW_TAG);
73 | if (emptyResId != VIEW_TAG) {
74 | View view = LayoutInflater.from(getContext()).inflate(emptyResId, this, false);
75 | addViewToHash(view, VIEW_EMPTY);
76 | addViewInLayout(view, -1, view.getLayoutParams());
77 | }
78 | int errorDataResId = typedArray.getResourceId(R.styleable.EasyStateView_esv_errorDataView, VIEW_TAG);
79 | if (errorDataResId != VIEW_TAG) {
80 | View view = LayoutInflater.from(getContext()).inflate(errorDataResId, this, false);
81 | addViewToHash(view, VIEW_ERROR_DATA);
82 | addViewInLayout(view, -1, view.getLayoutParams());
83 | }
84 | int errorNetResId = typedArray.getResourceId(R.styleable.EasyStateView_esv_errorNetView, VIEW_TAG);
85 | if (errorNetResId != VIEW_TAG) {
86 | View view = LayoutInflater.from(getContext()).inflate(errorNetResId, this, false);
87 | addViewToHash(view, VIEW_ERROR_NET);
88 | addViewInLayout(view, -1, view.getLayoutParams());
89 | }
90 | int loadingResId = typedArray.getResourceId(R.styleable.EasyStateView_esv_loadingView, VIEW_TAG);
91 | if (loadingResId != VIEW_TAG) {
92 | View view = LayoutInflater.from(getContext()).inflate(loadingResId, this, false);
93 | addViewToHash(view, VIEW_LOADING);
94 | addViewInLayout(view, -1, view.getLayoutParams());
95 | }
96 | mUseAnim = typedArray.getBoolean(R.styleable.EasyStateView_esv_use_anim, true);
97 | typedArray.recycle();
98 | }
99 |
100 | @Override
101 | public void addView(View child) {
102 | addContentV(child);
103 | super.addView(child);
104 | }
105 |
106 | private boolean isContentView(View child) {
107 | if (!isAddContent && null != child
108 | && null == child.getTag()) {
109 | return true;
110 | }
111 | return false;
112 | }
113 |
114 | private void addContentV(View child) {
115 | if (isContentView(child)) {
116 | addViewToHash(child, VIEW_CONTENT);
117 | isAddContent = true;
118 | }
119 | }
120 |
121 | private void addViewToHash(View child, int viewTag) {
122 | child.setTag(viewTag);
123 | if (viewTag != mCurrentState) {
124 | child.setVisibility(GONE);
125 | }
126 | mViews.put(viewTag, child);
127 | }
128 |
129 | @Override
130 | public void addView(View child, int index) {
131 | addContentV(child);
132 | super.addView(child, index);
133 | }
134 |
135 | @Override
136 | public void addView(View child, int index, ViewGroup.LayoutParams params) {
137 | addContentV(child);
138 | super.addView(child, index, params);
139 | }
140 |
141 | @Override
142 | public void addView(View child, ViewGroup.LayoutParams params) {
143 | addContentV(child);
144 | super.addView(child, params);
145 | }
146 |
147 | @Override
148 | public void addView(View child, int width, int height) {
149 | addContentV(child);
150 | super.addView(child, width, height);
151 | }
152 |
153 | @Override
154 | protected boolean addViewInLayout(View child, int index, ViewGroup.LayoutParams params) {
155 | addContentV(child);
156 | return super.addViewInLayout(child, index, params);
157 | }
158 |
159 | @Override
160 | protected boolean addViewInLayout(View child, int index, ViewGroup.LayoutParams params, boolean preventRequestLayout) {
161 | addContentV(child);
162 | return super.addViewInLayout(child, index, params, preventRequestLayout);
163 | }
164 |
165 |
166 | @Override
167 | protected Parcelable onSaveInstanceState() {
168 | Parcelable parcelable = super.onSaveInstanceState();
169 | int useAnim = 0;
170 | if (mUseAnim) {
171 | useAnim = 1;
172 | }
173 | return new SaveState(parcelable, mCurrentState, useAnim);
174 | }
175 |
176 | @Override
177 | protected void onRestoreInstanceState(Parcelable state) {
178 | SaveState saveState = (SaveState) state;
179 | if (saveState.useAnim == 1) {
180 | mUseAnim = true;
181 | } else {
182 | mUseAnim = false;
183 | }
184 | // 因为应用方向改变触发重绘后,重新初始化读取的 ViewState 是不准确的,所以要隐藏掉
185 | if (saveState.viewState != mCurrentState) {
186 | getStateView(mCurrentState).setVisibility(GONE);
187 | showViewState(saveState.viewState);
188 | }
189 | super.onRestoreInstanceState(saveState.getSuperState());
190 | }
191 |
192 | private static class SaveState extends BaseSavedState {
193 |
194 | private int viewState;
195 | /**
196 | * 布尔值存储居然没有api,只能存储布尔数组,故改成 int 记录
197 | * 1 使用动画
198 | * 2 不使用动画
199 | */
200 | private int useAnim;
201 |
202 | private SaveState(Parcel source) {
203 | super(source);
204 | viewState = source.readInt();
205 | }
206 |
207 | private SaveState(Parcelable superState, int viewState, int useAnim) {
208 | super(superState);
209 | this.viewState = viewState;
210 | this.useAnim = useAnim;
211 | }
212 |
213 | @Override
214 | public void writeToParcel(Parcel out, int flags) {
215 | super.writeToParcel(out, flags);
216 | out.writeInt(viewState);
217 | out.writeInt(useAnim);
218 | }
219 |
220 | public static final Parcelable.Creator CREATE = new Parcelable.Creator() {
221 |
222 | @Override
223 | public SaveState createFromParcel(Parcel source) {
224 | return new SaveState(source);
225 | }
226 |
227 | @Override
228 | public SaveState[] newArray(int size) {
229 | return new SaveState[size];
230 | }
231 | };
232 | }
233 |
234 | /**
235 | * 切换默认状态的 View
236 | *
237 | * @param state
238 | */
239 | public void showViewState(int state) {
240 | if (!checkState(state)) {
241 | showViewAnim(state, VIEW_TAG);
242 | }
243 | }
244 |
245 | /**
246 | * 切换 view 时用 loading view 过渡
247 | *
248 | * @param state
249 | */
250 | public void afterLoadingState(int state) {
251 | if (!checkState(state)) {
252 | if (mCurrentState == VIEW_LOADING) {
253 | showViewAnim(state, VIEW_TAG);
254 | } else {
255 | showViewAnim(VIEW_LOADING, state);
256 | }
257 | }
258 | }
259 |
260 | /**
261 | * 检查状态是否合法
262 | * true 表示不合法,不往下执行
263 | * false 表示该状态和当前状态不同,并合法数值状态
264 | *
265 | * @param state
266 | * @return
267 | */
268 | private boolean checkState(int state) {
269 | if (state <= VIEW_TAG) {
270 | throw new RuntimeException("ViewState 不在目标范围");
271 | }
272 | if (state == mCurrentState) {
273 | return true;
274 | } else if (isAniming) {
275 | return true;
276 | }
277 | return false;
278 | }
279 |
280 | public void setUseAnim(boolean useAnim) {
281 | this.mUseAnim = useAnim;
282 | }
283 |
284 | private void showViewAnim(int showState, int afterState) {
285 | if (!isAniming) {
286 | isAniming = true;
287 | }
288 | View showView = getStateView(showState);
289 | if (null == showView) {
290 | isAniming = false;
291 | return;
292 | }
293 | View currentView = getStateView(mCurrentState);
294 | if (mUseAnim) {
295 | showAlpha(showState, afterState, showView, currentView);
296 | } else {
297 | currentView.setVisibility(GONE);
298 | if (showView.getAlpha() == 0) {
299 | showView.setAlpha(1f);
300 | }
301 | showView.setVisibility(VISIBLE);
302 | mCurrentState = showState;
303 | if (null != mListener) {
304 | mListener.onStateChanged(showState);
305 | }
306 | isAniming = false;
307 | }
308 | }
309 |
310 | /**
311 | * 参数依次为:显示的状态、显示之后的状态码、要显示的 View、当前的 View
312 | *
313 | * @param showState
314 | * @param afterState
315 | * @param showView
316 | * @param currentView
317 | */
318 | private void showAlpha(final int showState, final int afterState, final View showView,
319 | final View currentView) {
320 | ObjectAnimator currentAnim = ObjectAnimator.ofFloat(currentView, "alpha", 1, 0);
321 | currentAnim.setDuration(250L);
322 | final ObjectAnimator showAnim = ObjectAnimator.ofFloat(showView, "alpha", 0, 1);
323 | showAnim.setDuration(250L);
324 | showAnim.addListener(new AnimatorListenerAdapter() {
325 |
326 | @Override
327 | public void onAnimationEnd(Animator animation) {
328 | super.onAnimationEnd(animation);
329 | if (null != mListener) {
330 | mListener.onStateChanged(showState);
331 | }
332 | if (afterState != VIEW_TAG) {
333 | showViewAnim(afterState, VIEW_TAG);
334 | } else {
335 | isAniming = false;
336 | }
337 | }
338 | });
339 | currentAnim.addListener(new AnimatorListenerAdapter() {
340 |
341 | @Override
342 | public void onAnimationEnd(Animator animation) {
343 | super.onAnimationEnd(animation);
344 | currentView.setVisibility(GONE);
345 | showView.setVisibility(VISIBLE);
346 | showAnim.start();
347 | mCurrentState = showState;
348 | }
349 | });
350 | currentAnim.start();
351 | }
352 |
353 | public int getCurrentState() {
354 | return mCurrentState;
355 | }
356 |
357 | public View getStateView(int state) {
358 | if (state <= VIEW_TAG) {
359 | throw new RuntimeException("ViewState 不在目标范围");
360 | }
361 | return mViews.get(state);
362 | }
363 |
364 | public void addUserView(int state, int layId) {
365 | setUserDefView(state, null, layId);
366 | }
367 |
368 | public void addUserView(int state, View view) {
369 | setUserDefView(state, view, -1);
370 | }
371 |
372 | private void setUserDefView(int state, View view, int layId) {
373 | if (state <= 0) {
374 | throw new RuntimeException("自定义的 ViewState TAG 必须大于 0");
375 | }
376 | if (null == view && layId != -1) {
377 | view = LayoutInflater.from(mContext).inflate(layId, this, false);
378 | }
379 | addViewToHash(view, state);
380 | addViewInLayout(view, -1, view.getLayoutParams());
381 | }
382 |
383 | }
384 |
--------------------------------------------------------------------------------
/state_view/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | // 是否使用过渡动画
7 |
8 |
9 | // 加载动画 View
10 |
11 |
12 | // 数据异常,加载失败 View
13 |
14 |
15 | // 网络异常 View
16 |
17 |
18 | // 空白页面 View
19 |
20 |
21 | // 设置当前显示的 viewState
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/state_view/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | state_view
3 |
4 |
--------------------------------------------------------------------------------
/state_view/src/test/java/com/rzj/view/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.rzj.view;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 |
18 | }
--------------------------------------------------------------------------------
/stateview/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/stateview/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion project.ext.sdk
5 |
6 | defaultConfig {
7 | minSdkVersion 15
8 | targetSdkVersion project.ext.sdk
9 | versionCode 1
10 | versionName "1.0"
11 |
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 |
14 | }
15 |
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 |
23 | lintOptions {
24 | abortOnError false
25 | }
26 |
27 |
28 | }
29 |
30 | dependencies {
31 | implementation fileTree(dir: 'libs', include: ['*.jar'])
32 | api 'com.android.support:appcompat-v7:28.0.0'
33 | testImplementation 'junit:junit:4.12'
34 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
35 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
36 | }
37 |
--------------------------------------------------------------------------------
/stateview/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/stateview/src/androidTest/java/com/rzj/stateview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.rzj.stateview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.rzj.stateview.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/stateview/src/androidTest/java/com/rzj/view/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.rzj.view;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.rzj.stateview.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/stateview/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/stateview/src/main/java/com/rzj/view/EasyStateView.java:
--------------------------------------------------------------------------------
1 | package com.rzj.view;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorListenerAdapter;
5 | import android.animation.ObjectAnimator;
6 | import android.content.Context;
7 | import android.content.res.TypedArray;
8 | import android.support.annotation.IntDef;
9 | import android.support.v4.util.ArrayMap;
10 | import android.util.AttributeSet;
11 | import android.util.Log;
12 | import android.view.LayoutInflater;
13 | import android.view.View;
14 | import android.widget.FrameLayout;
15 | import java.lang.annotation.Retention;
16 | import java.lang.annotation.RetentionPolicy;
17 |
18 | public class EasyStateView extends FrameLayout {
19 |
20 | // 内容 View
21 | public static final int VIEW_CONTENT = 0;
22 | // 加载 View
23 | public static final int VIEW_LOADING = -1;
24 | // 数据异常( 数据异常指原本应该是有数据,但是服务器返回了错误的、不符合格式的数据 ) View
25 | public static final int VIEW_ERROR_DATA = -2;
26 | // 网络异常 View
27 | public static final int VIEW_ERROR_NET = -3;
28 | // 数据为空 View
29 | public static final int VIEW_EMPTY = -4;
30 | // 用来存放自定义状态 View
31 | private ArrayMap mViews;
32 | private View mEmptyView;
33 | private View mErrorDataView;
34 | private View mErrorNetView;
35 | private View mLoadingView;
36 | private View mCurrentView;
37 | private View mContentView;
38 | // 是否使用默认的过渡动画
39 | private boolean isAnimation;
40 | // 当前显示的 ViewType
41 | private int mCurrentType;
42 | private Context mContext;
43 |
44 | @Retention(RetentionPolicy.SOURCE)
45 | @IntDef({VIEW_CONTENT, VIEW_LOADING, VIEW_ERROR_DATA, VIEW_ERROR_NET, VIEW_EMPTY})
46 | public @interface ViewState {
47 | }
48 |
49 | public EasyStateView(Context context) {
50 | this(context, null);
51 | }
52 |
53 | public EasyStateView(Context context, AttributeSet attrs) {
54 | this(context, attrs, 0);
55 | }
56 |
57 | public EasyStateView(Context context, AttributeSet attrs, int defStyleAttr) {
58 | super(context, attrs, defStyleAttr);
59 | init(context, attrs);
60 | }
61 |
62 | private void init(Context context, AttributeSet attrs) {
63 | mContext = context;
64 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EasyStateView);
65 | mCurrentType = typedArray.getInt(R.styleable.EasyStateView_esv_viewState, VIEW_CONTENT);
66 | Log.e("init", getChildCount() +" "+ mCurrentType);
67 | if(getChildCount() > 0){
68 | mContentView = getChildAt(0);
69 | if(mCurrentType != VIEW_CONTENT){
70 | mContentView.setVisibility(GONE);
71 | }else {
72 | mCurrentView = mContentView;
73 | }
74 | }
75 | int emptyResId = typedArray.getResourceId(R.styleable.EasyStateView_esv_emptyView, -1);
76 | if (emptyResId != -1) {
77 | mEmptyView = LayoutInflater.from(getContext()).inflate(emptyResId, this, false);
78 | if (mCurrentType != VIEW_EMPTY) {
79 | mEmptyView.setVisibility(GONE);
80 | } else {
81 | mCurrentView = mEmptyView;
82 | }
83 | addView(mEmptyView,mEmptyView.getLayoutParams());
84 | }
85 | int errorDataResId = typedArray.getResourceId(R.styleable.EasyStateView_esv_errorDataView, -1);
86 | if (errorDataResId != -1) {
87 | mErrorDataView = LayoutInflater.from(getContext()).inflate(errorDataResId, this, false);
88 | if (mCurrentType != VIEW_ERROR_DATA) {
89 | mErrorDataView.setVisibility(GONE);
90 | } else {
91 | mCurrentView = mErrorDataView;
92 | }
93 | addView(mErrorDataView, mErrorDataView.getLayoutParams());
94 | }
95 | int errorNetResId = typedArray.getResourceId(R.styleable.EasyStateView_esv_errorNetView, -1);
96 | if (errorNetResId != -1) {
97 | mErrorNetView = LayoutInflater.from(getContext()).inflate(errorNetResId, this, false);
98 | if (mCurrentType != VIEW_ERROR_NET) {
99 | mErrorNetView.setVisibility(GONE);
100 | } else {
101 | mCurrentView = mErrorNetView;
102 | }
103 | addView(mErrorNetView, mErrorNetView.getLayoutParams());
104 | }
105 | int loadingResId = typedArray.getResourceId(R.styleable.EasyStateView_esv_loadingView, -1);
106 | if (loadingResId != -1) {
107 | mLoadingView = LayoutInflater.from(getContext()).inflate(loadingResId, this, false);
108 | if (mCurrentType != VIEW_LOADING) {
109 | mLoadingView.setVisibility(GONE);
110 | } else {
111 | mCurrentView = mLoadingView;
112 | }
113 | addView(mLoadingView, mLoadingView.getLayoutParams());
114 | }
115 | isAnimation = typedArray.getBoolean(R.styleable.EasyStateView_esv_set_animation, true);
116 | typedArray.recycle();
117 | }
118 |
119 | /**
120 | * 切换默认状态的 View
121 | * @param state
122 | */
123 | public void setViewState(@ViewState int state) {
124 | switch (state) {
125 | case VIEW_CONTENT:
126 | showViewAnimator(mContentView);
127 | break;
128 | case VIEW_EMPTY:
129 | showViewAnimator(mEmptyView);
130 | break;
131 | case VIEW_ERROR_DATA:
132 | showViewAnimator(mErrorDataView);
133 | break;
134 | case VIEW_ERROR_NET:
135 | showViewAnimator(mErrorNetView);
136 | break;
137 | case VIEW_LOADING:
138 | showViewAnimator(mLoadingView);
139 | break;
140 | }
141 | }
142 |
143 | public void showViewAnimator(final View showView) {
144 | if(null == showView || null == mCurrentView
145 | || showView == mContentView){
146 | return;
147 | }
148 | ObjectAnimator currentAnim = ObjectAnimator.ofFloat(mCurrentView, "alpha", 1, 0);
149 | currentAnim.setDuration(300);
150 | currentAnim.start();
151 | currentAnim.addListener(new AnimatorListenerAdapter() {
152 | @Override
153 | public void onAnimationEnd(Animator animation) {
154 | super.onAnimationEnd(animation);
155 | ObjectAnimator showAnim = ObjectAnimator.ofFloat(showView, "alpha", 0, 1);
156 | showAnim.setDuration(300);
157 | animation.start();
158 | mCurrentView = showView;
159 | }
160 | });
161 | }
162 |
163 | public void setUserDefViewState(int state){
164 | if (state <= 0) {
165 | throw new RuntimeException("自定义的 ViewState TAG 必须大于 0");
166 | }
167 | if(null != mViews){
168 | showViewAnimator(mViews.get(state));
169 | }
170 | }
171 |
172 | public View getUserDefView(int state){
173 | if (state <= 0) {
174 | throw new RuntimeException("自定义的 ViewState TAG 必须大于 0");
175 | }
176 | if(null != mViews && mViews.size() > 0){
177 | return mViews.get(state);
178 | }
179 | return null;
180 | }
181 |
182 | public void setUserDefView(int state, int layId) {
183 | setUserDefView(state,null, layId);
184 | }
185 | public void setUserDefView(int state, View view) {
186 | setUserDefView(state,view, -1);
187 | }
188 |
189 | private void setUserDefView(int state, View view, int layId) {
190 | if (state <= 0) {
191 | throw new RuntimeException("自定义的 ViewState TAG 必须大于 0");
192 | }
193 | if(null == view && layId != -1){
194 | view = LayoutInflater.from(mContext).inflate(layId, this, false);
195 | }
196 | if(null == mViews){
197 | mViews = new ArrayMap();
198 | }
199 | mViews.put(state, view);
200 | }
201 |
202 | }
203 |
--------------------------------------------------------------------------------
/stateview/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | // 是否使用默认动画过渡
7 |
8 |
9 | // 加载动画 View
10 |
11 |
12 | // 数据异常,加载失败 View
13 |
14 |
15 | // 网络异常 View
16 |
17 |
18 | // 空白页面 View
19 |
20 |
21 | // 设置当前显示的 viewState
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/stateview/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | StateView
3 |
4 |
--------------------------------------------------------------------------------
/stateview/src/test/java/com/rzj/stateview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.rzj.stateview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/stateview/src/test/java/com/rzj/view/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.rzj.view;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------