11package com.f1reking.statuslayout.library
22
3+ import android.support.annotation.DrawableRes
34import android.support.annotation.LayoutRes
45import android.support.annotation.StringRes
56import android.text.TextUtils
67import android.view.LayoutInflater
78import android.view.View
9+ import android.widget.ImageView
810import android.widget.TextView
911
1012/* *
@@ -27,6 +29,9 @@ class StatusLayout {
2729 private var emptyText: String = " "
2830 private var errorText: String = " "
2931
32+ @DrawableRes private var emptyImgID: Int = 0
33+ @DrawableRes private var errorImgID: Int = 0
34+
3035 private var inflater: LayoutInflater ? = null
3136 private var statusLayoutHelper: StatusLayoutHelper ? = null
3237 private var statusClickListener: StatusClickListener ? = null
@@ -48,10 +53,12 @@ class StatusLayout {
4853 this .emptyLayout = builder.emptyLayout
4954 this .emptyLayoutID = builder.emptyLayoutID
5055 this .emptyText = builder.emptyText
56+ this .emptyImgID = builder.emptyImgID
5157
5258 this .errorLayout = builder.errorLayout
5359 this .errorLayoutID = builder.errorLayoutID
5460 this .errorText = builder.errorText
61+ this .errorImgID = builder.errorImgID
5562
5663 this .statusClickListener = builder.statusClickListener
5764 this .statusLayoutHelper = StatusLayoutHelper (contentLayout)
@@ -102,11 +109,16 @@ class StatusLayout {
102109 return
103110 }
104111
105- val view = emptyLayout!! .findViewById<TextView >(R .id.tv_click_empty)
106- if (null == view) { // 防止自定义布局ID出错
112+ if (emptyImgID > 0 ) {
113+ val emptyImageView = emptyLayout!! .findViewById<ImageView >(R .id.iv_status_empty)
114+ emptyImageView?.setImageResource(emptyImgID)
115+ }
116+
117+ val emptyClickView = emptyLayout!! .findViewById<TextView >(R .id.tv_click_empty)
118+ if (null == emptyClickView) { // 防止自定义布局ID出错
107119 return
108120 }
109- view .setOnClickListener {
121+ emptyClickView .setOnClickListener {
110122 statusClickListener!! .onEmptyClick(it)
111123 }
112124 }
@@ -131,6 +143,11 @@ class StatusLayout {
131143 return
132144 }
133145
146+ if (errorImgID > 0 ) {
147+ val emptyImageView = errorLayout!! .findViewById<ImageView >(R .id.iv_status_error)
148+ emptyImageView?.setImageResource(errorImgID)
149+ }
150+
134151 val view = errorLayout!! .findViewById<TextView >(R .id.tv_click_error)
135152 if (null == view) { // 防止自定义布局ID出错
136153 return
@@ -155,6 +172,9 @@ class StatusLayout {
155172 var emptyText: String = " "
156173 var errorText: String = " "
157174
175+ @DrawableRes var emptyImgID: Int = 0
176+ @DrawableRes var errorImgID: Int = 0
177+
158178 lateinit var statusClickListener: StatusClickListener
159179
160180 constructor (contentLayout: View ) {
@@ -203,6 +223,11 @@ class StatusLayout {
203223 return this
204224 }
205225
226+ fun setEmptyImg (@DrawableRes emptyImgID : Int ): Builder {
227+ this .emptyImgID = emptyImgID
228+ return this
229+ }
230+
206231 fun setEmptyText (@StringRes emptyTextStringRes : Int ): Builder {
207232 this .emptyText = contentLayout?.context?.resources?.getString(emptyTextStringRes)!!
208233 return this
@@ -223,6 +248,11 @@ class StatusLayout {
223248 return this
224249 }
225250
251+ fun setErrorImg (@DrawableRes errorImgID : Int ): Builder {
252+ this .errorImgID = errorImgID
253+ return this
254+ }
255+
226256 fun setErrorText (@StringRes errorTextStringRes : Int ): Builder {
227257 this .errorText = contentLayout?.context?.resources?.getString(errorTextStringRes)!!
228258 return this
0 commit comments