Skip to content

Commit e8dbf7f

Browse files
author
F1ReKing
committed
[优化] 优化布局
1 parent c7384d4 commit e8dbf7f

4 files changed

Lines changed: 43 additions & 10 deletions

File tree

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

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

3+
import android.support.annotation.DrawableRes
34
import android.support.annotation.LayoutRes
45
import android.support.annotation.StringRes
56
import android.text.TextUtils
67
import android.view.LayoutInflater
78
import android.view.View
9+
import android.widget.ImageView
810
import 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

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5+
android:background="@color/white"
56
android:gravity="center"
67
android:orientation="vertical"
7-
android:background="@color/white"
88
>
99

1010
<ImageView
11+
android:id="@+id/iv_status_empty"
1112
android:layout_width="150dp"
1213
android:layout_height="100dp"
1314
android:src="@drawable/ic_no"
@@ -27,14 +28,14 @@
2728
android:layout_width="wrap_content"
2829
android:layout_height="wrap_content"
2930
android:layout_marginTop="20dp"
30-
android:text="刷新"
31-
android:textColor="@color/click"
32-
android:textSize="14sp"
33-
android:paddingTop="5dp"
31+
android:background="@drawable/shape_bg_click"
3432
android:paddingBottom="5dp"
3533
android:paddingLeft="10dp"
3634
android:paddingRight="10dp"
37-
android:background="@drawable/shape_bg_click"
35+
android:paddingTop="5dp"
36+
android:text="刷新"
37+
android:textColor="@color/click"
38+
android:textSize="14sp"
3839
/>
3940

4041
</LinearLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
>
99

1010
<ImageView
11+
android:id="@+id/iv_status_error"
1112
android:layout_width="150dp"
1213
android:layout_height="100dp"
1314
android:src="@drawable/ic_no"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5+
android:background="@color/white"
56
android:gravity="center"
67
android:orientation="vertical"
78
>
@@ -17,8 +18,8 @@
1718
android:layout_height="wrap_content"
1819
android:layout_marginTop="10dp"
1920
android:text="加载中"
20-
android:textSize="16sp"
2121
android:textColor="@color/text"
22+
android:textSize="16sp"
2223
/>
2324

2425
</LinearLayout>

0 commit comments

Comments
 (0)