Skip to content

Commit 93da532

Browse files
committed
动态页面新增加载失败页面,提升用户体验
1 parent 83508bb commit 93da532

File tree

9 files changed

+77
-16
lines changed

9 files changed

+77
-16
lines changed

app/src/main/java/com/fmt/github/base/fragment/BasePagingVMFragment.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.recyclerview.widget.RecyclerView
88
import com.fmt.github.R
99
import com.fmt.github.base.viewmodel.BaseLPagingModel
1010
import com.fmt.github.ext.yes
11+
import com.google.android.material.button.MaterialButton
1112
import com.kennyc.view.MultiStateView
1213
import com.scwang.smartrefresh.layout.api.RefreshLayout
1314
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
@@ -28,6 +29,12 @@ abstract class BasePagingVMFragment<M, VM : BaseLPagingModel<M>, VH : RecyclerVi
2829
override fun getLayoutRes(): Int = R.layout.common_refresh_recyclerview
2930

3031
override fun initView() {
32+
mMultipleStatusView.viewState = MultiStateView.ViewState.LOADING
33+
mMultipleStatusView.getView(MultiStateView.ViewState.ERROR)
34+
?.findViewById<MaterialButton>(R.id.mb_retry_load)?.setOnClickListener {
35+
mMultipleStatusView.viewState = MultiStateView.ViewState.LOADING
36+
mViewModel.refresh()
37+
}
3138
mRefreshLayout.run {
3239
setOnRefreshListener(this@BasePagingVMFragment)
3340
setOnLoadMoreListener(this@BasePagingVMFragment)
@@ -42,15 +49,20 @@ abstract class BasePagingVMFragment<M, VM : BaseLPagingModel<M>, VH : RecyclerVi
4249
mMultipleStatusView.viewState = MultiStateView.ViewState.CONTENT
4350
}
4451
})
52+
mViewModel.refreshState.observe(this, Observer {
53+
it.yes {
54+
mMultipleStatusView.viewState = MultiStateView.ViewState.ERROR
55+
}
56+
})
4557
mViewModel.loadMoreState.observe(this, Observer {
46-
mRefreshLayout.setEnableLoadMore(it)//上拉加载进度条只有在Paging加载更多失败时才有效(用于规避Paging加载更多失败后,无法再次加载问题)
58+
//上拉加载进度条只有在Paging加载更多失败时才有效(用于规避Paging加载更多失败后,无法再次加载问题)
59+
mRefreshLayout.setEnableLoadMore(it)
4760
})
4861

4962
afterViewCreated()
5063
}
5164

5265
override fun initData() {
53-
mRefreshLayout.autoRefreshAnimationOnly()
5466
mViewModel.pagedList.observe(this, Observer<PagedList<M>> {
5567
mAdapter.submitList(it)
5668
})

app/src/main/java/com/fmt/github/base/viewmodel/BaseLPagingModel.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import kotlinx.coroutines.launch
1515
*/
1616
abstract class BaseLPagingModel<M> : BaseViewModel() {
1717

18-
lateinit var mDataSource: PageKeyedDataSource<Int, M>
18+
private lateinit var mDataSource: PageKeyedDataSource<Int, M>
1919

2020
val mBoundaryData = MutableLiveData(false)//控制页面显示状态
2121

22+
val refreshState = MutableLiveData(false)
23+
2224
val loadMoreState = MutableLiveData(false)
2325

24-
var loadMoreRetry: (() -> Unit)? = null
26+
private var loadMoreRetry: (() -> Unit)? = null
2527

2628
val pagedList: LiveData<PagedList<M>> by lazy {
2729
LivePagedListBuilder<Int, M>(
@@ -43,17 +45,23 @@ abstract class BaseLPagingModel<M> : BaseViewModel() {
4345
params: LoadInitialParams<Int>,
4446
callback: LoadInitialCallback<Int, M>
4547
) {
46-
launch {
47-
val list = getDataList(1)
48-
mBoundaryData.postValue(list.isNotEmpty())
49-
callback.onResult(list, null, 2)
48+
viewModelScope.launch {
49+
try {
50+
val list = getDataList(1)
51+
mBoundaryData.postValue(list.isNotEmpty())
52+
callback.onResult(list, null, 2)
53+
mStateLiveData.value = SuccessState
54+
refreshState.postValue(false)
55+
} catch (e: Exception) {
56+
refreshState.postValue(true)
57+
mStateLiveData.value = ErrorState(e.message)
58+
}
5059
}
5160
}
5261

5362
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, M>) {
5463
viewModelScope.launch {
5564
try {
56-
mStateLiveData.value = LoadState
5765
val list = getDataList(params.key)
5866
callback.onResult(
5967
list,
@@ -65,7 +73,7 @@ abstract class BaseLPagingModel<M> : BaseViewModel() {
6573
mStateLiveData.value = ErrorState(e.message)
6674
loadMoreState.postValue(true)
6775
loadMoreRetry = {
68-
//保存加载更多失败时的场景
76+
//保存加载更多失败时的场景,防止第一次加载失败后,后续无法再次调用loadAfter
6977
loadMoreFail(params, callback)
7078
}
7179
}
@@ -83,7 +91,7 @@ abstract class BaseLPagingModel<M> : BaseViewModel() {
8391
loadMoreRetry?.invoke()
8492
}
8593

86-
fun loadMoreFail(//加载更多失败时调用
94+
private fun loadMoreFail(//加载更多失败时调用
8795
params: PageKeyedDataSource.LoadParams<Int>,
8896
callback: PageKeyedDataSource.LoadCallback<Int, M>
8997
) {

app/src/main/res/layout/activity_user_info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
android:id="@+id/mTabLayout"
7070
android:layout_width="match_parent"
7171
android:layout_height="wrap_content"
72+
app:tabBackground="@color/colorPrimary"
7273
app:tabTextColor="@android:color/white"
7374
app:tabIndicatorColor="@android:color/white"
7475
app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"/>

app/src/main/res/layout/common_refresh_recyclerview.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
android:layout_width="match_parent"
1212
android:layout_height="match_parent"
1313
app:msv_emptyView="@layout/layout_empty"
14+
app:msv_loadingView="@layout/layout_loading"
15+
app:msv_errorView="@layout/layout_refresh_retyr"
1416
app:msv_viewState="empty">
1517

1618
<androidx.recyclerview.widget.RecyclerView
1719
android:id="@+id/mRecyclerView"
1820
android:layout_width="match_parent"
19-
android:layout_height="match_parent"
20-
/>
21+
android:layout_height="match_parent" />
2122
</com.kennyc.view.MultiStateView>
2223

2324
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="wrap_content"
4+
android:layout_height="wrap_content"
5+
android:layout_gravity="center"
6+
android:orientation="vertical">
7+
8+
</ProgressBar>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
xmlns:app="http://schemas.android.com/apk/res-auto"
6+
android:orientation="vertical"
7+
android:gravity="center">
8+
9+
<androidx.appcompat.widget.AppCompatImageView
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:src="@mipmap/icon_net_error"/>
13+
14+
<com.google.android.material.button.MaterialButton
15+
android:id="@+id/mb_retry_load"
16+
style="?android:attr/borderlessButtonStyle"
17+
android:layout_width="@dimen/dp_86"
18+
android:layout_height="@dimen/dp_31"
19+
android:layout_marginTop="@dimen/dp_18"
20+
android:insetTop="@dimen/dp_0"
21+
android:insetBottom="@dimen/dp_0"
22+
android:padding="@dimen/dp_0"
23+
android:text="@string/retry_load"
24+
android:textAllCaps="false"
25+
android:textColor="@android:color/white"
26+
android:textSize="@dimen/sp_14"
27+
app:cornerRadius="@dimen/dp_16"
28+
app:backgroundTint="@color/colorPrimary"/>
29+
30+
</LinearLayout>
5.83 KB
Loading

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<string name="profile">Profile</string>
6060
<string name="about">About</string>
6161
<string name="logout">Logout</string>
62+
<string name="retry_load">retry load</string>
6263

6364
<string-array name="github_glyph_strings">
6465
<item>M256 70.7c-102.6 0-185.9 83.2-185.9 185.9 0 82.1 53.3 151.8 127.1 176.4 9.3 1.7 12.3-4

app/src/main/res/values/styles.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<resources>
22

33
<!-- Base application theme. -->
4-
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
4+
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
55
<!-- Customize your theme here. -->
66
<item name="colorPrimary">@color/colorPrimary</item>
77
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
@@ -11,7 +11,7 @@
1111
<item name="android:windowBackground">@null</item>
1212
</style>
1313

14-
<style name="ToolBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
14+
<style name="ToolBarTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
1515
<item name="colorPrimary">@color/colorPrimary</item>
1616
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
1717
<!--右侧菜单按钮样式-->
@@ -29,7 +29,7 @@
2929
<item name="android:windowIsTranslucent">true</item>
3030
</style>
3131

32-
<style name="FullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
32+
<style name="FullScreenTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
3333
<item name="android:windowFullscreen">true</item>
3434
</style>
3535

0 commit comments

Comments
 (0)