├── .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 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 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 | ![image](http://lc-47sd2ifv.cn-n1.lcfile.com/f07096d512318918580c.png) 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 | ![image](http://lc-47sd2ifv.cn-n1.lcfile.com/a2a787511ddb0461bfb1.gif) 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 |