RecyclerView使用payload實現(xiàn)局部刷新
本文實例為大家分享了RecyclerView使用payload實現(xiàn)局部刷新的具體代碼,供大家參考,具體內(nèi)容如下
列表局部刷新:
01.notifyDataSetChanged() 刷新全部可見的item
02.notifyItemChanged(int position) 更新列表position位置上的數(shù)據(jù)可以調(diào)用
03.notifyItemInserted(int position) 列表position位置添加一條數(shù)據(jù)時可以調(diào)用,伴有動畫效果
04.notifyItemRemoved(int position) 列表position位置移除一條數(shù)據(jù)時調(diào)用,伴有動畫效果
05.notifyItemMoved(int fromPosition, int toPosition) 列表fromPosition位置的數(shù)據(jù)移到toPosition位置時調(diào)用,伴有動畫效果
06.notifyItemRangeChanged(int positionStart, int itemCount) 列表從positionStart位置到itemCount數(shù)量的列表項進行數(shù)據(jù)刷新
07.notifyItemRangeInserted(int positionStart, int itemCount) 列表從positionStart位置到itemCount數(shù)量的列表項批量添加數(shù)據(jù)時調(diào)用,伴有動畫效果
08.notifyItemRangeRemoved(int positionStart, int itemCount) 列表從positionStart位置到itemCount數(shù)量的列表項批量刪除數(shù)據(jù)時調(diào)用,伴有動畫效果
一、payload、notifyItemChanged()實現(xiàn)局部刷新:
1.在適配器中定義onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList)方法:
class NewsAdapter : ListAdapter<Data, NewsAdapter.ViewHolder>(Diff()) {
//構(gòu)建ListView的數(shù)據(jù)比較結(jié)果
class Diff : DiffUtil.ItemCallback<Data>() {
override fun areItemsTheSame(oldItem: Data, newItem: Data): Boolean {
return oldItem.hashId == newItem.hashId
}
override fun areContentsTheSame(oldItem: Data, newItem: Data): Boolean {
return oldItem.content == newItem.content
}
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val tvContent: TextView = view.findViewById(R.id.tvContent)
var tvPlay: TextView = view.findViewById(R.id.tvPlay)
var tvPlay1: TextView = view.findViewById(R.id.tvPlay1)
var tvPlay2: TextView = view.findViewById(R.id.tvPlay2)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_joke_list_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.tvContent.text = getItem(position).content
holder.tvPlay.text = "播放"
holder.tvPlay1.text = "播放"
holder.tvPlay2.text = "播放"
}
//局部刷新Item
override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
if(payloads.isEmpty()) {
onBindViewHolder(holder, position)
} else {
for (i in 0 until payloads.size) {
when(payloads[i].toString()) {
"aaa" -> {
holder.tvContent.text = "000"
}
"bbb" -> {
holder.tvPlay.text = "222"
}
}
}
}
}
}
2.使用notifyItemChanged()進行局部刷新:
class MainActivity : AppCompatActivity() {
private lateinit var recycler: RecyclerView
private lateinit var mAdapter: NewsAdapter
val data = listOf(
Data("123", "123", 1, "123"),
Data("456", "456", 1, "456"),
Data("789", "789", 1, "789")
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recycler = findViewById(R.id.recycler)
mAdapter = NewsAdapter()
val layoutManager = LinearLayoutManager(this)
recycler.layoutManager = layoutManager
recycler.adapter = mAdapter
mAdapter.submitList(data)
//點擊局部刷新
findViewById<Button>(R.id.btn).setOnClickListener {
mAdapter.notifyItemChanged(2, "aaa")
mAdapter.notifyItemChanged(0, "aaa")
mAdapter.notifyItemChanged(1, "aaa")
mAdapter.notifyItemChanged(2, "bbb")
mAdapter.notifyItemChanged(0, "bbb")
mAdapter.notifyItemChanged(1, "bbb")
}
}
}
3.MainActivity布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:text="局部刷新"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
4.列表Item布局文件:
<?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:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/tvPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text="哈哈" />
<TextView
android:id="@+id/tvPlay1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text="哈哈" />
<TextView
android:id="@+id/tvPlay2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text="哈哈" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Retrofit網(wǎng)絡(luò)請求和響應(yīng)處理重點分析講解
這篇文章主要介紹了Retrofit網(wǎng)絡(luò)請求和響應(yīng)處理重點分析,在使用?Retrofit發(fā)起網(wǎng)絡(luò)請求時,我們可以通過定義一個接口并使用Retrofit的注解來描述這個接口中的請求,Retrofit會自動生成一個實現(xiàn)該接口的代理對象2023-03-03
Android Studio gradle配置packagingOptions打包so庫重復
這篇文章主要為大家介紹了Android Studio gradle配置packagingOptions打包so庫重復問題的解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android handle-message的發(fā)送與處理案例詳解
這篇文章主要介紹了Android handle-message的發(fā)送與處理案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
Mac 下 Android Studio 不打印日志的解決辦法
這篇文章主要介紹了Mac 下 Android Studio 不打印日志的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
Flutter 自定義Drawer 滑出位置的大小實例代碼詳解
這篇文章主要介紹了Flutter 自定義Drawer 滑出位置的大小,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android App開發(fā)中自定義View和ViewGroup的實例教程
這篇文章主要介紹了Android App開發(fā)中自定義View和ViewGroup的實例教程,分別介紹了進度條和圖片上傳并排列的例子,效果很好很強大,需要的朋友可以參考下2016-05-05

