Skip to content

Commit c7384d4

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

10 files changed

Lines changed: 130 additions & 12 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.f1reking.statuslayout.library
2+
3+
import android.view.View
4+
5+
/**
6+
* @author: F1ReKing
7+
* @date: 2018/1/29 16:57
8+
* @desc:
9+
*/
10+
interface StatusClickListener {
11+
12+
/**
13+
* 空数据布局点击方法
14+
* @param view 被点击的view
15+
*/
16+
fun onEmptyClick(view: View)
17+
18+
/**
19+
* 错误数据布局点击方法
20+
* @param view 被点击的view
21+
*/
22+
fun onErrorClick(view: View)
23+
24+
}

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class StatusLayout {
2929

3030
private var inflater: LayoutInflater? = null
3131
private var statusLayoutHelper: StatusLayoutHelper? = null
32+
private var statusClickListener: StatusClickListener? = null
3233

3334
private fun inflater(@LayoutRes resource: Int): View {
3435
if (null == inflater) {
@@ -52,6 +53,7 @@ class StatusLayout {
5253
this.errorLayoutID = builder.errorLayoutID
5354
this.errorText = builder.errorText
5455

56+
this.statusClickListener = builder.statusClickListener
5557
this.statusLayoutHelper = StatusLayoutHelper(contentLayout)
5658
}
5759

@@ -96,8 +98,18 @@ class StatusLayout {
9698
val emptyTextView = emptyLayout!!.findViewById<TextView>(R.id.tv_status_empty)
9799
emptyTextView?.text = emptyText
98100
}
99-
}
101+
if (statusClickListener == null) { //防止出错
102+
return
103+
}
100104

105+
val view = emptyLayout!!.findViewById<TextView>(R.id.tv_click_empty)
106+
if (null == view) { //防止自定义布局ID出错
107+
return
108+
}
109+
view.setOnClickListener {
110+
statusClickListener!!.onEmptyClick(it)
111+
}
112+
}
101113

102114
/**
103115
* 显示错误布局
@@ -115,6 +127,17 @@ class StatusLayout {
115127
val errorTextView = errorLayout!!.findViewById<TextView>(R.id.tv_status_error)
116128
errorTextView?.text = errorText
117129
}
130+
if (statusClickListener == null) { //防止出错
131+
return
132+
}
133+
134+
val view = errorLayout!!.findViewById<TextView>(R.id.tv_click_error)
135+
if (null == view) { //防止自定义布局ID出错
136+
return
137+
}
138+
view.setOnClickListener {
139+
statusClickListener!!.onErrorClick(it)
140+
}
118141
}
119142

120143
class Builder {
@@ -132,12 +155,13 @@ class StatusLayout {
132155
var emptyText: String = ""
133156
var errorText: String = ""
134157

158+
lateinit var statusClickListener: StatusClickListener
159+
135160
constructor(contentLayout: View) {
136161
this.contentLayout = contentLayout
137162
this.loadingLayoutID = R.layout.layout_loading
138163
this.emptyLayoutID = R.layout.layout_empty
139164
this.errorLayoutID = R.layout.layout_error
140-
141165
}
142166

143167
fun build(): StatusLayout {
@@ -204,6 +228,9 @@ class StatusLayout {
204228
return this
205229
}
206230

207-
231+
fun setStatusClickListener(listener: StatusClickListener): Builder {
232+
this.statusClickListener = listener
233+
return this
234+
}
208235
}
209236
}
18.2 KB
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<stroke android:color="@color/click" android:width="1dp"/>
5+
<corners android:radius="20dp"/>
6+
</shape>

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,37 @@
44
android:layout_height="match_parent"
55
android:gravity="center"
66
android:orientation="vertical"
7+
android:background="@color/white"
78
>
89

10+
<ImageView
11+
android:layout_width="150dp"
12+
android:layout_height="100dp"
13+
android:src="@drawable/ic_no"
14+
/>
15+
916
<TextView
1017
android:id="@+id/tv_status_empty"
1118
android:layout_width="wrap_content"
1219
android:layout_height="wrap_content"
13-
android:text="空数据"
20+
android:text="数据为空"
21+
android:textColor="@color/title"
22+
android:textSize="16sp"
1423
/>
1524

16-
<Button
17-
android:id="@+id/btn_refresh"
25+
<TextView
26+
android:id="@+id/tv_click_empty"
1827
android:layout_width="wrap_content"
1928
android:layout_height="wrap_content"
20-
android:layout_marginTop="10dp"
21-
android:text="重新加载"
29+
android:layout_marginTop="20dp"
30+
android:text="刷新"
31+
android:textColor="@color/click"
32+
android:textSize="14sp"
33+
android:paddingTop="5dp"
34+
android:paddingBottom="5dp"
35+
android:paddingLeft="10dp"
36+
android:paddingRight="10dp"
37+
android:background="@drawable/shape_bg_click"
2238
/>
2339

2440
</LinearLayout>

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,36 @@
44
android:layout_height="match_parent"
55
android:gravity="center"
66
android:orientation="vertical"
7+
android:background="@color/white"
78
>
89

10+
<ImageView
11+
android:layout_width="150dp"
12+
android:layout_height="100dp"
13+
android:src="@drawable/ic_no"
14+
/>
15+
916
<TextView
1017
android:id="@+id/tv_status_error"
1118
android:layout_width="wrap_content"
1219
android:layout_height="wrap_content"
13-
android:text="出错了"
20+
android:text="发生错误,请重试"
21+
android:textSize="16sp"
22+
android:textColor="@color/title"
1423
/>
1524

16-
<Button
17-
android:id="@+id/btn_refresh"
25+
<TextView
26+
android:id="@+id/tv_click_error"
1827
android:layout_width="wrap_content"
1928
android:layout_height="wrap_content"
20-
android:layout_marginTop="10dp"
29+
android:layout_marginTop="20dp"
2130
android:text="重新加载"
31+
android:textSize="14sp"
32+
android:textColor="@color/click"
33+
android:paddingTop="5dp"
34+
android:paddingBottom="5dp"
35+
android:paddingLeft="10dp"
36+
android:paddingRight="10dp"
37+
android:background="@drawable/shape_bg_click"
2238
/>
2339
</LinearLayout>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
android:layout_height="wrap_content"
1818
android:layout_marginTop="10dp"
1919
android:text="加载中"
20+
android:textSize="16sp"
21+
android:textColor="@color/text"
2022
/>
2123

2224
</LinearLayout>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<color name="white">#ffffff</color>
5+
<color name="title">#333333</color>
6+
<color name="text">#888888</color>
7+
8+
<color name="click">#FF4081</color>
9+
10+
</resources>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import android.os.Bundle
44
import android.support.v7.app.AppCompatActivity
55
import android.view.Menu
66
import android.view.MenuItem
7+
import android.view.View
8+
import android.widget.Toast
9+
import com.f1reking.statuslayout.library.StatusClickListener
710
import com.f1reking.statuslayout.library.StatusLayout
811
import kotlinx.android.synthetic.main.activity_main.tv_content
912

@@ -22,6 +25,17 @@ class MainActivity : AppCompatActivity() {
2225
.setLoadingText("加载中...")
2326
.setEmptyText("空数据了...")
2427
.setErrorText("错误了...")
28+
.setStatusClickListener(object :StatusClickListener{
29+
override fun onEmptyClick(view: View) {
30+
println("数据空")
31+
Toast.makeText(this@MainActivity,"数据空",Toast.LENGTH_SHORT).show()
32+
}
33+
34+
override fun onErrorClick(view: View) {
35+
println("错误")
36+
Toast.makeText(this@MainActivity,"错误",Toast.LENGTH_SHORT).show()
37+
}
38+
})
2539
.build()
2640
}
2741

sample/src/main/res/values/colors.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88
<color name="colorPrimaryDark">#000000</color>
99
<color name="colorAccent">#455A64</color>
1010

11+
<color name="white">#ffffff</color>
12+
<color name="title">#333333</color>
13+
<color name="text">#888888</color>
1114
</resources>

0 commit comments

Comments
 (0)