├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hankkin │ │ └── pagelayoutdemo_java │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── hankkin │ │ │ └── pagelayoutdemo_java │ │ │ ├── DefaultActivity.java │ │ │ ├── DemoActivity.java │ │ │ ├── FragmentActivity.java │ │ │ ├── MainActivity.java │ │ │ └── TestFragment.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ └── pic.png │ │ ├── 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_java │ └── 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 └── settings.gradle /.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/PageLayoutDemojava/e6a7c91479dffb2a597c9759098952bf9a605874/.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 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PageLayoutDemojava 2 | 一款简单的page切换 空布局、错误布局、加载布局,支持一键配置、定义,不需要xml编写 3 | 4 | > **该功能是支持单独为某个布局设置状态改变的,比如很多同学提到的我一个listview的数据没有获取到,fun initPage(targetView: Any),这个targetView你只需要设置成你的listview或者包裹你listview的parent布局就OK了,具体原理可以看下面的代码解析啊,遍历获取索引,然后记录索引值....** 5 | 6 | **如果您想看Kotlin版本,点击[https://github.com/Hankkin/PageLayoutDemo](https://github.com/Hankkin/PageLayoutDemo)** 7 | 8 | > 项目中我们经常会用到的加载数据,加载完数据后显示内容,如果没有数据显示一个空白页,这是如果网络错误了显示一个网络错误页,自定义一个PageLayout。 9 | 10 | ## 绪论 11 | Android中经常使用一个空白页和网络错误页用来提高用户体验,给用户一个较好的感官,如果获取到的数据为空,那么会显示一个空白数据页,如果在获取数据的过程中网络错误了,会显示一个网络异常页,像最近比较火的某东这样,见下图。网上也有一些开源的组件,大部分都是自定义继承某个布局在xml中让其作为跟布局,然后将自己的内容布局添加进去,效果也都不错,但是个人总觉得稍微有些麻烦,不是那么灵活,n多个xml布局都去定义,写的心烦,所以有了今天的主角。 12 | 13 | 14 | 15 | 16 | ## 思考 17 | 实现的思路实际上是和上面说的一样,只不过换了一种方式,我们手动获取到contentView,将它从DecorView中移除,然后交给PageLayout取管理。当时考虑的时候就是不想在每个xml中去写页面切换的布局,那么我们可不可以用Java代码去控制?带着下面几个问题一起来看一下。 18 | 19 | - 1.自定义一个布局让其作为跟布局 20 | - 2.提供切换**加载loading**、**空白页empty**、**错误页errror**、**内容页content**功能 21 | - 3.怎么让其取管理上边的四个页面? 22 | - 4.contentView怎么添加? 23 | - 5.如果我想切换的跟布局不是个Activity或者Fragment怎么办? 24 | - 6.因为切换页面状态的功能一般都是一个APP统一的,那么可不可以一键配置呢? 25 | 26 | ## 实现 27 | 28 | ### 1.代码设计 29 | 30 | 首先我们定义PageLayout继承FrameLayout或者LinearLayou或者其他的布局都可以,然后我们需要提供切换四个布局的功能,当然如果支持自定义就更好了,还有状态布局里面的一些属性,还方便一键配置,所以最后采用了Builder模式来创建,使用方式就和Android里面的**AlertDialog**一样,通过Builder去构建一个PageLayout。最后的样子是长这样的: 31 | 32 | | 方法 | 注释 | 33 | | :------------------------- | ----------------------------- | 34 | | showLoading() | 显示loading | 35 | | showError() | 显示错误布局 | 36 | | showEmpty() | 显示空布局 | 37 | | hide() | 显示内容布局 | 38 | | **Builder** | | 39 | | setLoading() | setLoadingText() | 40 | | setError() | setDefaultLoadingBlinkText() | 41 | | setEmpty() | setLoadingTextColor() | 42 | | setDefaultEmptyText() | setDefaultLoadingBlinkColor() | 43 | | setDefaultEmptyTextColor() | setDefaultErrorText() | 44 | | setDefaultErrorTextColor() | setEmptyDrawable() | 45 | | setErrorDrawable() | | 46 | 47 | 48 | 49 | **默认样式** 50 | ``` 51 | PageLayout.Builder(this) 52 | .initPage(ll_default) 53 | .setOnRetryListener(object : PageLayout.OnRetryClickListener{ 54 | override fun onRetry() { 55 | loadData() 56 | } 57 | 58 | }) 59 | .create() 60 | ``` 61 | 62 | **自定义样式** 63 | 64 | ``` 65 | PageLayout.Builder(this) 66 | .initPage(ll_demo) 67 | .setLoading(R.layout.layout_loading_demo) 68 | .setEmpty(R.layout.layout_empty_demo,R.id.tv_page_empty_demo) 69 | .setError(R.layout.layout_error_demo,R.id.tv_page_error_demo,object : PageLayout.OnRetryClickListener{ 70 | override fun onRetry() { 71 | loadData() 72 | } 73 | }) 74 | .setEmptyDrawable(R.drawable.pic_empty) 75 | .setErrorDrawable(R.drawable.pic_error) 76 | .create() 77 | ``` 78 | 79 | ### 2.设置PageLayout 80 | 81 | > 考虑好了代码设计方式之后,我们来具体实现功能,首先需要考虑上面的5,6点: 82 | 83 | **contentView怎么添加?** 84 | 85 | **如果我想切换的跟布局不是个Activity或者Fragment怎么办?** 86 | 87 | #### 1.Activity 88 | 如果我们要切换的跟布局是个Activity时,首先我们需要了解一下Android中的setContentView()方法,很熟悉,是我们新建完Activity后默认会在生命周期方法onCreate()中默认存在的,那么setContentView()做了些什么呢?我们先看一张图: 89 | 90 | ![image](http://lc-47sd2ifv.cn-n1.lcfile.com/f07096d512318918580c.png) 91 | 92 | > 一个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才是真正的显示视图。 93 | 94 | 再来看上面的那张图,可以说DecorView是一个界面的真正跟布局,TitleView我们可以通过设置theme样式显示隐藏的,状态布局切换时我们不考虑TitleView,我们只需要考虑ContentView,而ContentView也就是**android.R.id.content**,知道了这些我们来看看怎么获取将contenView交给PageLayout管理。 95 | 96 | #### 2.Fragment、View 97 | 98 | 如果我们要切换的跟布局是个Fragment、View时,我们只需要获取到它的parent 99 | 100 | ### 3.PageLayout设置跟布局 101 | 获取到了contentView跟布局后,我们要移除自己的显示内容的布局,并把这个布局交给PageLayout,下面看一下代码,注释的很详细了 102 | 103 | ``` 104 | /** 105 | * set target view for root 106 | */ 107 | fun initPage(targetView: Any): Builder { 108 | var content: ViewGroup? = null 109 | when (targetView) { 110 | is Activity -> { //如果是Activity,获取到android.R.content 111 | mContext = targetView 112 | content = (mContext as Activity).findViewById(android.R.id.content) 113 | } 114 | is Fragment -> { //如果是Fragment获取到parent 115 | mContext = targetView.activity!! 116 | content = (targetView.view)?.parent as ViewGroup 117 | } 118 | is View -> { //如果是View,也取到parent 119 | mContext = targetView.context 120 | try { 121 | content = (targetView.parent) as ViewGroup 122 | } catch (e: TypeCastException) { 123 | } 124 | } 125 | } 126 | val childCount = content?.childCount 127 | var index = 0 128 | val oldContent: View 129 | if (targetView is View) { //如果是某个线性布局或者相对布局时,遍历它的孩子,找到对应的索引,记录下来 130 | oldContent = targetView 131 | childCount?.let { 132 | for (i in 0 until childCount) { 133 | if (content!!.getChildAt(i) === oldContent) { 134 | index = i 135 | break 136 | } 137 | } 138 | } 139 | 140 | } else { //如果是Activity或者Fragment时,取到索引为第一个的View 141 | oldContent = content!!.getChildAt(0) 142 | } 143 | mPageLayout.mContent = oldContent //给PageLayout设置contentView 144 | mPageLayout.removeAllViews() 145 | content?.removeView(oldContent) //将本身content移除,并且把PageLayout添加到DecorView中去 146 | val lp = oldContent.layoutParams 147 | content?.addView(mPageLayout, index, lp) 148 | mPageLayout.addView(oldContent) 149 | initDefault() //设置默认状态布局 150 | return this 151 | } 152 | ``` 153 | 154 | 这样我们就解决了上面的5,6的问题。 155 | 156 | ### 4.其他 157 | 158 | - **因为错误布局中一般都包括一个点击重试的功能,如果你需要自定义布局,你可以在配置PageLayout之前,设置好错误布局和点击事件,然后setError进去,同时也提供了一个默认方式的方法** 159 | 160 | ``` 161 | fun setError(errorView: Int, errorClickId: Int, onRetryClickListener: OnRetryClickListener) 162 | ``` 163 | - **考虑到此功能的APP统一性,所以并没有提供过多的自定义功能,如果你需要的话,你都可以提前设置好View,然后进行set** 164 | - **之前和同事讨论,xml形式和代码形式哪个更方便更灵活,这些都属于个人喜好吧,如果你更喜欢在xml里写的话,你可以进行改造,也挺简单,目前没提供xml方式,PageLayout的初衷就是模仿AlertDialog方式,随时随地使用状态布局切换** 165 | - **你也可以在BaseActivity和BaseFragment中进行PageLayout的初始化,Demo中未使用,自行解决** 166 | 167 | ## 效果图 168 | 169 | ![image](http://lc-47sd2ifv.cn-n1.lcfile.com/a2a787511ddb0461bfb1.gif) 170 | 171 | 172 | 代码已经上传到Github[https://github.com/Hankkin/PageLayoutDemojava](https://github.com/Hankkin/PageLayoutDemojava) 173 | 174 | 175 | **Reading:一款不错的Material Desgin风格的Kotlin版本的开源APP** 176 | [https://github.com/Hankkin/Reading](https://github.com/Hankkin/Reading) 177 | 178 | 欢迎大家Follow、star、fork,谢谢 179 | 如果有不合适的地方,请提issues讨论指正 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.hankkin.pagelayoutdemo_java" 7 | minSdkVersion 15 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | dependencies { 21 | implementation fileTree(include: ['*.jar'], dir: 'libs') 22 | implementation 'com.android.support:appcompat-v7:27.1.1' 23 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 24 | testImplementation 'junit:junit:4.12' 25 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 26 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 27 | implementation project(':pagelayout-java') 28 | } 29 | -------------------------------------------------------------------------------- /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_java/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.hankkin.pagelayoutdemo_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() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.hankkin.pagelayoutdemo_java", 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_java/DefaultActivity.java: -------------------------------------------------------------------------------- 1 | package com.hankkin.pagelayoutdemo_java; 2 | 3 | import android.os.Handler; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.LayoutInflater; 7 | import android.view.Menu; 8 | import android.view.MenuInflater; 9 | import android.view.MenuItem; 10 | 11 | import com.hankkin.pagelayout_java.PageLayout; 12 | 13 | public class DefaultActivity extends AppCompatActivity { 14 | 15 | private PageLayout mPageLayout; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_default); 21 | 22 | mPageLayout = new PageLayout.Builder(this) 23 | .initPage(findViewById(R.id.ll_default)) 24 | .setCustomView(LayoutInflater.from(this).inflate(R.layout.layout_custom,null)) 25 | .setOnRetryListener(new PageLayout.OnRetryClickListener() { 26 | @Override 27 | public void onRetry() { 28 | loadData(); 29 | } 30 | }) 31 | .create(); 32 | 33 | loadData(); 34 | } 35 | 36 | private void loadData(){ 37 | mPageLayout.showLoading(); 38 | new Handler().post(new Runnable() { 39 | @Override 40 | public void run() { 41 | mPageLayout.hide(); 42 | } 43 | }); 44 | } 45 | 46 | @Override 47 | public boolean onCreateOptionsMenu(Menu menu) { 48 | new MenuInflater(this).inflate(R.menu.menus,menu); 49 | return super.onCreateOptionsMenu(menu); 50 | } 51 | 52 | @Override 53 | public boolean onOptionsItemSelected(MenuItem item) { 54 | switch (item.getItemId()){ 55 | case R.id.menu_content: 56 | mPageLayout.hide(); 57 | break; 58 | case R.id.menu_customer: 59 | mPageLayout.showCustom(); 60 | break; 61 | case R.id.menu_empty: 62 | mPageLayout.showEmpty(); 63 | break; 64 | case R.id.menu_error: 65 | mPageLayout.showError(); 66 | break; 67 | case R.id.menu_loading: 68 | mPageLayout.showLoading(); 69 | break; 70 | } 71 | return super.onOptionsItemSelected(item); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/java/com/hankkin/pagelayoutdemo_java/DemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.hankkin.pagelayoutdemo_java; 2 | 3 | import android.os.Handler; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.LayoutInflater; 7 | import android.view.Menu; 8 | import android.view.MenuInflater; 9 | import android.view.MenuItem; 10 | import android.view.View; 11 | import android.widget.ImageView; 12 | import android.widget.TextView; 13 | 14 | import com.hankkin.pagelayout_java.PageLayout; 15 | 16 | public class DemoActivity extends AppCompatActivity { 17 | 18 | private PageLayout mPageLayout; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_demo); 24 | 25 | View custom = LayoutInflater.from(this) 26 | .inflate(R.layout.layout_custom, null); 27 | ((ImageView) (custom.findViewById(R.id.iv_custom))).setImageResource(R.mipmap.icon_smile); 28 | ((TextView) custom.findViewById(R.id.tv_custom_content)).setText("This is PageLayout"); 29 | mPageLayout = new PageLayout.Builder(this) 30 | .initPage(findViewById(R.id.ll_demo)) 31 | .setLoading(R.layout.layout_loading_demo, R.id.tv_page_loading_demo) 32 | .setEmpty(R.layout.layout_empty_demo, R.id.tv_page_empty_demo) 33 | .setCustomView(custom) 34 | .setError(R.layout.layout_error_demo, R.id.tv_page_error_demo, new PageLayout.OnRetryClickListener() { 35 | @Override 36 | public void onRetry() { 37 | loadData(); 38 | } 39 | }) 40 | .setEmptyDrawable(R.drawable.pic_empty) 41 | .setErrorDrawable(R.drawable.pic_error) 42 | .setLoadingText("Loading") 43 | .create(); 44 | 45 | loadData(); 46 | } 47 | 48 | 49 | private void loadData() { 50 | mPageLayout.showLoading(); 51 | new Handler().post(new Runnable() { 52 | @Override 53 | public void run() { 54 | mPageLayout.hide(); 55 | } 56 | }); 57 | } 58 | 59 | @Override 60 | public boolean onCreateOptionsMenu(Menu menu) { 61 | new MenuInflater(this).inflate(R.menu.menus, menu); 62 | return super.onCreateOptionsMenu(menu); 63 | } 64 | 65 | @Override 66 | public boolean onOptionsItemSelected(MenuItem item) { 67 | switch (item.getItemId()) { 68 | case R.id.menu_content: 69 | mPageLayout.hide(); 70 | break; 71 | case R.id.menu_customer: 72 | mPageLayout.showCustom(); 73 | break; 74 | case R.id.menu_empty: 75 | mPageLayout.showEmpty(); 76 | break; 77 | case R.id.menu_error: 78 | mPageLayout.showError(); 79 | break; 80 | case R.id.menu_loading: 81 | mPageLayout.showLoading(); 82 | break; 83 | } 84 | return super.onOptionsItemSelected(item); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /app/src/main/java/com/hankkin/pagelayoutdemo_java/FragmentActivity.java: -------------------------------------------------------------------------------- 1 | package com.hankkin.pagelayoutdemo_java; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.FragmentManager; 5 | import android.support.v4.app.FragmentTransaction; 6 | 7 | public class FragmentActivity extends android.support.v4.app.FragmentActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.activity_fragment); 13 | 14 | TestFragment fragment = new TestFragment(); 15 | FragmentManager fragmentManager = getSupportFragmentManager(); 16 | FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 17 | fragmentTransaction.replace(R.id.container, fragment); 18 | fragmentTransaction.commit(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/hankkin/pagelayoutdemo_java/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.hankkin.pagelayoutdemo_java; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_main); 14 | 15 | findViewById(R.id.btn_default).setOnClickListener(new View.OnClickListener() { 16 | @Override 17 | public void onClick(View v) { 18 | startActivity(new Intent(MainActivity.this,DefaultActivity.class)); 19 | } 20 | }); 21 | 22 | findViewById(R.id.btn_demo).setOnClickListener(new View.OnClickListener() { 23 | @Override 24 | public void onClick(View v) { 25 | startActivity(new Intent(MainActivity.this,DemoActivity.class)); 26 | } 27 | }); 28 | 29 | findViewById(R.id.btn_fg).setOnClickListener(new View.OnClickListener() { 30 | @Override 31 | public void onClick(View v) { 32 | startActivity(new Intent(MainActivity.this,FragmentActivity.class)); 33 | } 34 | }); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/com/hankkin/pagelayoutdemo_java/TestFragment.java: -------------------------------------------------------------------------------- 1 | package com.hankkin.pagelayoutdemo_java; 2 | 3 | import android.content.Context; 4 | import android.net.Uri; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v4.app.Fragment; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | 12 | import com.hankkin.pagelayout_java.PageLayout; 13 | 14 | 15 | /** 16 | * A simple {@link Fragment} subclass. 17 | * Activities that contain this fragment must implement the 18 | * {@link TestFragment.OnFragmentInteractionListener} interface 19 | * to handle interaction events. 20 | * Use the {@link TestFragment#newInstance} factory method to 21 | * create an instance of this fragment. 22 | */ 23 | public class TestFragment extends Fragment { 24 | // TODO: Rename parameter arguments, choose names that match 25 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 26 | private static final String ARG_PARAM1 = "param1"; 27 | private static final String ARG_PARAM2 = "param2"; 28 | 29 | // TODO: Rename and change types of parameters 30 | private String mParam1; 31 | private String mParam2; 32 | 33 | private PageLayout mPageLayout; 34 | 35 | 36 | 37 | public TestFragment() { 38 | // Required empty public constructor 39 | } 40 | 41 | /** 42 | * Use this factory method to create a new instance of 43 | * this fragment using the provided parameters. 44 | * 45 | * @param param1 Parameter 1. 46 | * @param param2 Parameter 2. 47 | * @return A new instance of fragment TestFragment. 48 | */ 49 | // TODO: Rename and change types and number of parameters 50 | public static TestFragment newInstance(String param1, String param2) { 51 | TestFragment fragment = new TestFragment(); 52 | Bundle args = new Bundle(); 53 | args.putString(ARG_PARAM1, param1); 54 | args.putString(ARG_PARAM2, param2); 55 | fragment.setArguments(args); 56 | return fragment; 57 | } 58 | 59 | @Override 60 | public void onCreate(Bundle savedInstanceState) { 61 | super.onCreate(savedInstanceState); 62 | if (getArguments() != null) { 63 | mParam1 = getArguments().getString(ARG_PARAM1); 64 | mParam2 = getArguments().getString(ARG_PARAM2); 65 | } 66 | } 67 | 68 | @Override 69 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 70 | Bundle savedInstanceState) { 71 | // Inflate the layout for this fragment 72 | return inflater.inflate(R.layout.fragment_test, container, false); 73 | } 74 | 75 | 76 | 77 | @Override 78 | public void onAttach(Context context) { 79 | super.onAttach(context); 80 | 81 | } 82 | 83 | @Override 84 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 85 | super.onActivityCreated(savedInstanceState); 86 | PageLayout pageLayout = new PageLayout.Builder(getActivity()) 87 | // .initPage(getActivity().findViewById(R.id.fl_test)) 88 | .initPage(this) 89 | .create(); 90 | pageLayout.showLoading(); 91 | } 92 | 93 | @Override 94 | public void onDetach() { 95 | super.onDetach(); 96 | } 97 | 98 | /** 99 | * This interface must be implemented by activities that contain this 100 | * fragment to allow an interaction in this fragment to be communicated 101 | * to the activity and potentially other fragments contained in that 102 | * activity. 103 | *

104 | * See the Android Training lesson Communicating with Other Fragments for more information. 107 | */ 108 | public interface OnFragmentInteractionListener { 109 | // TODO: Update argument type and name 110 | void onFragmentInteraction(Uri uri); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/pic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hankkin/PageLayoutDemojava/e6a7c91479dffb2a597c9759098952bf9a605874/app/src/main/res/drawable/pic.png -------------------------------------------------------------------------------- /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 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |