布局优化
支持自定义布局
This commit is contained in:
@@ -3,6 +3,9 @@ package com.hankkin.pagelayoutdemo
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.widget.ImageView
|
||||
import com.hankkin.pagelayout.PageLayout
|
||||
import kotlinx.android.synthetic.main.activity_default.*
|
||||
|
||||
@@ -11,11 +14,15 @@ class DefaultActivity : AppCompatActivity() {
|
||||
private val mPageLayout by lazy {
|
||||
PageLayout.Builder(this)
|
||||
.initPage(ll_default)
|
||||
.setOnRetryListener(object : PageLayout.OnRetryClickListener{
|
||||
.setCustomView(layoutInflater.inflate(R.layout.layout_custom, null)
|
||||
.apply {
|
||||
//todo
|
||||
findViewById<ImageView>(R.id.iv_custom).setImageResource(R.mipmap.icon_smile)
|
||||
})
|
||||
.setOnRetryListener(object : PageLayout.OnRetryClickListener {
|
||||
override fun onRetry() {
|
||||
loadData()
|
||||
}
|
||||
|
||||
})
|
||||
.create()
|
||||
}
|
||||
@@ -26,29 +33,33 @@ class DefaultActivity : AppCompatActivity() {
|
||||
|
||||
loadData()
|
||||
|
||||
empty.setOnClickListener {
|
||||
mPageLayout.showEmpty()
|
||||
}
|
||||
|
||||
error.setOnClickListener {
|
||||
mPageLayout.showError()
|
||||
}
|
||||
|
||||
loading.setOnClickListener {
|
||||
mPageLayout.showLoading()
|
||||
}
|
||||
|
||||
content.setOnClickListener {
|
||||
mPageLayout.hide()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun loadData(){
|
||||
private fun loadData() {
|
||||
mPageLayout.showLoading()
|
||||
Handler().postDelayed({
|
||||
mPageLayout.hide()
|
||||
},3000)
|
||||
}, 3000)
|
||||
}
|
||||
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
menuInflater.inflate(R.menu.menus, menu)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.menu_loading -> loadData()
|
||||
R.id.menu_empty -> mPageLayout.showEmpty()
|
||||
R.id.menu_error -> mPageLayout.showError()
|
||||
R.id.menu_content -> mPageLayout.hide()
|
||||
R.id.menu_customer -> mPageLayout.showCustom()
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
return super.onOptionsItemSelected(item)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package com.hankkin.pagelayoutdemo
|
||||
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.widget.Toast
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import com.hankkin.pagelayout.PageLayout
|
||||
import kotlinx.android.synthetic.main.activity_demo.*
|
||||
|
||||
@@ -14,6 +17,12 @@ class DemoActivity : AppCompatActivity() {
|
||||
.initPage(ll_demo)
|
||||
.setLoading(R.layout.layout_loading_demo,R.id.tv_page_loading_demo)
|
||||
.setEmpty(R.layout.layout_empty_demo,R.id.tv_page_empty_demo)
|
||||
.setCustomView(layoutInflater.inflate(R.layout.layout_custom, null)
|
||||
.apply {
|
||||
//todo
|
||||
findViewById<ImageView>(R.id.iv_custom).setImageResource(R.mipmap.icon_smile)
|
||||
findViewById<TextView>(R.id.tv_custom_content).setText("This is PageLayout")
|
||||
})
|
||||
.setError(R.layout.layout_error_demo,R.id.tv_page_error_demo,object : PageLayout.OnRetryClickListener{
|
||||
override fun onRetry() {
|
||||
loadData()
|
||||
@@ -32,17 +41,6 @@ class DemoActivity : AppCompatActivity() {
|
||||
|
||||
loadData()
|
||||
|
||||
loading_demo.setOnClickListener {
|
||||
loadData()
|
||||
}
|
||||
|
||||
error_demo.setOnClickListener {
|
||||
mPageLayout.showError()
|
||||
}
|
||||
|
||||
empty_demo.setOnClickListener {
|
||||
mPageLayout.showEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadData(){
|
||||
@@ -51,4 +49,22 @@ class DemoActivity : AppCompatActivity() {
|
||||
mPageLayout.hide()
|
||||
},3000)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
menuInflater.inflate(R.menu.menus, menu)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.menu_loading -> loadData()
|
||||
R.id.menu_empty -> mPageLayout.showEmpty()
|
||||
R.id.menu_error -> mPageLayout.showError()
|
||||
R.id.menu_content -> mPageLayout.hide()
|
||||
R.id.menu_customer -> mPageLayout.showCustom()
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
return super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,36 +7,6 @@
|
||||
android:orientation="vertical"
|
||||
tools:context=".DefaultActivity">
|
||||
|
||||
<LinearLayout
|
||||
android:gravity="center"
|
||||
android:padding="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<Button
|
||||
android:id="@+id/loading"
|
||||
android:text="加载"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/empty"
|
||||
android:text="空布局"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/error"
|
||||
android:text="错误布局"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/content"
|
||||
android:text="内容布局"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</LinearLayout>
|
||||
<LinearLayout
|
||||
android:layout_weight="1"
|
||||
android:padding="20dp"
|
||||
|
||||
@@ -8,31 +8,6 @@
|
||||
tools:context=".DemoActivity">
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:padding="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<Button
|
||||
android:id="@+id/loading_demo"
|
||||
android:text="自定义加载"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/empty_demo"
|
||||
android:text="自定义空布局"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/error_demo"
|
||||
android:text="自定义错误布局"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_weight="1"
|
||||
android:padding="20dp"
|
||||
|
||||
41
app/src/main/res/layout/layout_custom.xml
Normal file
41
app/src/main/res/layout/layout_custom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
android:layout_centerInParent="true"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/iv_custom"
|
||||
android:src="@mipmap/icon_smile"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:layout_marginTop="10dp"
|
||||
android:text="欢迎您"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_custom_content"
|
||||
android:paddingBottom="3dp"
|
||||
android:paddingTop="3dp"
|
||||
android:text="This is PageLayout"
|
||||
android:textColor="@color/colorPrimary"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
<TextView
|
||||
android:text="自定义空布局"
|
||||
android:textSize="20sp"
|
||||
android:id="@+id/tv_page_empty_demo"
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
<TextView
|
||||
android:gravity="center"
|
||||
android:text="自定义错误布局"
|
||||
android:textSize="20sp"
|
||||
android:id="@+id/tv_page_error_demo"
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
@@ -13,10 +13,11 @@
|
||||
android:layout_marginTop="10dp" />
|
||||
|
||||
<TextView
|
||||
android:layout_marginTop="10dp"
|
||||
android:id="@+id/tv_page_loading_demo"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:text="正在加载中..."
|
||||
android:textSize="18sp" />
|
||||
android:text="加载中..."
|
||||
/>
|
||||
</LinearLayout>
|
||||
27
app/src/main/res/menu/menus.xml
Executable file
27
app/src/main/res/menu/menus.xml
Executable file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
tools:context="me.bakumon.ugank.module.webview.WebViewActivity">
|
||||
<item
|
||||
android:id="@+id/menu_loading"
|
||||
android:title="加载中"
|
||||
app:showAsAction="never"/>
|
||||
<item
|
||||
android:id="@+id/menu_empty"
|
||||
android:title="空数据"
|
||||
app:showAsAction="never"/>
|
||||
<item
|
||||
android:id="@+id/menu_error"
|
||||
android:title="出错"
|
||||
app:showAsAction="never"/>
|
||||
<item
|
||||
android:id="@+id/menu_content"
|
||||
android:title="内容布局"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_customer"
|
||||
android:title="自定义布局"
|
||||
app:showAsAction="never"/>
|
||||
</menu>
|
||||
BIN
app/src/main/res/mipmap-xhdpi/icon_smile.png
Normal file
BIN
app/src/main/res/mipmap-xhdpi/icon_smile.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#FF4081</color>
|
||||
<color name="colorPrimary">#ff662c</color>
|
||||
<color name="colorPrimaryDark">#ff662c</color>
|
||||
<color name="colorAccent">#ff662c</color>
|
||||
</resources>
|
||||
|
||||
@@ -9,8 +9,8 @@ android {
|
||||
defaultConfig {
|
||||
minSdkVersion 15
|
||||
targetSdkVersion 27
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
versionCode 20180925
|
||||
versionName "1.1"
|
||||
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
|
||||
/**
|
||||
@@ -21,12 +20,14 @@ class PageLayout : FrameLayout {
|
||||
EMPTY_TYPE,
|
||||
LOADING_TYPE,
|
||||
ERROR_TYPE,
|
||||
CONTENT_TYPE
|
||||
CONTENT_TYPE,
|
||||
CUSTOM_TYPE
|
||||
}
|
||||
private var mLoading: View? = null
|
||||
private var mEmpty: View? = null
|
||||
private var mError: View? = null
|
||||
private var mContent: View? = null
|
||||
private var mCustom: View? = null
|
||||
private var mContext: Context? = null
|
||||
private var mBlinkLayout: BlinkLayout? = null
|
||||
private var mCurrentState = State.CONTENT_TYPE
|
||||
@@ -52,6 +53,7 @@ class PageLayout : FrameLayout {
|
||||
mContent?.visibility = if (type == State.CONTENT_TYPE) View.VISIBLE else View.GONE
|
||||
mError?.visibility = if (type == State.ERROR_TYPE) View.VISIBLE else View.GONE
|
||||
mEmpty?.visibility = if (type == State.EMPTY_TYPE) View.VISIBLE else View.GONE
|
||||
mCustom?.visibility = if (type == State.CUSTOM_TYPE) View.VISIBLE else View.GONE
|
||||
}
|
||||
|
||||
|
||||
@@ -79,6 +81,10 @@ class PageLayout : FrameLayout {
|
||||
}
|
||||
}
|
||||
|
||||
fun showCustom(){
|
||||
showView(State.CUSTOM_TYPE)
|
||||
}
|
||||
|
||||
|
||||
class Builder {
|
||||
private var mPageLayout: PageLayout
|
||||
@@ -86,6 +92,7 @@ class PageLayout : FrameLayout {
|
||||
private var mContext: Context
|
||||
private lateinit var mTvEmpty: TextView
|
||||
private lateinit var mTvError: TextView
|
||||
private lateinit var mTvErrorRetry: TextView
|
||||
private lateinit var mTvLoading: TextView
|
||||
private lateinit var mTvLoadingBlink: TextView
|
||||
private lateinit var mBlinkLayout: BlinkLayout
|
||||
@@ -123,7 +130,8 @@ class PageLayout : FrameLayout {
|
||||
mPageLayout.mError = mInflater.inflate(R.layout.layout_error, mPageLayout, false)
|
||||
.apply {
|
||||
mTvError = findViewById(R.id.tv_page_error)
|
||||
mTvError.setOnClickListener { mOnRetryClickListener?.onRetry() }
|
||||
mTvErrorRetry = findViewById(R.id.tv_page_error_retry)
|
||||
mTvErrorRetry.setOnClickListener { mOnRetryClickListener?.onRetry() }
|
||||
}
|
||||
mPageLayout.mError?.visibility = View.GONE
|
||||
mPageLayout.addView(mPageLayout.mError)
|
||||
@@ -171,8 +179,10 @@ class PageLayout : FrameLayout {
|
||||
* 设置前需手动初始化好View中各个事件
|
||||
*/
|
||||
fun setError(errorView: View): Builder {
|
||||
mPageLayout.mError = errorView
|
||||
mPageLayout.addView(errorView)
|
||||
mPageLayout.apply {
|
||||
mError = errorView
|
||||
addView(errorView)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -189,6 +199,17 @@ class PageLayout : FrameLayout {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义布局
|
||||
*/
|
||||
fun setCustomView(view: View): Builder{
|
||||
mPageLayout.apply {
|
||||
mCustom = view
|
||||
addView(view)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置加载文案
|
||||
*/
|
||||
@@ -254,6 +275,22 @@ class PageLayout : FrameLayout {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认错误布局重试文案
|
||||
*/
|
||||
fun setDefaultErrorRetryText(text: String): Builder {
|
||||
mTvErrorRetry.text = text
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认错误布局重试文案颜色
|
||||
*/
|
||||
fun setDefaultErrorRetryTextColor(color: Int): Builder {
|
||||
mTvErrorRetry.setTextColor(mContext.resources.getColor(color))
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置空布局提醒图片
|
||||
*/
|
||||
@@ -346,7 +383,4 @@ class PageLayout : FrameLayout {
|
||||
fun onRetry()
|
||||
}
|
||||
|
||||
abstract class CustomView{
|
||||
abstract fun setView(pageLayout: PageLayout)
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 2.3 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 4.3 KiB |
@@ -6,9 +6,8 @@
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:textColor="@android:color/black"
|
||||
android:drawableTop="@drawable/pic_empty"
|
||||
android:text="@string/page_empty"
|
||||
android:textSize="20sp"
|
||||
android:id="@+id/tv_page_empty"
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
@@ -1,17 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.hankkin.pagelayout.BlinkLayout
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:drawableTop="@drawable/pic_error"
|
||||
android:gravity="center"
|
||||
android:text="@string/page_error"
|
||||
android:textSize="20sp"
|
||||
android:id="@+id/tv_page_error"
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:textColor="@color/colorPrimary"
|
||||
android:layout_marginTop="10dp"
|
||||
android:textSize="13sp"
|
||||
android:gravity="center"
|
||||
android:text="@string/page_error_retry"
|
||||
android:id="@+id/tv_page_error_retry"
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</com.hankkin.pagelayout.BlinkLayout>
|
||||
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#FF4081</color>
|
||||
<color name="colorPrimary">#ff662c</color>
|
||||
<color name="colorPrimaryDark">#ff662c</color>
|
||||
<color name="colorAccent">#ff662c</color>
|
||||
<color name="blink_color">#b1b1b1</color>
|
||||
<color name="shimmer_color">#a2878787</color>
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<resources>
|
||||
<string name="app_name">EmptyPage</string>
|
||||
<string name="page_error">网络开小差了\n点击重试</string>
|
||||
<string name="page_error">网络开小差了</string>
|
||||
<string name="page_error_retry">点击重试</string>
|
||||
<string name="page_empty">暂无新数据</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user