11package com.f1reking.statuslayout.library
22
33import android.support.annotation.LayoutRes
4+ import android.support.annotation.StringRes
5+ import android.text.TextUtils
46import android.view.LayoutInflater
57import android.view.View
8+ import android.widget.TextView
69
710/* *
811 * @author: F1ReKing
@@ -20,6 +23,10 @@ class StatusLayout {
2023 @LayoutRes private var emptyLayoutID: Int = 0
2124 @LayoutRes private var errorLayoutID: Int = 0
2225
26+ private var loadingText: String = " "
27+ private var emptyText: String = " "
28+ private var errorText: String = " "
29+
2330 private var inflater: LayoutInflater ? = null
2431 private var statusLayoutHelper: StatusLayoutHelper ? = null
2532
@@ -35,20 +42,24 @@ class StatusLayout {
3542
3643 this .loadingLayout = builder.loadingLayout
3744 this .loadingLayoutID = builder.loadingLayoutID
45+ this .loadingText = builder.loadingText
3846
3947 this .emptyLayout = builder.emptyLayout
4048 this .emptyLayoutID = builder.emptyLayoutID
49+ this .emptyText = builder.emptyText
4150
4251 this .errorLayout = builder.errorLayout
4352 this .errorLayoutID = builder.errorLayoutID
53+ this .errorText = builder.errorText
54+
4455 this .statusLayoutHelper = StatusLayoutHelper (contentLayout)
4556 }
4657
4758 /* *
4859 * 显示内容布局
4960 */
5061 fun showContentLayout () {
51- statusLayoutHelper?.defaultLayout ()
62+ statusLayoutHelper?.setContentLayout ()
5263 }
5364
5465 /* *
@@ -62,7 +73,11 @@ class StatusLayout {
6273 private fun createLoadingLayout () {
6374 if (null == loadingLayout) {
6475 loadingLayout = inflater(loadingLayoutID)
65- } // todo 加载布局的控件处理
76+ }
77+ if (! TextUtils .isEmpty(loadingText)) {
78+ val loadingTextView = loadingLayout!! .findViewById<TextView >(R .id.tv_status_loading)
79+ loadingTextView?.text = loadingText
80+ }
6681 }
6782
6883 /* *
@@ -77,8 +92,13 @@ class StatusLayout {
7792 if (null == emptyLayout) {
7893 emptyLayout = inflater(emptyLayoutID)
7994 }
95+ if (! TextUtils .isEmpty(emptyText)) {
96+ val emptyTextView = emptyLayout!! .findViewById<TextView >(R .id.tv_status_empty)
97+ emptyTextView?.text = emptyText
98+ }
8099 }
81100
101+
82102 /* *
83103 * 显示错误布局
84104 */
@@ -91,6 +111,10 @@ class StatusLayout {
91111 if (null == errorLayout) {
92112 errorLayout = inflater(errorLayoutID)
93113 }
114+ if (! TextUtils .isEmpty(errorText)) {
115+ val errorTextView = errorLayout!! .findViewById<TextView >(R .id.tv_status_error)
116+ errorTextView?.text = errorText
117+ }
94118 }
95119
96120 class Builder {
@@ -104,11 +128,16 @@ class StatusLayout {
104128 @LayoutRes var emptyLayoutID: Int = 0
105129 @LayoutRes var errorLayoutID: Int = 0
106130
131+ var loadingText: String = " "
132+ var emptyText: String = " "
133+ var errorText: String = " "
134+
107135 constructor (contentLayout: View ) {
108136 this .contentLayout = contentLayout
109137 this .loadingLayoutID = R .layout.layout_loading
110138 this .emptyLayoutID = R .layout.layout_empty
111- this .errorLayoutID = R .layout.layout_error // todo 处理布局中控件
139+ this .errorLayoutID = R .layout.layout_error
140+
112141 }
113142
114143 fun build (): StatusLayout {
@@ -125,6 +154,16 @@ class StatusLayout {
125154 return this
126155 }
127156
157+ fun setLoadingText (loadingText : String ): Builder {
158+ this .loadingText = loadingText
159+ return this
160+ }
161+
162+ fun setLoadingtext (@StringRes loadingTextStringRes : Int ): Builder {
163+ this .loadingText = contentLayout?.context?.resources?.getString(loadingTextStringRes)!!
164+ return this
165+ }
166+
128167 fun setEmptyLayout (@LayoutRes emptyLayoutID : Int ): Builder {
129168 this .emptyLayoutID = emptyLayoutID
130169 return this
@@ -135,6 +174,16 @@ class StatusLayout {
135174 return this
136175 }
137176
177+ fun setEmptyText (emptyText : String ): Builder {
178+ this .emptyText = emptyText
179+ return this
180+ }
181+
182+ fun setEmptyText (@StringRes emptyTextStringRes : Int ): Builder {
183+ this .emptyText = contentLayout?.context?.resources?.getString(emptyTextStringRes)!!
184+ return this
185+ }
186+
138187 fun setErrorLayout (@LayoutRes errorLayoutID : Int ): Builder {
139188 this .errorLayoutID = errorLayoutID
140189 return this
@@ -144,5 +193,17 @@ class StatusLayout {
144193 this .errorLayout = errorLayout
145194 return this
146195 }
196+
197+ fun setErrorText (errorText : String ): Builder {
198+ this .errorText = errorText
199+ return this
200+ }
201+
202+ fun setErrorText (@StringRes errorTextStringRes : Int ): Builder {
203+ this .errorText = contentLayout?.context?.resources?.getString(errorTextStringRes)!!
204+ return this
205+ }
206+
207+
147208 }
148209}
0 commit comments