Skip to content

Commit a3750fe

Browse files
author
F1ReKing
committed
[优化] 新增了修改布局TextView
1 parent 13f5fcc commit a3750fe

7 files changed

Lines changed: 94 additions & 12 deletions

File tree

library/src/main/java/com/f1reking/statuslayout/library/StatusLayout.kt

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.f1reking.statuslayout.library
22

33
import android.support.annotation.LayoutRes
4+
import android.support.annotation.StringRes
5+
import android.text.TextUtils
46
import android.view.LayoutInflater
57
import 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
}

library/src/main/java/com/f1reking/statuslayout/library/StatusLayoutHelper.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,15 @@ class StatusLayoutHelper {
5050
if (currentLayout != view) {
5151
currentLayout = view
5252
val parent = view.parent as? ViewGroup
53-
if (parent != null) {
54-
parent.removeView(view)
55-
}
53+
parent?.removeView(view)
5654
parentLayout?.removeViewAt(viewIndex!!)
5755
parentLayout?.addView(view, viewIndex!!, params)
5856
return true
5957
}
6058
return false
6159
}
6260

63-
fun defaultLayout(): Boolean {
61+
fun setContentLayout(): Boolean {
6462
return showStatusLayout(contentLayout!!)
6563
}
6664
}

library/src/main/res/layout/layout_empty.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
>
88

99
<TextView
10-
android:id="@+id/tv_empty"
10+
android:id="@+id/tv_status_empty"
1111
android:layout_width="wrap_content"
1212
android:layout_height="wrap_content"
1313
android:text="空数据"

library/src/main/res/layout/layout_error.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
>
88

99
<TextView
10-
android:id="@+id/tv_empty"
10+
android:id="@+id/tv_status_error"
1111
android:layout_width="wrap_content"
1212
android:layout_height="wrap_content"
1313
android:text="出错了"

library/src/main/res/layout/layout_loading.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/>
1313

1414
<TextView
15+
android:id="@+id/tv_status_loading"
1516
android:layout_width="wrap_content"
1617
android:layout_height="wrap_content"
1718
android:layout_marginTop="10dp"

sample/src/main/java/com/f1reking/statuslayout/MainActivity.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ class MainActivity : AppCompatActivity() {
1818
}
1919

2020
private fun initView() {
21-
statusLayout = StatusLayout.Builder(tv_content).build()
21+
statusLayout = StatusLayout.Builder(tv_content)
22+
.setLoadingText("加载中...")
23+
.setEmptyText("空数据了...")
24+
.setErrorText("错误了...")
25+
.build()
2226
}
2327

2428
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
@@ -36,11 +40,11 @@ class MainActivity : AppCompatActivity() {
3640
statusLayout.showLoadingLayout()
3741
return true
3842
}
39-
R.id.menu_empty -> {
43+
R.id.menu_empty -> {
4044
statusLayout.showEmptyLayout()
4145
return true
4246
}
43-
R.id.menu_error -> {
47+
R.id.menu_error -> {
4448
statusLayout.showErrorLayout()
4549
return true
4650
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical"
6+
android:gravity="center"
7+
>
8+
9+
<ProgressBar
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
/>
13+
<TextView
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content"
16+
android:text="dawdwadwdwadawdwa"
17+
/>
18+
</LinearLayout>

0 commit comments

Comments
 (0)