优化封装

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。最后的样子是长这样的
| 方法 | 注释 |
| :------------------------- | ----------------------------- |
| 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 {
PageLayout.Builder(this)
.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)
.setError(R.layout.layout_error_demo,R.id.tv_page_error_demo,object : PageLayout.OnRetryClickListener{
override fun onRetry() {
@@ -21,7 +21,9 @@ class DemoActivity : AppCompatActivity() {
})
.setEmptyDrawable(R.drawable.pic_empty)
.setErrorDrawable(R.drawable.pic_error)
.setLoadingText("Loading")
.create()
}
override fun onCreate(savedInstanceState: Bundle?) {

View File

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

View File

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

View File

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