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