优化封装

This commit is contained in:
huanghaijie
2018-09-17 10:01:49 +08:00
parent 5cdb1646ab
commit 4fcdb590dd
6 changed files with 83 additions and 16 deletions

View File

@@ -34,6 +34,22 @@ Android中经常使用一个空白页和网络错误页用来提高用户体验
首先我们定义PageLayout继承FrameLayout或者LinearLayou或者其他的布局都可以然后我们需要提供切换四个布局的功能当然如果支持自定义就更好了还有状态布局里面的一些属性还方便一键配置所以最后采用了Builder模式来创建使用方式就和Android里面的**AlertDialog**一样通过Builder去构建一个PageLayout。最后的样子是长这样的 首先我们定义PageLayout继承FrameLayout或者LinearLayou或者其他的布局都可以然后我们需要提供切换四个布局的功能当然如果支持自定义就更好了还有状态布局里面的一些属性还方便一键配置所以最后采用了Builder模式来创建使用方式就和Android里面的**AlertDialog**一样通过Builder去构建一个PageLayout。最后的样子是长这样的
| 方法 | 注释 |
| :------------------------- | ----------------------------- |
| showLoading() | 显示loading |
| showError() | 显示错误布局 |
| showEmpty() | 显示空布局 |
| hide() | 显示内容布局 |
| **Builder** | |
| setLoading() | setLoadingText() |
| setError() | setDefaultLoadingBlinkText() |
| setEmpty() | setLoadingTextColor() |
| setDefaultEmptyText() | setDefaultLoadingBlinkColor() |
| setDefaultEmptyTextColor() | setDefaultErrorText() |
| setDefaultErrorTextColor() | setEmptyDrawable() |
| setErrorDrawable() | |
**默认样式** **默认样式**
``` ```

View File

@@ -12,7 +12,7 @@ class DemoActivity : AppCompatActivity() {
private val mPageLayout by lazy { private val mPageLayout by lazy {
PageLayout.Builder(this) PageLayout.Builder(this)
.initPage(ll_demo) .initPage(ll_demo)
.setLoading(R.layout.layout_loading_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) .setEmpty(R.layout.layout_empty_demo,R.id.tv_page_empty_demo)
.setError(R.layout.layout_error_demo,R.id.tv_page_error_demo,object : PageLayout.OnRetryClickListener{ .setError(R.layout.layout_error_demo,R.id.tv_page_error_demo,object : PageLayout.OnRetryClickListener{
override fun onRetry() { override fun onRetry() {
@@ -21,7 +21,9 @@ class DemoActivity : AppCompatActivity() {
}) })
.setEmptyDrawable(R.drawable.pic_empty) .setEmptyDrawable(R.drawable.pic_empty)
.setErrorDrawable(R.drawable.pic_error) .setErrorDrawable(R.drawable.pic_error)
.setLoadingText("Loading")
.create() .create()
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {

View File

@@ -13,6 +13,7 @@
android:layout_marginTop="10dp" /> android:layout_marginTop="10dp" />
<TextView <TextView
android:id="@+id/tv_page_loading_demo"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"

View File

@@ -22,6 +22,8 @@ import android.view.ViewTreeObserver;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import org.jetbrains.annotations.NotNull;
public class BlinkLayout extends LinearLayout { public class BlinkLayout extends LinearLayout {
private Paint maskPaint; private Paint maskPaint;

View File

@@ -134,21 +134,28 @@ class PageLayout : FrameLayout {
.apply { .apply {
mBlinkLayout = findViewById(R.id.blinklayout) mBlinkLayout = findViewById(R.id.blinklayout)
mPageLayout.mBlinkLayout = mBlinkLayout mPageLayout.mBlinkLayout = mBlinkLayout
mTvLoading = findViewById(R.id.tv_page_loading)
mTvLoadingBlink = findViewById(R.id.tv_page_loading_blink) mTvLoadingBlink = findViewById(R.id.tv_page_loading_blink)
} }
mPageLayout.mLoading?.visibility = View.GONE mPageLayout.mLoading?.visibility = View.GONE
mPageLayout.addView(mPageLayout.mLoading) mPageLayout.addView(mPageLayout.mLoading)
} }
fun setLoading(loading: Int): Builder { /**
* 设置loading布局
*/
fun setLoading(loading: Int,loadingTvId: Int): Builder {
mInflater.inflate(loading, mPageLayout, false).apply { mInflater.inflate(loading, mPageLayout, false).apply {
mTvLoading = findViewById(loadingTvId)
mPageLayout.mLoading = this mPageLayout.mLoading = this
mPageLayout.addView(this) mPageLayout.addView(this)
} }
return this return this
} }
/**
* 自定义错误布局
* 默认样式传入错误文案ID及点击回调
*/
fun setError(errorView: Int, errorClickId: Int, onRetryClickListener: OnRetryClickListener): Builder { fun setError(errorView: Int, errorClickId: Int, onRetryClickListener: OnRetryClickListener): Builder {
mInflater.inflate(errorView, mPageLayout, false).apply { mInflater.inflate(errorView, mPageLayout, false).apply {
mPageLayout.mError = this mPageLayout.mError = this
@@ -159,6 +166,10 @@ class PageLayout : FrameLayout {
return this return this
} }
/**
* 自定义错误布局
* 设置前需手动初始化好View中各个事件
*/
fun setError(errorView: View): Builder { fun setError(errorView: View): Builder {
mPageLayout.mError = errorView mPageLayout.mError = errorView
mPageLayout.addView(errorView) mPageLayout.addView(errorView)
@@ -166,6 +177,9 @@ class PageLayout : FrameLayout {
} }
/**
* 自定义空布局
*/
fun setEmpty(empty: Int,emptyTvId: Int): Builder { fun setEmpty(empty: Int,emptyTvId: Int): Builder {
mInflater.inflate(empty, null, false).apply { mInflater.inflate(empty, null, false).apply {
mTvEmpty = findViewById(emptyTvId) mTvEmpty = findViewById(emptyTvId)
@@ -175,54 +189,91 @@ class PageLayout : FrameLayout {
return this return this
} }
fun setDefaultLoadingText(text: String): Builder { /**
* 设置加载文案
*/
fun setLoadingText(text: String): Builder {
mTvLoading.text = text mTvLoading.text = text
return this return this
} }
/**
* 设置默认闪烁加载文案
*/
fun setDefaultLoadingBlinkText(text: String): Builder { fun setDefaultLoadingBlinkText(text: String): Builder {
mTvLoadingBlink.text = text mTvLoadingBlink.text = text
return this return this
} }
fun setDefaultLoadingTextColor(color: Int): Builder { /**
* 设置加载文字颜色
*/
fun setLoadingTextColor(color: Int): Builder {
mTvLoading.setTextColor(mContext.resources.getColor(color)) mTvLoading.setTextColor(mContext.resources.getColor(color))
return this return this
} }
/**
* 设置默认加载闪烁颜色
*/
fun setDefaultLoadingBlinkColor(color: Int): Builder { fun setDefaultLoadingBlinkColor(color: Int): Builder {
mBlinkLayout.setShimmerColor(mContext.resources.getColor(color)) mBlinkLayout.setShimmerColor(mContext.resources.getColor(color))
return this return this
} }
/**
* 设置默认空布局文案
*/
fun setDefaultEmptyText(text: String): Builder { fun setDefaultEmptyText(text: String): Builder {
mTvEmpty.text = text mTvEmpty.text = text
return this return this
} }
/**
* 设置默认空布局文案颜色
*/
fun setDefaultEmptyTextColor(color: Int): Builder { fun setDefaultEmptyTextColor(color: Int): Builder {
mTvEmpty.setTextColor(mContext.resources.getColor(color)) mTvEmpty.setTextColor(mContext.resources.getColor(color))
return this return this
} }
fun setDefaultErrorText(text: String) { /**
* 设置默认错误布局文案
*/
fun setDefaultErrorText(text: String): Builder {
mTvError.text = text mTvError.text = text
return this
} }
fun setDefaultErrorTextColor(color: Int) {
/**
* 设置默认错误布局文案颜色
*/
fun setDefaultErrorTextColor(color: Int): Builder {
mTvError.setTextColor(mContext.resources.getColor(color)) mTvError.setTextColor(mContext.resources.getColor(color))
return this
} }
/**
* 设置空布局提醒图片
*/
fun setEmptyDrawable(resId: Int): Builder{ fun setEmptyDrawable(resId: Int): Builder{
setTopDrawables(mTvEmpty,resId) setTopDrawables(mTvEmpty,resId)
return this return this
} }
/**
* 设置错误布局提醒图片
*/
fun setErrorDrawable(resId: Int): Builder{ fun setErrorDrawable(resId: Int): Builder{
setTopDrawables(mTvError,resId) setTopDrawables(mTvError,resId)
return this return this
} }
/**
* 设置布局top drawable
*/
private fun setTopDrawables(textView: TextView, resId: Int) { private fun setTopDrawables(textView: TextView, resId: Int) {
if (resId == 0){ if (resId == 0){
textView.setCompoundDrawables(null, null, null, null) textView.setCompoundDrawables(null, null, null, null)
@@ -294,4 +345,8 @@ class PageLayout : FrameLayout {
interface OnRetryClickListener { interface OnRetryClickListener {
fun onRetry() fun onRetry()
} }
abstract class CustomView{
abstract fun setView(pageLayout: PageLayout)
}
} }

View File

@@ -6,15 +6,6 @@
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<TextView
android:id="@+id/tv_page_loading"
android:textSize="18sp"
android:textColor="@color/blink_color"
android:text="加载中..."
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView <TextView
android:text="PageLaout" android:text="PageLaout"
android:textStyle="bold" android:textStyle="bold"