Skip to content

Commit

Permalink
功能实现:新增“加载全部内容”场景
Browse files Browse the repository at this point in the history
  • Loading branch information
lelelongwang committed Jul 9, 2021
1 parent 01432f3 commit 94cf757
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 36 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ WanJetpack
- [AndroidSwipeLayout](https://github.com/daimajia/AndroidSwipeLayout)
- [SwipeRecyclerView](https://github.com/yanzhenjie/SwipeRecyclerView)
- [Android 简单易上手的下拉刷新控件](https://www.jianshu.com/p/459e611c0f62)
- 加载状态的几个场景:下拉刷新、上拉加载更多、底部的已加载全部内容、首次进入页面的加载状态(及加载失败提醒)
- 下拉刷新、上拉加载更多:略
- 首次进入页面的加载状态:
- 底部的已加载全部内容:方案比较多,个人比较倾向下面两种方案
- 方案一:通过withLoadStateFooter实现,和上拉加载更多用同一套布局,同一个adapter。【参考本demo】
- 方案二:通过ConcatAdapter.addAdapter实现,专门显示加载更多

- Animation 动画: 下拉刷新场景通过属性动画实现
- [官方文档](https://developer.android.google.cn/training/animation)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.longjunhao.wanjetpack.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
Expand All @@ -23,43 +19,60 @@ class FooterAdapter(
private val retry: () -> Unit
) : LoadStateAdapter<FooterViewHolder>() {

override fun onCreateViewHolder(
parent: ViewGroup,
loadState: LoadState
) = FooterViewHolder(parent, retry)
override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState) =
FooterViewHolder(
ListItemFooterBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
),
retry
)

override fun onBindViewHolder(
holder: FooterViewHolder,
loadState: LoadState
) = holder.bind(loadState)
override fun onBindViewHolder(holder: FooterViewHolder, loadState: LoadState) {
holder.bind(loadState)
}

/**
* 1. 如果直接返回true的话,首次进入界面加载时,会执行两次网络请求,且RecyclerView会向下滚动一段距离。
* 所以需要加上loadState.endOfPaginationReached= true的条件。
*
* 2. 注意is的优先级比||、 &&的优先级高,所以:
* a is b && c 是 (a is b) && c 的意思
* a is b || c 是 (a is b) || c 的意思
*
*/
override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
return loadState is LoadState.Loading
|| loadState is LoadState.Error
|| (loadState is LoadState.NotLoading && loadState.endOfPaginationReached)
}
}

class FooterViewHolder(
parent: ViewGroup,
val binding: ListItemFooterBinding,
retry: () -> Unit
) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.list_item_footer, parent, false)
) {
private val binding = ListItemFooterBinding.bind(itemView)
private val progressBar: ProgressBar = binding.progressBar
private val errorMsg: TextView = binding.errorMsg
private val retry: Button = binding.retryButton
.also {
it.setOnClickListener { retry() }
) : RecyclerView.ViewHolder(binding.root) {

init {
binding.retry.setOnClickListener {
retry()
}
}

/**
* todo 遗留问题:当所有数据都加载后,应该在FooterView上显示“已经到底了”之类的。
*/
fun bind(loadState: LoadState) {
Log.d("FooterAdapter", "bind: ljh loadState=$loadState")
if (loadState is LoadState.Error) {
errorMsg.text = loadState.error.localizedMessage
binding.apply {

if (loadState is LoadState.Error) {
msg.text = loadState.error.localizedMessage
} else if (loadState.endOfPaginationReached) {
msg.text = binding.root.resources.getString(R.string.load_end)
}

progressBar.isVisible = loadState is LoadState.Loading
retry.isVisible = loadState is LoadState.Error
msg.isVisible = loadState is LoadState.Error || loadState.endOfPaginationReached
}

progressBar.isVisible = loadState is LoadState.Loading
retry.isVisible = loadState is LoadState.Error
errorMsg.isVisible = loadState is LoadState.Error
}
}
9 changes: 5 additions & 4 deletions app/src/main/res/layout/list_item_footer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
android:orientation="vertical"
android:padding="8dp">

<TextView
android:id="@+id/error_msg"
<com.google.android.material.textview.MaterialTextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceListItem"
android:layout_gravity="center_horizontal" />

<ProgressBar
Expand All @@ -25,8 +26,8 @@
android:indeterminateTint="?attr/colorPrimary"
android:layout_gravity="center" />

<Button
android:id="@+id/retry_button"
<com.google.android.material.button.MaterialButton
android:id="@+id/retry"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<string name="register_fail">注册错误,请重重新输入</string>
<string name="logout">退出登录</string>
<string name="load_error">加载失败,请点击重试</string>
<string name="load_end">已加载全部内容…</string>
<string name="no_net">请检查网络是否连接…</string>
</resources>

0 comments on commit 94cf757

Please sign in to comment.