示意图:
1.添加依赖
-
SwipeRefreshLayout 是一个支持下拉刷新功能的布局,它是 Android Support Library 或 AndroidX 库的一部分。
-
BaseQuickAdapter 是一个来自开源库 BRVAH (Base RecyclerView Adapter Helper) 的功能丰富的适配器,用于简化 RecyclerView 的适配器实现。
dependencies {
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
implementation("com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.6")
}
2.布局文件
在你的布局文件中(例如 activity_main.xml),你需要有一个 RecyclerView 和一个 SwipeRefreshLayout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
3.Activity 代码
在你的 Activity 中,设置下拉刷新和上拉加载更多:
import android.os.Bundle
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
class MainActivity : AppCompatActivity() {
private lateinit var swipeRefreshLayout: SwipeRefreshLayout
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: MyAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout)
recyclerView = findViewById(R.id.recycler_view)
adapter = MyAdapter()
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
swipeRefreshLayout.setOnRefreshListener {
// 处理刷新
Handler().postDelayed({
swipeRefreshLayout.isRefreshing = false
adapter.setNewInstance(listOf("New Item 1", "New Item 2", "New Item 3"))
}, 2000)
}
adapter.loadMoreModule.setOnLoadMoreListener {
// 处理加载更多
Handler().postDelayed({
adapter.addData(listOf("More data 1", "More data 2"))
adapter.loadMoreModule.loadMoreComplete()
}, 2000)
}
// 可选: 自定义LoadMoreView
adapter.loadMoreModule.loadMoreView = CustomLoadMoreView()
}
}
class MyAdapter : BaseQuickAdapter<String, BaseViewHolder>(android.R.layout.simple_list_item_1) {
override fun convert(holder: BaseViewHolder, item: String) {
holder.setText(android.R.id.text1, item)
}
}
4.自定义 LoadMoreView
package com.mofang.call.callphone.view
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.chad.library.adapter.base.loadmore.BaseLoadMoreView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.mofang.call.callphone.R
class CustomLoadMoreView : BaseLoadMoreView() {
override fun getRootView(parent: ViewGroup): View {
return LayoutInflater.from(parent.context).inflate(R.layout.costom_load_more_view, parent, false)
}
override fun getLoadingView(holder: BaseViewHolder): View {
return holder.getView(R.id.loading_v)
}
override fun getLoadComplete(holder: BaseViewHolder): View {
return getLoadEndView(holder)
}
override fun getLoadFailView(holder: BaseViewHolder): View {
val textView = holder.getView<TextView>(R.id.load_more_text)
textView.text = "加载失败,请重试"
return textView
}
override fun getLoadEndView(holder: BaseViewHolder): View {
val textView = holder.getView<TextView>(R.id.load_more_text)
textView.text = "没有更多数据"
return textView
}
}
custom_load_more.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/loading_v"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:indeterminateTint="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:gravity="center_vertical"
android:text="正在加载中..."
android:textColor="#FFFFFFFF"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:id="@+id/load_more_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:gravity="center_vertical"
android:text="正在加载中..."
android:textColor="#FFFFFFFF"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>