Android kotlin 使用BaseQuickAdapter 加载更多数据 页面VIew全刷新问题解决详细代码
在使用BaseQuickAdapter加载更多数据时,页面View全刷新的问题可以通过以下详细代码解决:
- 在Activity或Fragment中添加以下代码:
private lateinit var mAdapter: BaseQuickAdapter<String, BaseViewHolder>
private var mDataList: MutableList<String> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mAdapter = object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_data, mDataList) {
override fun convert(holder: BaseViewHolder, item: String) {
// 绑定数据
holder.setText(R.id.tv_data, item)
}
}
// 设置加载更多回调
mAdapter.setOnLoadMoreListener(object : BaseQuickAdapter.RequestLoadMoreListener {
override fun onLoadMoreRequested() {
// 加载更多数据
loadMoreData()
}
}, rv_data)
// 设置加载更多时的样式
mAdapter.setLoadMoreView(CustomLoadMoreView())
// 设置动画效果
mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN)
// 设置RecyclerView布局管理器和适配器
rv_data.layoutManager = LinearLayoutManager(this)
rv_data.adapter = mAdapter
// 初始化数据
initData()
}
private fun initData() {
// 模拟初始数据
mDataList.add("Data 1")
mDataList.add("Data 2")
mDataList.add("Data 3")
mDataList.add("Data 4")
mDataList.add("Data 5")
mDataList.add("Data 6")
mAdapter.notifyDataSetChanged()
}
private fun loadMoreData() {
// 模拟加载更多数据
mDataList.add("More Data 1")
mDataList.add("More Data 2")
mDataList.add("More Data 3")
mDataList.add("More Data 4")
mDataList.add("More Data 5")
// 刷新适配器
mAdapter.notifyDataSetChanged()
// 加载完成
mAdapter.loadMoreComplete()
}
- 创建自定义的加载更多视图类
CustomLoadMoreView,继承自BaseLoadMoreView,并实现相应的方法:
class CustomLoadMoreView : BaseLoadMoreView() {
override fun getRootView(parent: ViewGroup): View {
// 返回自定义的加载更多视图布局
return LayoutInflater.from(parent.context).inflate(R.layout.view_custom_load_more, parent, false)
}
override fun getLoadingView(holder: BaseViewHolder): View {
// 返回加载中的视图
return holder.getView(R.id.progressbar_loading)
}
override fun getLoadEndView(holder: BaseViewHolder): View {
// 返回加载完成的视图
return holder.getView(R.id.tv_load_end)
}
override fun getLoadFailView(holder: BaseViewHolder): View {
// 返回加载失败的视图
return holder.getView(R.id.tv_load_fail)
}
}
- 创建自定义的加载更多视图布局文件
view_custom_load_more.xml,可以根据需要进行定制:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<ProgressBar
android:id="@+id/progressbar_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
<TextView
android:id="@+id/tv_load_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已加载全部数据"
android:visibility="gone" />
<TextView
android:id="@+id/tv_load_fail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载失败,点击重试"
android:visibility="gone" />
</LinearLayout>
以上是使用BaseQuickAdapter加载更多数据时解决页面View全刷新问题的详细代码。在加载更多数据时,通过notifyDataSetChanged()方法刷新适配器,然后调用loadMoreComplete()方法表示加载完成。自定义的加载更多视图可以根据需要进行样式定制
原文地址: https://www.cveoy.top/t/topic/iNi4 著作权归作者所有。请勿转载和采集!