优化封装
This commit is contained in:
@@ -15,20 +15,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
|
|
||||||
PageLayout.Builder(this)
|
|
||||||
.initPage(this)
|
|
||||||
.setEmpty(R.layout.layout_empty)
|
|
||||||
.setError(R.layout.layout_error)
|
|
||||||
.setLoading(R.layout.layout_loading)
|
|
||||||
.setOnRetryListener(object : PageLayout.OnRetryClickListener{
|
|
||||||
override fun onRetry() {
|
|
||||||
loadData()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.create()
|
|
||||||
|
|
||||||
page = PageLayout.Builder(this)
|
page = PageLayout.Builder(this)
|
||||||
.initPage(ll)
|
.initPage(ll)
|
||||||
|
.setEmpty(R.layout.layout_empty)
|
||||||
|
.setError(R.layout.layout_error)
|
||||||
|
.setLoading(R.layout.layout_loading_demo)
|
||||||
.setOnRetryListener(object : PageLayout.OnRetryClickListener{
|
.setOnRetryListener(object : PageLayout.OnRetryClickListener{
|
||||||
override fun onRetry() {
|
override fun onRetry() {
|
||||||
loadData()
|
loadData()
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ class PageLayout : FrameLayout {
|
|||||||
private lateinit var mTvError: TextView
|
private lateinit var mTvError: TextView
|
||||||
private lateinit var mTvLoading: TextView
|
private lateinit var mTvLoading: TextView
|
||||||
private lateinit var mBlinkLayout: BlinkLayout
|
private lateinit var mBlinkLayout: BlinkLayout
|
||||||
private lateinit var mOnRetryClickListener: OnRetryClickListener
|
private var mOnRetryClickListener: OnRetryClickListener? = null
|
||||||
|
|
||||||
|
|
||||||
constructor(context: Context) {
|
constructor(context: Context) {
|
||||||
@@ -130,17 +130,17 @@ class PageLayout : FrameLayout {
|
|||||||
initViews()
|
initViews()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initViews(){
|
private fun initViews() {
|
||||||
mPageLayout.mEmpty = mInflater.inflate(R.layout.layout_empty,mPageLayout,false)
|
mPageLayout.mEmpty = mInflater.inflate(R.layout.layout_empty, mPageLayout, false)
|
||||||
.apply {
|
.apply {
|
||||||
mTvEmpty = findViewById<TextView>(R.id.tv_page_empty)!!
|
mTvEmpty = findViewById<TextView>(R.id.tv_page_empty)!!
|
||||||
}
|
}
|
||||||
mPageLayout.mError = mInflater.inflate(R.layout.layout_error,mPageLayout,false)
|
mPageLayout.mError = mInflater.inflate(R.layout.layout_error, mPageLayout, false)
|
||||||
.apply {
|
.apply {
|
||||||
mTvError = findViewById(R.id.tv_page_error)
|
mTvError = findViewById(R.id.tv_page_error)
|
||||||
mTvError.setOnClickListener { mOnRetryClickListener.onRetry() }
|
mTvError.setOnClickListener { mOnRetryClickListener?.onRetry() }
|
||||||
}
|
}
|
||||||
mPageLayout.mLoading = mInflater.inflate(R.layout.layout_loading,mPageLayout,false)
|
mPageLayout.mLoading = mInflater.inflate(R.layout.layout_loading, mPageLayout, false)
|
||||||
.apply {
|
.apply {
|
||||||
mBlinkLayout = findViewById(R.id.blinklayout)
|
mBlinkLayout = findViewById(R.id.blinklayout)
|
||||||
mPageLayout.mBlinkLayout = mBlinkLayout
|
mPageLayout.mBlinkLayout = mBlinkLayout
|
||||||
@@ -153,41 +153,60 @@ class PageLayout : FrameLayout {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setError(error: Int): Builder {
|
fun setError(errorView: View, errorClickTv: TextView): Builder {
|
||||||
mPageLayout.mError = mInflater.inflate(error, mPageLayout, false)
|
mPageLayout.mError = errorView
|
||||||
|
mTvError = errorClickTv
|
||||||
|
errorClick(mTvError)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setError(errorView: View, errorClickId: Int): Builder {
|
||||||
|
mPageLayout.mError = errorView
|
||||||
|
mTvError = errorView.findViewById(errorClickId)
|
||||||
|
errorClick(mTvError)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun errorClick(errorClickView: View) {
|
||||||
|
if (mOnRetryClickListener != null) {
|
||||||
|
errorClickView.setOnClickListener {
|
||||||
|
mOnRetryClickListener?.onRetry()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw NullPointerException("Please setOnRetryClickListener")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun setEmpty(empty: Int): Builder {
|
fun setEmpty(empty: Int): Builder {
|
||||||
mPageLayout.mEmpty = mInflater.inflate(empty, null, false)
|
mPageLayout.mEmpty = mInflater.inflate(empty, null, false)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLoadingText(text: String){
|
fun setLoadingText(text: String) {
|
||||||
mTvLoading.text = text
|
mTvLoading.text = text
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLoadingTextColor(color: Int){
|
fun setLoadingTextColor(color: Int) {
|
||||||
mTvLoading.setTextColor(mContext.resources.getColor(color))
|
mTvLoading.setTextColor(mContext.resources.getColor(color))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLoadingBlinkColor(color: Int){
|
fun setLoadingBlinkColor(color: Int) {
|
||||||
mBlinkLayout.setShimmerColor(mContext.resources.getColor(color))
|
mBlinkLayout.setShimmerColor(mContext.resources.getColor(color))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setEmptyText(text: String){
|
fun setEmptyText(text: String) {
|
||||||
mTvEmpty.text = text
|
mTvEmpty.text = text
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setEmptyTextColor(color: Int){
|
fun setEmptyTextColor(color: Int) {
|
||||||
mTvEmpty.setTextColor(mContext.resources.getColor(color))
|
mTvEmpty.setTextColor(mContext.resources.getColor(color))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setErrorText(text: String){
|
fun setErrorText(text: String) {
|
||||||
mTvError.text = text
|
mTvError.text = text
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setErrorTextColor(color: Int){
|
fun setErrorTextColor(color: Int) {
|
||||||
mTvError.setTextColor(mContext.resources.getColor(color))
|
mTvError.setTextColor(mContext.resources.getColor(color))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user