├── .gitattributes
├── .gitignore
├── .idea
├── caches
│ └── build_file_checksums.ser
├── codeStyles
│ └── Project.xml
├── gradle.xml
├── misc.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── hankkin
│ │ └── pagelayoutdemo
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── hankkin
│ │ │ └── pagelayoutdemo
│ │ │ ├── DefaultActivity.kt
│ │ │ ├── DemoActivity.kt
│ │ │ ├── FragmentActivity.kt
│ │ │ ├── MainActivity.kt
│ │ │ └── TestFragment.kt
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable-xhdpi
│ │ └── pic.png
│ │ ├── drawable
│ │ ├── bg_theme_circle.xml
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── activity_default.xml
│ │ ├── activity_demo.xml
│ │ ├── activity_fragment.xml
│ │ ├── activity_main.xml
│ │ ├── fragment_test.xml
│ │ ├── layout_custom.xml
│ │ ├── layout_empty_demo.xml
│ │ ├── layout_error_demo.xml
│ │ └── layout_loading_demo.xml
│ │ ├── menu
│ │ └── menus.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
│ │ └── icon_smile.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
│ └── hankkin
│ └── pagelayoutdemo
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── pagelayout-java
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── hankkin
│ │ └── pagelayout_java
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── hankkin
│ │ │ └── pagelayout_java
│ │ │ ├── BlinkLayout.java
│ │ │ └── PageLayout.java
│ └── res
│ │ ├── drawable
│ │ ├── pic_empty.png
│ │ └── pic_error.png
│ │ ├── layout
│ │ ├── layout_empty.xml
│ │ ├── layout_error.xml
│ │ └── layout_loading.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── hankkin
│ └── pagelayout_java
│ └── ExampleUnitTest.java
├── pagelayout
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── hankkin
│ │ └── pagelayout
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── hankkin
│ │ │ └── pagelayout
│ │ │ ├── BlinkLayout.java
│ │ │ └── PageLayout.kt
│ └── res
│ │ ├── drawable
│ │ ├── pic_empty.png
│ │ └── pic_error.png
│ │ ├── layout
│ │ ├── layout_empty.xml
│ │ ├── layout_error.xml
│ │ └── layout_loading.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── hankkin
│ └── pagelayout
│ └── ExampleUnitTest.java
└── settings.gradle
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.java linguist-language=kotlin
2 | *.yml linguist-language=kotlin
3 | *.html linguist-language=kotlin
4 | *.js linguist-language=kotlin
5 | *.xml linguist-language=kotlin
6 | *.css linguist-language=kotlin
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/.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/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
19 |
20 |
--------------------------------------------------------------------------------
/.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 | # PageLayoutDemo
2 | 一款简单的page切换 空布局、错误布局、加载布局,支持一键配置、定义,不需要xml编写
3 |
4 | > **该功能是支持单独为某个布局设置状态改变的,比如很多同学提到的我一个listview的数据没有获取到,fun initPage(targetView: Any),这个targetView你只需要设置成你的listview或者包裹你listview的parent布局就OK了,具体原理可以看下面的代码解析啊,遍历获取索引,然后记录索引值....**
5 |
6 | **如果您想看JAVA版本,点击[https://github.com/Hankkin/PageLayoutDemojava](https://github.com/Hankkin/PageLayoutDemojava)**
7 |
8 | > 项目中我们经常会用到的加载数据,加载完数据后显示内容,如果没有数据显示一个空白页,这是如果网络错误了显示一个网络错误页,自定义一个PageLayout。
9 |
10 | ## DownLoad
11 |
12 | [https://fir.im/pagelayout](https://fir.im/pagelayout)
13 |
14 |
15 |
16 | ## 绪论
17 | Android中经常使用一个空白页和网络错误页用来提高用户体验,给用户一个较好的感官,如果获取到的数据为空,那么会显示一个空白数据页,如果在获取数据的过程中网络错误了,会显示一个网络异常页,像最近比较火的某东这样,见下图。网上也有一些开源的组件,大部分都是自定义继承某个布局在xml中让其作为跟布局,然后将自己的内容布局添加进去,效果也都不错,但是个人总觉得稍微有些麻烦,不是那么灵活,n多个xml布局都去定义,写的心烦,所以有了今天的主角。
18 |
19 |
20 |
21 |
22 | ## 思考
23 | 实现的思路实际上是和上面说的一样,只不过换了一种方式,我们手动获取到contentView,将它从DecorView中移除,然后交给PageLayout取管理。当时考虑的时候就是不想在每个xml中去写页面切换的布局,那么我们可不可以用Java代码去控制?带着下面几个问题一起来看一下。
24 |
25 | - 1.自定义一个布局让其作为跟布局
26 | - 2.提供切换**加载loading**、**空白页empty**、**错误页errror**、**内容页content**功能
27 | - 3.怎么让其取管理上边的四个页面?
28 | - 4.contentView怎么添加?
29 | - 5.如果我想切换的跟布局不是个Activity或者Fragment怎么办?
30 | - 6.因为切换页面状态的功能一般都是一个APP统一的,那么可不可以一键配置呢?
31 |
32 | ## 实现
33 |
34 | ### 1.代码设计
35 |
36 | 首先我们定义PageLayout继承FrameLayout或者LinearLayou或者其他的布局都可以,然后我们需要提供切换四个布局的功能,当然如果支持自定义就更好了,还有状态布局里面的一些属性,还方便一键配置,所以最后采用了Builder模式来创建,使用方式就和Android里面的**AlertDialog**一样,通过Builder去构建一个PageLayout。最后的样子是长这样的:
37 |
38 | | 方法 | 注释 |
39 | | :------------------------- | ----------------------------- |
40 | | showLoading() | 显示loading |
41 | | showError() | 显示错误布局 |
42 | | showEmpty() | 显示空布局 |
43 | | hide() | 显示内容布局 |
44 | | **Builder** | |
45 | | setLoading() | setLoadingText() |
46 | | setError() | setDefaultLoadingBlinkText() |
47 | | setEmpty() | setLoadingTextColor() |
48 | | setDefaultEmptyText() | setDefaultLoadingBlinkColor() |
49 | | setDefaultEmptyTextColor() | setDefaultErrorText() |
50 | | setDefaultErrorTextColor() | setEmptyDrawable() |
51 | | setErrorDrawable() | |
52 |
53 |
54 |
55 | **默认样式**
56 | ```
57 | PageLayout.Builder(this)
58 | .initPage(ll_default)
59 | .setOnRetryListener(object : PageLayout.OnRetryClickListener{
60 | override fun onRetry() {
61 | loadData()
62 | }
63 |
64 | })
65 | .create()
66 | ```
67 |
68 | **自定义样式**
69 |
70 | ```
71 | PageLayout.Builder(this)
72 | .initPage(ll_demo)
73 | .setLoading(R.layout.layout_loading_demo)
74 | .setEmpty(R.layout.layout_empty_demo,R.id.tv_page_empty_demo)
75 | .setError(R.layout.layout_error_demo,R.id.tv_page_error_demo,object : PageLayout.OnRetryClickListener{
76 | override fun onRetry() {
77 | loadData()
78 | }
79 | })
80 | .setEmptyDrawable(R.drawable.pic_empty)
81 | .setErrorDrawable(R.drawable.pic_error)
82 | .create()
83 | ```
84 |
85 | ### 2.设置PageLayout
86 |
87 | > 考虑好了代码设计方式之后,我们来具体实现功能,首先需要考虑上面的5,6点:
88 |
89 | **contentView怎么添加?**
90 |
91 | **如果我想切换的跟布局不是个Activity或者Fragment怎么办?**
92 |
93 | #### 1.Activity
94 | 如果我们要切换的跟布局是个Activity时,首先我们需要了解一下Android中的setContentView()方法,很熟悉,是我们新建完Activity后默认会在生命周期方法onCreate()中默认存在的,那么setContentView()做了些什么呢?我们先看一张图:
95 |
96 | 
97 |
98 | > 一个Activity是通过ActivityThread创建出来的,创建完毕后,会将DecorView添加到Window中,同时会创建ViewRootImpl对象,并将ViewRootImpl对象和DecorView建立关联,setContentView()是通过getWindow()调用的,这里的window实际初始化的时候初始化为PhoneWindow,也就是说Activity会调用PhoneWindow的setContentView()将layout布局添加到DecorView上,而此时的DecorView就是那个最底层的View。然后通过LayoutInflater.infalte()方法加载布局生成View对象并通过addView()方法添加到Window上,(一层一层的叠加到Window上)所以,Activity其实不是显示视图,Window才是真正的显示视图。
99 |
100 | 再来看上面的那张图,可以说DecorView是一个界面的真正跟布局,TitleView我们可以通过设置theme样式显示隐藏的,状态布局切换时我们不考虑TitleView,我们只需要考虑ContentView,而ContentView也就是**android.R.id.content**,知道了这些我们来看看怎么获取将contenView交给PageLayout管理。
101 |
102 | #### 2.Fragment、View
103 |
104 | 如果我们要切换的跟布局是个Fragment、View时,我们只需要获取到它的parent
105 |
106 | ### 3.PageLayout设置跟布局
107 | 获取到了contentView跟布局后,我们要移除自己的显示内容的布局,并把这个布局交给PageLayout,下面看一下代码,注释的很详细了
108 |
109 | ```
110 | /**
111 | * set target view for root
112 | */
113 | fun initPage(targetView: Any): Builder {
114 | var content: ViewGroup? = null
115 | when (targetView) {
116 | is Activity -> { //如果是Activity,获取到android.R.content
117 | mContext = targetView
118 | content = (mContext as Activity).findViewById(android.R.id.content)
119 | }
120 | is Fragment -> { //如果是Fragment获取到parent
121 | mContext = targetView.activity!!
122 | content = (targetView.view)?.parent as ViewGroup
123 | }
124 | is View -> { //如果是View,也取到parent
125 | mContext = targetView.context
126 | try {
127 | content = (targetView.parent) as ViewGroup
128 | } catch (e: TypeCastException) {
129 | }
130 | }
131 | }
132 | val childCount = content?.childCount
133 | var index = 0
134 | val oldContent: View
135 | if (targetView is View) { //如果是某个线性布局或者相对布局时,遍历它的孩子,找到对应的索引,记录下来
136 | oldContent = targetView
137 | childCount?.let {
138 | for (i in 0 until childCount) {
139 | if (content!!.getChildAt(i) === oldContent) {
140 | index = i
141 | break
142 | }
143 | }
144 | }
145 |
146 | } else { //如果是Activity或者Fragment时,取到索引为第一个的View
147 | oldContent = content!!.getChildAt(0)
148 | }
149 | mPageLayout.mContent = oldContent //给PageLayout设置contentView
150 | mPageLayout.removeAllViews()
151 | content?.removeView(oldContent) //将本身content移除,并且把PageLayout添加到DecorView中去
152 | val lp = oldContent.layoutParams
153 | content?.addView(mPageLayout, index, lp)
154 | mPageLayout.addView(oldContent)
155 | initDefault() //设置默认状态布局
156 | return this
157 | }
158 | ```
159 |
160 | 这样我们就解决了上面的5,6的问题。
161 |
162 | ### 4.其他
163 |
164 | - **因为错误布局中一般都包括一个点击重试的功能,如果你需要自定义布局,你可以在配置PageLayout之前,设置好错误布局和点击事件,然后setError进去,同时也提供了一个默认方式的方法**
165 |
166 | ```
167 | fun setError(errorView: Int, errorClickId: Int, onRetryClickListener: OnRetryClickListener)
168 | ```
169 | - **考虑到此功能的APP统一性,所以并没有提供过多的自定义功能,如果你需要的话,你都可以提前设置好View,然后进行set**
170 | - **之前和同事讨论,xml形式和代码形式哪个更方便更灵活,这些都属于个人喜好吧,如果你更喜欢在xml里写的话,你可以进行改造,也挺简单,目前没提供xml方式,PageLayout的初衷就是模仿AlertDialog方式,随时随地使用状态布局切换**
171 | - **你也可以在BaseActivity和BaseFragment中进行PageLayout的初始化,Demo中未使用,自行解决**
172 |
173 | ## 效果图
174 |
175 | 
176 |
177 |
178 | 代码已经上传到Github[https://github.com/Hankkin/PageLayoutDemo](https://github.com/Hankkin/PageLayoutDemo)
179 |
180 |
181 | **Reading:一款不错的Material Desgin风格的Kotlin版本的开源APP**
182 | [https://github.com/Hankkin/Reading](https://github.com/Hankkin/Reading)
183 |
184 | 欢迎大家Follow、star、fork,谢谢
185 | 如果有不合适的地方,请提issues讨论指正
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 |
5 | android {
6 | compileSdkVersion 27
7 | defaultConfig {
8 | applicationId "com.hankkin.pagelayoutdemo"
9 | minSdkVersion 15
10 | targetSdkVersion 27
11 | versionCode 1
12 | versionName "1.0"
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | implementation fileTree(dir: 'libs', include: ['*.jar'])
25 | implementation 'com.android.support:appcompat-v7:27.1.1'
26 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
27 | implementation 'com.android.support:support-v4:27.1.1'
28 | testImplementation 'junit:junit:4.12'
29 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
30 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
31 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
32 | implementation project(path: ':pagelayout')
33 | }
34 | repositories {
35 | mavenCentral()
36 | }
37 |
--------------------------------------------------------------------------------
/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/hankkin/pagelayoutdemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayoutdemo;
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.hankkin.pagelayoutdemo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hankkin/pagelayoutdemo/DefaultActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayoutdemo
2 |
3 | import android.os.Bundle
4 | import android.os.Handler
5 | import android.support.v7.app.AppCompatActivity
6 | import android.view.Menu
7 | import android.view.MenuItem
8 | import android.widget.ImageView
9 | import com.hankkin.pagelayout.PageLayout
10 | import kotlinx.android.synthetic.main.activity_default.*
11 |
12 | class DefaultActivity : AppCompatActivity() {
13 |
14 | private val mPageLayout by lazy {
15 | PageLayout.Builder(this)
16 | .initPage(ll_default)
17 | .setCustomView(layoutInflater.inflate(R.layout.layout_custom, null)
18 | .apply {
19 | //todo
20 | findViewById(R.id.iv_custom).setImageResource(R.mipmap.icon_smile)
21 | })
22 | .setOnRetryListener(object : PageLayout.OnRetryClickListener {
23 | override fun onRetry() {
24 | loadData()
25 | }
26 | })
27 | .create()
28 | }
29 |
30 | override fun onCreate(savedInstanceState: Bundle?) {
31 | super.onCreate(savedInstanceState)
32 | setContentView(R.layout.activity_default)
33 |
34 | loadData()
35 |
36 | }
37 |
38 |
39 | private fun loadData() {
40 | mPageLayout.showLoading()
41 | Handler().postDelayed({
42 | mPageLayout.hide()
43 | }, 3000)
44 | }
45 |
46 |
47 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
48 | menuInflater.inflate(R.menu.menus, menu)
49 | return true
50 | }
51 |
52 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
53 | when (item.itemId) {
54 | R.id.menu_loading -> loadData()
55 | R.id.menu_empty -> mPageLayout.showEmpty()
56 | R.id.menu_error -> mPageLayout.showError()
57 | R.id.menu_content -> mPageLayout.hide()
58 | R.id.menu_customer -> mPageLayout.showCustom()
59 | else -> {
60 | }
61 | }
62 | return super.onOptionsItemSelected(item)
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hankkin/pagelayoutdemo/DemoActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayoutdemo
2 |
3 | import android.os.Bundle
4 | import android.os.Handler
5 | import android.support.v7.app.AppCompatActivity
6 | import android.view.Menu
7 | import android.view.MenuItem
8 | import android.widget.ImageView
9 | import android.widget.TextView
10 | import com.hankkin.pagelayout.PageLayout
11 | import kotlinx.android.synthetic.main.activity_demo.*
12 |
13 | class DemoActivity : AppCompatActivity() {
14 |
15 | private val mPageLayout by lazy {
16 | PageLayout.Builder(this)
17 | .initPage(ll_demo)
18 | .setLoading(R.layout.layout_loading_demo,R.id.tv_page_loading_demo)
19 | .setEmpty(R.layout.layout_empty_demo,R.id.tv_page_empty_demo)
20 | .setCustomView(layoutInflater.inflate(R.layout.layout_custom, null)
21 | .apply {
22 | //todo
23 | findViewById(R.id.iv_custom).setImageResource(R.mipmap.icon_smile)
24 | findViewById(R.id.tv_custom_content).setText("This is PageLayout")
25 | })
26 | .setError(R.layout.layout_error_demo,R.id.tv_page_error_demo,object : PageLayout.OnRetryClickListener{
27 | override fun onRetry() {
28 | loadData()
29 | }
30 | })
31 | .setEmptyDrawable(R.drawable.pic_empty)
32 | .setErrorDrawable(R.drawable.pic_error)
33 | .setLoadingText("Loading")
34 | .create()
35 |
36 | }
37 |
38 | override fun onCreate(savedInstanceState: Bundle?) {
39 | super.onCreate(savedInstanceState)
40 | setContentView(R.layout.activity_demo)
41 |
42 | loadData()
43 |
44 | }
45 |
46 | private fun loadData(){
47 | mPageLayout.showLoading()
48 | Handler().postDelayed({
49 | mPageLayout.hide()
50 | },3000)
51 | }
52 |
53 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
54 | menuInflater.inflate(R.menu.menus, menu)
55 | return true
56 | }
57 |
58 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
59 | when (item.itemId) {
60 | R.id.menu_loading -> loadData()
61 | R.id.menu_empty -> mPageLayout.showEmpty()
62 | R.id.menu_error -> mPageLayout.showError()
63 | R.id.menu_content -> mPageLayout.hide()
64 | R.id.menu_customer -> mPageLayout.showCustom()
65 | else -> {
66 | }
67 | }
68 | return super.onOptionsItemSelected(item)
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hankkin/pagelayoutdemo/FragmentActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayoutdemo
2 |
3 | import android.os.Bundle
4 | import android.support.v4.app.FragmentActivity
5 |
6 | class FragmentActivity : FragmentActivity() {
7 |
8 | override fun onCreate(savedInstanceState: Bundle?) {
9 | super.onCreate(savedInstanceState)
10 | setContentView(R.layout.activity_fragment)
11 |
12 |
13 | val fragment = TestFragment()
14 | val fragmentManager = supportFragmentManager
15 | val fragmentTransaction = fragmentManager.beginTransaction()
16 | fragmentTransaction.replace(R.id.container, fragment)
17 | fragmentTransaction.commit()
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hankkin/pagelayoutdemo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayoutdemo
2 |
3 | import android.content.Intent
4 | import android.os.Bundle
5 | import android.support.v7.app.AppCompatActivity
6 | import kotlinx.android.synthetic.main.activity_main.*
7 |
8 | class MainActivity : AppCompatActivity() {
9 |
10 | override fun onCreate(savedInstanceState: Bundle?) {
11 | super.onCreate(savedInstanceState)
12 | setContentView(R.layout.activity_main)
13 |
14 | btn_default.setOnClickListener {
15 | startActivity(Intent(this,DefaultActivity::class.java))
16 | }
17 |
18 | btn_demo.setOnClickListener {
19 | startActivity(Intent(this,DemoActivity::class.java))
20 | }
21 |
22 | btn_fg.setOnClickListener {
23 | startActivity(Intent(this,FragmentActivity::class.java))
24 | }
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hankkin/pagelayoutdemo/TestFragment.kt:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayoutdemo
2 |
3 | import android.content.Context
4 | import android.net.Uri
5 | import android.os.Bundle
6 | import android.support.v4.app.Fragment
7 | import android.view.LayoutInflater
8 | import android.view.View
9 | import android.view.ViewGroup
10 | import com.hankkin.pagelayout.PageLayout
11 |
12 |
13 | // TODO: Rename parameter arguments, choose names that match
14 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
15 | private const val ARG_PARAM1 = "param1"
16 | private const val ARG_PARAM2 = "param2"
17 |
18 | /**
19 | * A simple [Fragment] subclass.
20 | * Activities that contain this fragment must implement the
21 | * [TestFragment.OnFragmentInteractionListener] interface
22 | * to handle interaction events.
23 | * Use the [TestFragment.newInstance] factory method to
24 | * create an instance of this fragment.
25 | *
26 | */
27 | class TestFragment : Fragment() {
28 | // TODO: Rename and change types of parameters
29 | private var param1: String? = null
30 | private var param2: String? = null
31 |
32 | override fun onCreate(savedInstanceState: Bundle?) {
33 | super.onCreate(savedInstanceState)
34 | arguments?.let {
35 | param1 = it.getString(ARG_PARAM1)
36 | param2 = it.getString(ARG_PARAM2)
37 | }
38 | }
39 |
40 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
41 | savedInstanceState: Bundle?): View? {
42 | // Inflate the layout for this fragment
43 | return inflater.inflate(R.layout.fragment_test, container, false)
44 | }
45 |
46 | override fun onActivityCreated(savedInstanceState: Bundle?) {
47 | super.onActivityCreated(savedInstanceState)
48 | val pageLayout = context?.let {
49 | PageLayout.Builder(it)
50 | //.initPage(getActivity().findViewById(R.id.fl_test))
51 | .initPage(this)
52 | .create()
53 | }
54 | pageLayout?.showLoading()
55 | }
56 |
57 |
58 | override fun onAttach(context: Context) {
59 | super.onAttach(context)
60 | }
61 |
62 | override fun onDetach() {
63 | super.onDetach()
64 | }
65 |
66 | /**
67 | * This interface must be implemented by activities that contain this
68 | * fragment to allow an interaction in this fragment to be communicated
69 | * to the activity and potentially other fragments contained in that
70 | * activity.
71 | *
72 | *
73 | * See the Android Training lesson [Communicating with Other Fragments]
74 | * (http://developer.android.com/training/basics/fragments/communicating.html)
75 | * for more information.
76 | */
77 | interface OnFragmentInteractionListener {
78 | // TODO: Update argument type and name
79 | fun onFragmentInteraction(uri: Uri)
80 | }
81 |
82 | companion object {
83 | /**
84 | * Use this factory method to create a new instance of
85 | * this fragment using the provided parameters.
86 | *
87 | * @param param1 Parameter 1.
88 | * @param param2 Parameter 2.
89 | * @return A new instance of fragment TestFragment.
90 | */
91 | // TODO: Rename and change types and number of parameters
92 | @JvmStatic
93 | fun newInstance(param1: String, param2: String) =
94 | TestFragment().apply {
95 | arguments = Bundle().apply {
96 | putString(ARG_PARAM1, param1)
97 | putString(ARG_PARAM2, param2)
98 | }
99 | }
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/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-xhdpi/pic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/drawable-xhdpi/pic.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/bg_theme_circle.xml:
--------------------------------------------------------------------------------
1 |
2 |
18 |
19 |
20 | -
21 |
22 |
23 |
24 |
25 |
26 |
27 | -
28 |
29 |
30 |
31 |
32 |
33 |
34 | -
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/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/layout/activity_default.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
22 |
27 |
32 |
33 |
38 |
39 |
44 |
45 |
50 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
18 |
23 |
28 |
33 |
34 |
39 |
40 |
45 |
46 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
20 |
21 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_custom.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
16 |
17 |
22 |
23 |
28 |
29 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_empty_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_error_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_loading_demo.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
14 |
15 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menus.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/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/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/icon_smile.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-xhdpi/icon_smile.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ff662c
4 | #ff662c
5 | #ff662c
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PageLayoutDemo
3 |
4 |
5 | Hello blank fragment
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/hankkin/pagelayoutdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayoutdemo;
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 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.2.61'
5 |
6 | repositories {
7 | google()
8 | jcenter()
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.0.1'
12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/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=-Xmx1536m
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 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Sep 06 22:48:40 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/pagelayout-java/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/pagelayout-java/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 26
5 |
6 |
7 |
8 | defaultConfig {
9 | minSdkVersion 15
10 | targetSdkVersion 26
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 |
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation fileTree(dir: 'libs', include: ['*.jar'])
29 |
30 | implementation 'com.android.support:appcompat-v7:26.1.0'
31 | testImplementation 'junit:junit:4.12'
32 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
33 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
34 | }
35 |
--------------------------------------------------------------------------------
/pagelayout-java/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 |
--------------------------------------------------------------------------------
/pagelayout-java/src/androidTest/java/com/hankkin/pagelayout_java/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayout_java;
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() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.hankkin.pagelayout_java.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/pagelayout-java/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/pagelayout-java/src/main/java/com/hankkin/pagelayout_java/BlinkLayout.java:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayout_java;
2 |
3 | import android.animation.Animator;
4 | import android.animation.ObjectAnimator;
5 | import android.animation.ValueAnimator;
6 | import android.content.Context;
7 | import android.content.res.TypedArray;
8 | import android.graphics.Bitmap;
9 | import android.graphics.Canvas;
10 | import android.graphics.Color;
11 | import android.graphics.LinearGradient;
12 | import android.graphics.Paint;
13 | import android.graphics.PorterDuff;
14 | import android.graphics.PorterDuffXfermode;
15 | import android.graphics.Shader;
16 | import android.os.Build;
17 | import android.support.annotation.ColorInt;
18 | import android.support.annotation.RequiresApi;
19 | import android.support.v4.content.ContextCompat;
20 | import android.util.AttributeSet;
21 | import android.view.ViewTreeObserver;
22 | import android.widget.LinearLayout;
23 |
24 | public class BlinkLayout extends LinearLayout {
25 |
26 | private Paint maskPaint;
27 |
28 | private ValueAnimator maskAnimator;
29 |
30 | private Bitmap localAvailableBitmap;
31 | private Bitmap localMaskBitmap;
32 |
33 | private Bitmap destinationBitmap;
34 | private Bitmap sourceMaskBitmap;
35 | private Canvas canvasForRendering;
36 |
37 | private int maskOffsetX;
38 |
39 | private boolean isAnimationStarted;
40 |
41 | private int shimmerAnimationDuration;
42 |
43 | @ColorInt
44 | private int shimmerColor;
45 |
46 | public BlinkLayout(Context context) {
47 | this(context, null);
48 | }
49 |
50 | public BlinkLayout(Context context, AttributeSet attrs) {
51 | this(context, attrs, 0);
52 | }
53 |
54 | public BlinkLayout(Context context, AttributeSet attrs, int defStyle) {
55 | super(context, attrs, defStyle);
56 |
57 | setWillNotDraw(false);
58 |
59 | maskPaint = new Paint();
60 | maskPaint.setAntiAlias(true);
61 | maskPaint.setDither(true);
62 | maskPaint.setFilterBitmap(true);
63 | maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
64 |
65 | TypedArray a = context.getTheme().obtainStyledAttributes(
66 | attrs,
67 | R.styleable.BlinkLayout,
68 | 0, 0);
69 |
70 | try {
71 | shimmerAnimationDuration = a.getInteger(R.styleable.BlinkLayout_blink_animation_duration, 1500);
72 | shimmerColor = a.getColor(R.styleable.BlinkLayout_blink_color, ContextCompat.getColor(context, R.color.shimmer_color));
73 | } finally {
74 | a.recycle();
75 | }
76 | }
77 |
78 | @Override
79 | protected void onDetachedFromWindow() {
80 | resetShimmering();
81 | super.onDetachedFromWindow();
82 | }
83 |
84 | @Override
85 | protected void dispatchDraw(Canvas canvas) {
86 | if (!isAnimationStarted || getWidth() <= 0 || getHeight() <= 0) {
87 | super.dispatchDraw(canvas);
88 | } else {
89 | dispatchDrawUsingBitmap(canvas);
90 | }
91 | }
92 |
93 | public void startShimmerAnimation() {
94 | if (isAnimationStarted) {
95 | return;
96 | }
97 |
98 | if (getWidth() == 0) {
99 | getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
100 | @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
101 | @Override
102 | public void onGlobalLayout() {
103 | BlinkLayout.this.getViewTreeObserver().removeOnGlobalLayoutListener(this);
104 | startShimmerAnimation();
105 | }
106 | });
107 |
108 | return;
109 | }
110 |
111 | Animator animator = getShimmerAnimation();
112 | animator.start();
113 | isAnimationStarted = true;
114 | }
115 |
116 | public void stopShimmerAnimation() {
117 | resetShimmering();
118 | }
119 |
120 | public void setShimmerColor(int shimmerColor) {
121 | this.shimmerColor = shimmerColor;
122 |
123 | if (isAnimationStarted) {
124 | resetShimmering();
125 | startShimmerAnimation();
126 | }
127 | }
128 |
129 | private void dispatchDrawUsingBitmap(Canvas canvas) {
130 | super.dispatchDraw(canvas);
131 |
132 | localAvailableBitmap = getDestinationBitmap();
133 | if (localAvailableBitmap == null) {
134 | return;
135 | }
136 |
137 | if (canvasForRendering == null) {
138 | canvasForRendering = new Canvas(localAvailableBitmap);
139 | }
140 |
141 | drawMask(canvasForRendering);
142 | canvas.save();
143 | canvas.clipRect(maskOffsetX, 0, maskOffsetX + getWidth() / 2, getHeight());
144 | canvas.drawBitmap(localAvailableBitmap, 0, 0, null);
145 | canvas.restore();
146 |
147 | localAvailableBitmap = null;
148 | }
149 |
150 | private void drawMask(Canvas renderCanvas) {
151 | localMaskBitmap = getSourceMaskBitmap();
152 | if (localMaskBitmap == null) {
153 | return;
154 | }
155 |
156 | renderCanvas.save();
157 | renderCanvas.clipRect(maskOffsetX, 0,
158 | maskOffsetX + localMaskBitmap.getWidth(),
159 | getHeight());
160 |
161 | super.dispatchDraw(renderCanvas);
162 | renderCanvas.drawBitmap(localMaskBitmap, maskOffsetX, 0, maskPaint);
163 |
164 | renderCanvas.restore();
165 |
166 | localMaskBitmap = null;
167 | }
168 |
169 | private void resetShimmering() {
170 | if (maskAnimator != null) {
171 | maskAnimator.end();
172 | maskAnimator.removeAllUpdateListeners();
173 | }
174 |
175 | maskAnimator = null;
176 | isAnimationStarted = false;
177 |
178 | releaseBitMaps();
179 | }
180 |
181 | private void releaseBitMaps() {
182 | if (sourceMaskBitmap != null) {
183 | sourceMaskBitmap.recycle();
184 | sourceMaskBitmap = null;
185 | }
186 |
187 | if (destinationBitmap != null) {
188 | destinationBitmap.recycle();
189 | destinationBitmap = null;
190 | }
191 |
192 | canvasForRendering = null;
193 | }
194 |
195 | private Bitmap getDestinationBitmap() {
196 | if (destinationBitmap == null) {
197 | destinationBitmap = createBitmap(getWidth(), getHeight());
198 | }
199 |
200 | return destinationBitmap;
201 | }
202 |
203 | private Bitmap getSourceMaskBitmap() {
204 | if (sourceMaskBitmap != null) {
205 | return sourceMaskBitmap;
206 | }
207 |
208 | int width = getWidth() / 2;
209 | int height = getHeight();
210 |
211 | sourceMaskBitmap = createBitmap(width, height);
212 | Canvas canvas = new Canvas(sourceMaskBitmap);
213 |
214 | LinearGradient gradient = new LinearGradient(
215 | 0, 0,
216 | width, 0,
217 | new int[]{Color.TRANSPARENT, shimmerColor, shimmerColor, Color.TRANSPARENT},
218 | new float[]{0.25F, 0.5F, 0.5F, 0.75F},
219 | Shader.TileMode.CLAMP);
220 |
221 | canvas.rotate(20, width / 2, height / 2);
222 |
223 | Paint paint = new Paint();
224 | paint.setShader(gradient);
225 | int padding = (int) (Math.sqrt(2) * Math.max(width, height)) / 2;
226 | canvas.drawRect(0, -padding, width, height + padding, paint);
227 |
228 | return sourceMaskBitmap;
229 | }
230 |
231 | private Animator getShimmerAnimation() {
232 | if (maskAnimator != null) {
233 | return maskAnimator;
234 | }
235 |
236 | final int animationToX = getWidth();
237 | final int animationFromX = -animationToX;
238 | final int shimmerBitmapWidth = getWidth() / 2;
239 | final int shimmerAnimationFullLength = animationToX - animationFromX;
240 |
241 | maskAnimator = ValueAnimator.ofFloat(0.0F, 1.0F);
242 | maskAnimator.setDuration(shimmerAnimationDuration);
243 | maskAnimator.setRepeatCount(ObjectAnimator.INFINITE);
244 |
245 | final float[] value = new float[1];
246 | maskAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
247 | @Override
248 | public void onAnimationUpdate(ValueAnimator animation) {
249 | value[0] = (Float) animation.getAnimatedValue();
250 | maskOffsetX = ((int) (animationFromX + shimmerAnimationFullLength * value[0]));
251 |
252 | if (maskOffsetX + shimmerBitmapWidth >= 0) {
253 | invalidate();
254 | }
255 | }
256 | });
257 |
258 | return maskAnimator;
259 | }
260 |
261 | private Bitmap createBitmap(int width, int height) {
262 | try {
263 | return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
264 | } catch (OutOfMemoryError e) {
265 | System.gc();
266 | return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
267 | }
268 | }
269 | }
--------------------------------------------------------------------------------
/pagelayout-java/src/main/java/com/hankkin/pagelayout_java/PageLayout.java:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayout_java;
2 |
3 | import android.content.Context;
4 | import android.os.Looper;
5 | import android.support.annotation.NonNull;
6 | import android.support.annotation.Nullable;
7 | import android.util.AttributeSet;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.widget.FrameLayout;
11 | import android.widget.TextView;
12 |
13 |
14 | /**
15 | * Created by ${Hankkin} on 2018/10/12.
16 | */
17 |
18 | public class PageLayout extends FrameLayout {
19 |
20 | enum State{
21 | EMPTY_TYPE,
22 | LOADING_TYPE,
23 | ERROR_TYPE,
24 | CONTENT_TYPE,
25 | CUSTOM_TYPE
26 | }
27 |
28 | private View mLoading;
29 | private View mEmpty;
30 | private View mError;
31 | private View mContent;
32 | private View mCustom;
33 | private Context mContext;
34 | private BlinkLayout mBlinkLayout;
35 | private State mCurrentState;
36 |
37 | public PageLayout(@NonNull Context context) {
38 | super(context);
39 | }
40 |
41 | public PageLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
42 | super(context, attrs);
43 | }
44 |
45 | public PageLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
46 | super(context, attrs, defStyleAttr);
47 | }
48 |
49 | private void showView(final State state){
50 | if (Looper.myLooper() == Looper.getMainLooper()){
51 | changeView(state);
52 | }
53 | else {
54 | post(new Runnable() {
55 | @Override
56 | public void run() {
57 | changeView(state);
58 | }
59 | });
60 | }
61 | }
62 |
63 | private void changeView(State state){
64 | mCurrentState = state;
65 | switch (state){
66 | case LOADING_TYPE:
67 | mLoading.setVisibility(VISIBLE);
68 | break;
69 | case EMPTY_TYPE:
70 | mEmpty.setVisibility(VISIBLE);
71 | break;
72 | case ERROR_TYPE:
73 | mError.setVisibility(VISIBLE);
74 | break;
75 | case CUSTOM_TYPE:
76 | mCustom.setVisibility(VISIBLE);
77 | break;
78 | case CONTENT_TYPE:
79 | mContent.setVisibility(VISIBLE);
80 | break;
81 | }
82 | }
83 |
84 | public void showLoading(){
85 | showView(State.LOADING_TYPE);
86 | if (mBlinkLayout != null){
87 | mBlinkLayout.startShimmerAnimation();
88 | }
89 | }
90 |
91 | public void showError(){
92 | showView(State.ERROR_TYPE);
93 | }
94 |
95 | public void showEmpty(){
96 | showView(State.EMPTY_TYPE);
97 | }
98 |
99 | public void hide(){
100 | showView(State.CONTENT_TYPE);
101 | if (mBlinkLayout!= null){
102 | mBlinkLayout.stopShimmerAnimation();
103 | }
104 | }
105 |
106 | public void showCustom(){
107 | showView(State.CUSTOM_TYPE);
108 | }
109 |
110 | class Builder{
111 | private PageLayout mPageLayout;
112 | private LayoutInflater mInflater;
113 | private Context mContext;
114 | private TextView mTvEmpty;
115 | private TextView mTvError;
116 | private TextView mTvErrorRetry;
117 | private TextView mTvLoading;
118 | private TextView mTvLoadingBlink;
119 | private BlinkLayout mBlinkLayout;
120 | private OnRetryClickListener mOnRetryClickListener;
121 |
122 | public Builder(Context context) {
123 | mContext = context;
124 | this.mPageLayout = new PageLayout(mContext);
125 | this.mInflater = LayoutInflater.from(mContext);
126 | }
127 |
128 | private void initDefault(){
129 | if (mPageLayout.mEmpty == null){
130 | setDefaultEmpty();
131 | }
132 |
133 | }
134 |
135 | private void setDefaultEmpty(){
136 | mPageLayout.mEmpty = mInflater.inflate(R.layout.layout_empty, mPageLayout, false);
137 | mTvEmpty = mPageLayout.mEmpty.findViewById(R.id.tv_page_empty);
138 | mPageLayout.mEmpty.setVisibility(GONE);
139 | mPageLayout.addView(mPageLayout.mEmpty);
140 | }
141 |
142 | private void setDefaultError()
143 | }
144 |
145 | interface OnRetryClickListener{
146 | void onRetry();
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/pagelayout-java/src/main/res/drawable/pic_empty.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/pagelayout-java/src/main/res/drawable/pic_empty.png
--------------------------------------------------------------------------------
/pagelayout-java/src/main/res/drawable/pic_error.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/pagelayout-java/src/main/res/drawable/pic_error.png
--------------------------------------------------------------------------------
/pagelayout-java/src/main/res/layout/layout_empty.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
15 |
16 |
--------------------------------------------------------------------------------
/pagelayout-java/src/main/res/layout/layout_error.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
28 |
29 |
--------------------------------------------------------------------------------
/pagelayout-java/src/main/res/layout/layout_loading.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
18 |
19 |
--------------------------------------------------------------------------------
/pagelayout-java/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ff662c
4 | #ff662c
5 | #ff662c
6 | #b1b1b1
7 | #a2878787
8 |
9 |
10 |
--------------------------------------------------------------------------------
/pagelayout-java/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | EmptyPage
3 | 网络开小差了
4 | 点击重试
5 | 暂无新数据
6 |
7 |
--------------------------------------------------------------------------------
/pagelayout-java/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/pagelayout-java/src/test/java/com/hankkin/pagelayout_java/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayout_java;
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() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/pagelayout/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/pagelayout/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'kotlin-android'
3 |
4 | android {
5 | compileSdkVersion 27
6 |
7 |
8 |
9 | defaultConfig {
10 | minSdkVersion 15
11 | targetSdkVersion 27
12 | versionCode 20180925
13 | versionName "1.1"
14 |
15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16 |
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 |
26 | }
27 |
28 | dependencies {
29 | implementation fileTree(dir: 'libs', include: ['*.jar'])
30 |
31 | implementation 'com.android.support:appcompat-v7:27.1.1'
32 | testImplementation 'junit:junit:4.12'
33 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
34 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
35 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
36 | }
37 | repositories {
38 | mavenCentral()
39 | }
40 |
41 |
--------------------------------------------------------------------------------
/pagelayout/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 |
--------------------------------------------------------------------------------
/pagelayout/src/androidTest/java/com/hankkin/pagelayout/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayout;
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.hankkin.pagelayout.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/pagelayout/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/pagelayout/src/main/java/com/hankkin/pagelayout/BlinkLayout.java:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayout;
2 |
3 | import android.animation.Animator;
4 | import android.animation.ObjectAnimator;
5 | import android.animation.ValueAnimator;
6 | import android.content.Context;
7 | import android.content.res.TypedArray;
8 | import android.graphics.Bitmap;
9 | import android.graphics.Canvas;
10 | import android.graphics.Color;
11 | import android.graphics.LinearGradient;
12 | import android.graphics.Paint;
13 | import android.graphics.PorterDuff;
14 | import android.graphics.PorterDuffXfermode;
15 | import android.graphics.Shader;
16 | import android.os.Build;
17 | import android.support.annotation.ColorInt;
18 | import android.support.annotation.RequiresApi;
19 | import android.support.v4.content.ContextCompat;
20 | import android.util.AttributeSet;
21 | import android.view.ViewTreeObserver;
22 | import android.widget.FrameLayout;
23 | import android.widget.LinearLayout;
24 |
25 | import org.jetbrains.annotations.NotNull;
26 |
27 | public class BlinkLayout extends LinearLayout {
28 |
29 | private Paint maskPaint;
30 |
31 | private ValueAnimator maskAnimator;
32 |
33 | private Bitmap localAvailableBitmap;
34 | private Bitmap localMaskBitmap;
35 |
36 | private Bitmap destinationBitmap;
37 | private Bitmap sourceMaskBitmap;
38 | private Canvas canvasForRendering;
39 |
40 | private int maskOffsetX;
41 |
42 | private boolean isAnimationStarted;
43 |
44 | private int shimmerAnimationDuration;
45 |
46 | @ColorInt
47 | private int shimmerColor;
48 |
49 | public BlinkLayout(Context context) {
50 | this(context, null);
51 | }
52 |
53 | public BlinkLayout(Context context, AttributeSet attrs) {
54 | this(context, attrs, 0);
55 | }
56 |
57 | public BlinkLayout(Context context, AttributeSet attrs, int defStyle) {
58 | super(context, attrs, defStyle);
59 |
60 | setWillNotDraw(false);
61 |
62 | maskPaint = new Paint();
63 | maskPaint.setAntiAlias(true);
64 | maskPaint.setDither(true);
65 | maskPaint.setFilterBitmap(true);
66 | maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
67 |
68 | TypedArray a = context.getTheme().obtainStyledAttributes(
69 | attrs,
70 | R.styleable.BlinkLayout,
71 | 0, 0);
72 |
73 | try {
74 | shimmerAnimationDuration = a.getInteger(R.styleable.BlinkLayout_blink_animation_duration, 1500);
75 | shimmerColor = a.getColor(R.styleable.BlinkLayout_blink_color, ContextCompat.getColor(context, R.color.shimmer_color));
76 | } finally {
77 | a.recycle();
78 | }
79 | }
80 |
81 | @Override
82 | protected void onDetachedFromWindow() {
83 | resetShimmering();
84 | super.onDetachedFromWindow();
85 | }
86 |
87 | @Override
88 | protected void dispatchDraw(Canvas canvas) {
89 | if (!isAnimationStarted || getWidth() <= 0 || getHeight() <= 0) {
90 | super.dispatchDraw(canvas);
91 | } else {
92 | dispatchDrawUsingBitmap(canvas);
93 | }
94 | }
95 |
96 | public void startShimmerAnimation() {
97 | if (isAnimationStarted) {
98 | return;
99 | }
100 |
101 | if (getWidth() == 0) {
102 | getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
103 | @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
104 | @Override
105 | public void onGlobalLayout() {
106 | BlinkLayout.this.getViewTreeObserver().removeOnGlobalLayoutListener(this);
107 | startShimmerAnimation();
108 | }
109 | });
110 |
111 | return;
112 | }
113 |
114 | Animator animator = getShimmerAnimation();
115 | animator.start();
116 | isAnimationStarted = true;
117 | }
118 |
119 | public void stopShimmerAnimation() {
120 | resetShimmering();
121 | }
122 |
123 | public void setShimmerColor(int shimmerColor) {
124 | this.shimmerColor = shimmerColor;
125 |
126 | if (isAnimationStarted) {
127 | resetShimmering();
128 | startShimmerAnimation();
129 | }
130 | }
131 |
132 | private void dispatchDrawUsingBitmap(Canvas canvas) {
133 | super.dispatchDraw(canvas);
134 |
135 | localAvailableBitmap = getDestinationBitmap();
136 | if (localAvailableBitmap == null) {
137 | return;
138 | }
139 |
140 | if (canvasForRendering == null) {
141 | canvasForRendering = new Canvas(localAvailableBitmap);
142 | }
143 |
144 | drawMask(canvasForRendering);
145 | canvas.save();
146 | canvas.clipRect(maskOffsetX, 0, maskOffsetX + getWidth() / 2, getHeight());
147 | canvas.drawBitmap(localAvailableBitmap, 0, 0, null);
148 | canvas.restore();
149 |
150 | localAvailableBitmap = null;
151 | }
152 |
153 | private void drawMask(Canvas renderCanvas) {
154 | localMaskBitmap = getSourceMaskBitmap();
155 | if (localMaskBitmap == null) {
156 | return;
157 | }
158 |
159 | renderCanvas.save();
160 | renderCanvas.clipRect(maskOffsetX, 0,
161 | maskOffsetX + localMaskBitmap.getWidth(),
162 | getHeight());
163 |
164 | super.dispatchDraw(renderCanvas);
165 | renderCanvas.drawBitmap(localMaskBitmap, maskOffsetX, 0, maskPaint);
166 |
167 | renderCanvas.restore();
168 |
169 | localMaskBitmap = null;
170 | }
171 |
172 | private void resetShimmering() {
173 | if (maskAnimator != null) {
174 | maskAnimator.end();
175 | maskAnimator.removeAllUpdateListeners();
176 | }
177 |
178 | maskAnimator = null;
179 | isAnimationStarted = false;
180 |
181 | releaseBitMaps();
182 | }
183 |
184 | private void releaseBitMaps() {
185 | if (sourceMaskBitmap != null) {
186 | sourceMaskBitmap.recycle();
187 | sourceMaskBitmap = null;
188 | }
189 |
190 | if (destinationBitmap != null) {
191 | destinationBitmap.recycle();
192 | destinationBitmap = null;
193 | }
194 |
195 | canvasForRendering = null;
196 | }
197 |
198 | private Bitmap getDestinationBitmap() {
199 | if (destinationBitmap == null) {
200 | destinationBitmap = createBitmap(getWidth(), getHeight());
201 | }
202 |
203 | return destinationBitmap;
204 | }
205 |
206 | private Bitmap getSourceMaskBitmap() {
207 | if (sourceMaskBitmap != null) {
208 | return sourceMaskBitmap;
209 | }
210 |
211 | int width = getWidth() / 2;
212 | int height = getHeight();
213 |
214 | sourceMaskBitmap = createBitmap(width, height);
215 | Canvas canvas = new Canvas(sourceMaskBitmap);
216 |
217 | LinearGradient gradient = new LinearGradient(
218 | 0, 0,
219 | width, 0,
220 | new int[]{Color.TRANSPARENT, shimmerColor, shimmerColor, Color.TRANSPARENT},
221 | new float[]{0.25F, 0.5F, 0.5F, 0.75F},
222 | Shader.TileMode.CLAMP);
223 |
224 | canvas.rotate(20, width / 2, height / 2);
225 |
226 | Paint paint = new Paint();
227 | paint.setShader(gradient);
228 | int padding = (int) (Math.sqrt(2) * Math.max(width, height)) / 2;
229 | canvas.drawRect(0, -padding, width, height + padding, paint);
230 |
231 | return sourceMaskBitmap;
232 | }
233 |
234 | private Animator getShimmerAnimation() {
235 | if (maskAnimator != null) {
236 | return maskAnimator;
237 | }
238 |
239 | final int animationToX = getWidth();
240 | final int animationFromX = -animationToX;
241 | final int shimmerBitmapWidth = getWidth() / 2;
242 | final int shimmerAnimationFullLength = animationToX - animationFromX;
243 |
244 | maskAnimator = ValueAnimator.ofFloat(0.0F, 1.0F);
245 | maskAnimator.setDuration(shimmerAnimationDuration);
246 | maskAnimator.setRepeatCount(ObjectAnimator.INFINITE);
247 |
248 | final float[] value = new float[1];
249 | maskAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
250 | @Override
251 | public void onAnimationUpdate(ValueAnimator animation) {
252 | value[0] = (Float) animation.getAnimatedValue();
253 | maskOffsetX = ((int) (animationFromX + shimmerAnimationFullLength * value[0]));
254 |
255 | if (maskOffsetX + shimmerBitmapWidth >= 0) {
256 | invalidate();
257 | }
258 | }
259 | });
260 |
261 | return maskAnimator;
262 | }
263 |
264 | private Bitmap createBitmap(int width, int height) {
265 | try {
266 | return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
267 | } catch (OutOfMemoryError e) {
268 | System.gc();
269 | return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
270 | }
271 | }
272 | }
--------------------------------------------------------------------------------
/pagelayout/src/main/java/com/hankkin/pagelayout/PageLayout.kt:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayout
2 |
3 | import android.app.Activity
4 | import android.content.Context
5 | import android.os.Looper
6 | import android.support.v4.app.Fragment
7 | import android.util.AttributeSet
8 | import android.view.LayoutInflater
9 | import android.view.View
10 | import android.view.ViewGroup
11 | import android.widget.FrameLayout
12 | import android.widget.TextView
13 |
14 | /**
15 | * Created by huanghaijie on 2018/9/5.
16 | */
17 | class PageLayout : FrameLayout {
18 |
19 | enum class State{
20 | EMPTY_TYPE,
21 | LOADING_TYPE,
22 | ERROR_TYPE,
23 | CONTENT_TYPE,
24 | CUSTOM_TYPE
25 | }
26 | private var mLoading: View? = null
27 | private var mEmpty: View? = null
28 | private var mError: View? = null
29 | private var mContent: View? = null
30 | private var mCustom: View? = null
31 | private var mContext: Context? = null
32 | private var mBlinkLayout: BlinkLayout? = null
33 | private var mCurrentState = State.CONTENT_TYPE
34 |
35 |
36 |
37 |
38 | constructor(context: Context) : super(context)
39 | constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
40 |
41 | constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
42 |
43 |
44 | private fun showView(type: State) {
45 | if (Looper.myLooper() == Looper.getMainLooper()) {
46 | changeView(type)
47 | } else {
48 | post { changeView(type) }
49 | }
50 | }
51 |
52 | private fun changeView(type: State) {
53 | mCurrentState = type
54 | mLoading?.visibility = if (type == State.LOADING_TYPE) View.VISIBLE else View.GONE
55 | mContent?.visibility = if (type == State.CONTENT_TYPE) View.VISIBLE else View.GONE
56 | mError?.visibility = if (type == State.ERROR_TYPE) View.VISIBLE else View.GONE
57 | mEmpty?.visibility = if (type == State.EMPTY_TYPE) View.VISIBLE else View.GONE
58 | mCustom?.visibility = if (type == State.CUSTOM_TYPE) View.VISIBLE else View.GONE
59 | }
60 |
61 |
62 |
63 | fun showLoading() {
64 | showView(State.LOADING_TYPE)
65 | mBlinkLayout?.apply {
66 | mBlinkLayout!!.startShimmerAnimation()
67 | }
68 | }
69 |
70 |
71 | fun showError() {
72 | showView(State.ERROR_TYPE)
73 | }
74 |
75 | fun showEmpty() {
76 | showView(State.EMPTY_TYPE)
77 | }
78 |
79 | fun hide() {
80 | showView(State.CONTENT_TYPE)
81 | mBlinkLayout?.apply {
82 | mBlinkLayout!!.stopShimmerAnimation()
83 | }
84 | }
85 |
86 | fun showCustom(){
87 | showView(State.CUSTOM_TYPE)
88 | }
89 |
90 |
91 | class Builder {
92 | private var mPageLayout: PageLayout
93 | private var mInflater: LayoutInflater
94 | private var mContext: Context
95 | private lateinit var mTvEmpty: TextView
96 | private lateinit var mTvError: TextView
97 | private lateinit var mTvErrorRetry: TextView
98 | private lateinit var mTvLoading: TextView
99 | private lateinit var mTvLoadingBlink: TextView
100 | private lateinit var mBlinkLayout: BlinkLayout
101 | private var mOnRetryClickListener: OnRetryClickListener? = null
102 |
103 |
104 | constructor(context: Context) {
105 | this.mContext = context
106 | this.mPageLayout = PageLayout(context)
107 | mInflater = LayoutInflater.from(context)
108 | }
109 |
110 | private fun initDefault() {
111 | if (mPageLayout.mEmpty == null) {
112 | setDefaultEmpty()
113 | }
114 | if (mPageLayout.mError == null) {
115 | setDefaultError()
116 | }
117 | if (mPageLayout.mLoading == null) {
118 | setDefaultLoading()
119 | }
120 | }
121 |
122 | private fun setDefaultEmpty() {
123 | mPageLayout.mEmpty = mInflater.inflate(R.layout.layout_empty, mPageLayout, false)
124 | .apply {
125 | mTvEmpty = findViewById(R.id.tv_page_empty)!!
126 | }
127 | mPageLayout.mEmpty?.visibility = View.GONE
128 | mPageLayout.addView(mPageLayout.mEmpty)
129 | }
130 |
131 | private fun setDefaultError() {
132 | mPageLayout.mError = mInflater.inflate(R.layout.layout_error, mPageLayout, false)
133 | .apply {
134 | mTvError = findViewById(R.id.tv_page_error)
135 | mTvErrorRetry = findViewById(R.id.tv_page_error_retry)
136 | mTvErrorRetry.setOnClickListener { mOnRetryClickListener?.onRetry() }
137 | }
138 | mPageLayout.mError?.visibility = View.GONE
139 | mPageLayout.addView(mPageLayout.mError)
140 | }
141 |
142 | private fun setDefaultLoading() {
143 | mPageLayout.mLoading = mInflater.inflate(R.layout.layout_loading, mPageLayout, false)
144 | .apply {
145 | mBlinkLayout = findViewById(R.id.blinklayout)
146 | mPageLayout.mBlinkLayout = mBlinkLayout
147 | mTvLoadingBlink = findViewById(R.id.tv_page_loading_blink)
148 | }
149 | mPageLayout.mLoading?.visibility = View.GONE
150 | mPageLayout.addView(mPageLayout.mLoading)
151 | }
152 |
153 | /**
154 | * 设置loading布局
155 | */
156 | fun setLoading(loading: Int,loadingTvId: Int): Builder {
157 | mInflater.inflate(loading, mPageLayout, false).apply {
158 | mTvLoading = findViewById(loadingTvId)
159 | mPageLayout.mLoading = this
160 | mPageLayout.addView(this)
161 | }
162 | return this
163 | }
164 |
165 | /**
166 | * 自定义错误布局
167 | * 默认样式,传入错误文案ID,及点击回调
168 | */
169 | fun setError(errorView: Int, errorClickId: Int, onRetryClickListener: OnRetryClickListener): Builder {
170 | mInflater.inflate(errorView, mPageLayout, false).apply {
171 | mPageLayout.mError = this
172 | mPageLayout.addView(this)
173 | mTvError = findViewById(errorClickId)
174 | mTvError.setOnClickListener { onRetryClickListener.onRetry() }
175 | }
176 | return this
177 | }
178 |
179 | /**
180 | * 自定义错误布局
181 | * 设置前需手动初始化好View中各个事件
182 | */
183 | fun setError(errorView: View): Builder {
184 | mPageLayout.apply {
185 | mError = errorView
186 | addView(errorView)
187 | }
188 | return this
189 | }
190 |
191 |
192 | /**
193 | * 自定义空布局
194 | */
195 | fun setEmpty(empty: Int,emptyTvId: Int): Builder {
196 | mInflater.inflate(empty, null, false).apply {
197 | mTvEmpty = findViewById(emptyTvId)
198 | mPageLayout.mEmpty = this
199 | mPageLayout.addView(this)
200 | }
201 | return this
202 | }
203 |
204 | /**
205 | * 自定义布局
206 | */
207 | fun setCustomView(view: View): Builder{
208 | mPageLayout.apply {
209 | mCustom = view
210 | addView(view)
211 | }
212 | return this
213 | }
214 |
215 | /**
216 | * 设置加载文案
217 | */
218 | fun setLoadingText(text: String): Builder {
219 | mTvLoading.text = text
220 | return this
221 | }
222 |
223 | /**
224 | * 设置默认闪烁加载文案
225 | */
226 | fun setDefaultLoadingBlinkText(text: String): Builder {
227 | mTvLoadingBlink.text = text
228 | return this
229 | }
230 |
231 | /**
232 | * 设置加载文字颜色
233 | */
234 | fun setLoadingTextColor(color: Int): Builder {
235 | mTvLoading.setTextColor(mContext.resources.getColor(color))
236 | return this
237 | }
238 |
239 | /**
240 | * 设置默认加载闪烁颜色
241 | */
242 | fun setDefaultLoadingBlinkColor(color: Int): Builder {
243 | mBlinkLayout.setShimmerColor(mContext.resources.getColor(color))
244 | return this
245 | }
246 |
247 | /**
248 | * 设置默认空布局文案
249 | */
250 | fun setDefaultEmptyText(text: String): Builder {
251 | mTvEmpty.text = text
252 | return this
253 | }
254 |
255 | /**
256 | * 设置默认空布局文案颜色
257 | */
258 | fun setDefaultEmptyTextColor(color: Int): Builder {
259 | mTvEmpty.setTextColor(mContext.resources.getColor(color))
260 | return this
261 | }
262 |
263 | /**
264 | * 设置默认错误布局文案
265 | */
266 | fun setDefaultErrorText(text: String): Builder {
267 | mTvError.text = text
268 | return this
269 | }
270 |
271 |
272 | /**
273 | * 设置默认错误布局文案颜色
274 | */
275 | fun setDefaultErrorTextColor(color: Int): Builder {
276 | mTvError.setTextColor(mContext.resources.getColor(color))
277 | return this
278 | }
279 |
280 | /**
281 | * 设置默认错误布局重试文案
282 | */
283 | fun setDefaultErrorRetryText(text: String): Builder {
284 | mTvErrorRetry.text = text
285 | return this
286 | }
287 |
288 | /**
289 | * 设置默认错误布局重试文案颜色
290 | */
291 | fun setDefaultErrorRetryTextColor(color: Int): Builder {
292 | mTvErrorRetry.setTextColor(mContext.resources.getColor(color))
293 | return this
294 | }
295 |
296 | /**
297 | * 设置空布局提醒图片
298 | */
299 | fun setEmptyDrawable(resId: Int): Builder{
300 | setTopDrawables(mTvEmpty,resId)
301 | return this
302 | }
303 |
304 | /**
305 | * 设置错误布局提醒图片
306 | */
307 | fun setErrorDrawable(resId: Int): Builder{
308 | setTopDrawables(mTvError,resId)
309 | return this
310 | }
311 |
312 |
313 | /**
314 | * 设置布局top drawable
315 | */
316 | private fun setTopDrawables(textView: TextView, resId: Int) {
317 | if (resId == 0){
318 | textView.setCompoundDrawables(null, null, null, null)
319 | }
320 | val drawable = mContext.resources.getDrawable(resId)
321 | drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)//必须设置图片大小,否则不显示
322 | textView.setCompoundDrawables(null, drawable, null, null)
323 | textView.compoundDrawablePadding = 20
324 | }
325 |
326 | /**
327 | * set target view for root
328 | */
329 | fun initPage(targetView: Any): Builder {
330 | var content: ViewGroup? = null
331 | when (targetView) {
332 | is Activity -> { //如果是Activity,获取到android.R.content
333 | mContext = targetView
334 | content = (mContext as Activity).findViewById(android.R.id.content)
335 | }
336 | is Fragment -> { //如果是Fragment获取到parent
337 | mContext = targetView.activity!!
338 | content = (targetView.view)?.parent as ViewGroup
339 | }
340 | is android.app.Fragment -> {
341 | mContext = targetView.activity!!
342 | content = (targetView.view)?.parent as ViewGroup
343 | }
344 | is View -> { //如果是View,也取到parent
345 | mContext = targetView.context
346 | try {
347 | content = (targetView.parent) as ViewGroup
348 | } catch (e: TypeCastException) {
349 | }
350 | }
351 | }
352 | val childCount = content?.childCount
353 | var index = 0
354 | val oldContent: View
355 | if (targetView is View) { //如果是某个线性布局或者相对布局时,遍历它的孩子,找到对应的索引,记录下来
356 | oldContent = targetView
357 | childCount?.let {
358 | for (i in 0 until childCount) {
359 | if (content!!.getChildAt(i) === oldContent) {
360 | index = i
361 | break
362 | }
363 | }
364 | }
365 |
366 | } else { //如果是Activity或者Fragment时,取到索引为第一个的View
367 | oldContent = content!!.getChildAt(0)
368 | }
369 | mPageLayout.mContent = oldContent //给PageLayout设置contentView
370 | mPageLayout.removeAllViews()
371 | content?.removeView(oldContent) //将本身content移除,并且把PageLayout添加到DecorView中去
372 | val lp = oldContent.layoutParams
373 | content?.addView(mPageLayout, index, lp)
374 | mPageLayout.addView(oldContent)
375 | initDefault() //设置默认状态布局
376 | return this
377 | }
378 |
379 |
380 | fun setOnRetryListener(onRetryListener: OnRetryClickListener): Builder {
381 | this.mOnRetryClickListener = onRetryListener
382 | return this
383 | }
384 |
385 | fun create() = mPageLayout
386 | }
387 |
388 | interface OnRetryClickListener {
389 | fun onRetry()
390 | }
391 |
392 | }
--------------------------------------------------------------------------------
/pagelayout/src/main/res/drawable/pic_empty.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/pagelayout/src/main/res/drawable/pic_empty.png
--------------------------------------------------------------------------------
/pagelayout/src/main/res/drawable/pic_error.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hankkin/PageLayoutDemo/b5be25579a5abce6c81b9ae9cf0879e993ece5a3/pagelayout/src/main/res/drawable/pic_error.png
--------------------------------------------------------------------------------
/pagelayout/src/main/res/layout/layout_empty.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
15 |
16 |
--------------------------------------------------------------------------------
/pagelayout/src/main/res/layout/layout_error.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
28 |
29 |
--------------------------------------------------------------------------------
/pagelayout/src/main/res/layout/layout_loading.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
18 |
19 |
--------------------------------------------------------------------------------
/pagelayout/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ff662c
4 | #ff662c
5 | #ff662c
6 | #b1b1b1
7 | #a2878787
8 |
9 |
10 |
--------------------------------------------------------------------------------
/pagelayout/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | EmptyPage
3 | 网络开小差了
4 | 点击重试
5 | 暂无新数据
6 |
7 |
--------------------------------------------------------------------------------
/pagelayout/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/pagelayout/src/test/java/com/hankkin/pagelayout/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.hankkin.pagelayout;
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 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':pagelayout', ':pagelayout-java'
2 |
--------------------------------------------------------------------------------