RecyclerView 局部刷新
相信現(xiàn)在已經(jīng)很少人不用 RecyclerView 了父叙,如果只有一個(gè) item 的數(shù)據(jù)源改變的時(shí)候理逊,大家都會(huì)用 notifyItemChanged(int position) 而不是 notifyDataSetChanged 棚唆,這就是最簡(jiǎn)單的局部刷新。但是這還不夠,有時(shí)候我們需要只刷新一個(gè) item 的 某條數(shù)據(jù)王污,比較熟悉的例子是微信朋友圈點(diǎn)贊,這時(shí)候怎么辦呢楚午?答案是還是用 notifyItemChanged 方法昭齐,不過(guò)是它的重載方法,notifyItemChanged(int position, @Nullable Object payload)矾柜。
小tips
我們默認(rèn)調(diào)用 notifyItemChanged 會(huì)發(fā)現(xiàn) item 會(huì)閃一下阱驾,這是 RecyclerView 默認(rèn)動(dòng)畫的緣故,由于各種各樣的原因(設(shè)計(jì)師覺得不美觀怪蔑,動(dòng)畫相對(duì)來(lái)說(shuō)耗性能)里覆,我們會(huì)去掉這個(gè)動(dòng)畫,簡(jiǎn)單粗暴的方法是直接設(shè)置 RecyclerView 的默認(rèn)動(dòng)畫為空:recycler.itemAnimator = null
但是 RecyclerView 默認(rèn)的增加饮睬、移除 item 的動(dòng)畫挺好看的租谈,如果我們的需求是只去掉 item 更新的閃動(dòng)動(dòng)畫,可以這樣做:
fun RecyclerView.closeChangeAnim() { if (this.itemAnimator is DefaultItemAnimator) { (this.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false } }
言歸正傳,要想局部刷新成功割去,只調(diào)用 notifyItemChanged(int position, @Nullable Object payload) 是沒有效果的窟却,還要重寫 onBindViewHolder(ViewHolder holder, int position, List<Object> payload)方法。下面是示例代碼:
MyBean
data class MyBean(
val title: String = "",
val content: String = ""
)
MyAdapter
class MyAdapter(val datas: MutableList<MyBean>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return datas.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.titleTv.text = datas[position].title
holder.contentTv.text = datas[position].content
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int, payloads: MutableList<Any>) {
if (payloads.isNullOrEmpty().not()) {
val newTitle = payloads[0].toString()
holder.titleTv.text = newTitle
Log.d(TAG, "onBindViewHolder:payloads=${payloads} ")
} else {
// payloads 為空呻逆,整個(gè)ViewHolder
super.onBindViewHolder(holder, position, payloads)
Log.d(TAG, "onBindViewHolder:payloads=null ")
}
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val titleTv = itemView.findViewById<TextView>(R.id.item_title)
val contentTv = itemView.findViewById<TextView>(R.id.item_content)
}
}
MainActivity
private const val TAG = "MainActivity"
class MainActivity : AppCompatActivity() {
lateinit var recycler: RecyclerView
lateinit var adapter: MyAdapter
lateinit var refreshItemBtn: Button
lateinit var refreshItemTitleBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
private fun init() {
recycler = findViewById(R.id.rv)
recycler.layoutManager = LinearLayoutManager(this)
val datas = mutableListOf<MyBean>().apply {
for (i in 1..50) {
add(MyBean("title$i", "content$i"))
}
}
adapter = MyAdapter(datas)
recycler.adapter = adapter
refreshItemBtn = findViewById(R.id.refresh_item_btn)
refreshItemTitleBtn = findViewById(R.id.refresh_title_btn)
refreshItemBtn.setOnClickListener {
val item = MyBean("New Title","New Content")
adapter.datas[5] = item
adapter.notifyItemChanged(5)
}
refreshItemTitleBtn.setOnClickListener {
// val item = MyBean("New Title","New Content")
// adapter.datas[5] = item
adapter.notifyItemChanged(5,"New Title")
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<Button
android:id="@+id/refresh_item_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/refresh_title_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_title"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:textStyle="bold"
android:text="This is title."
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="8dp"
android:id="@+id/item_content"
android:layout_marginLeft="20dp"
android:text="This is content."
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
以上代碼還是挺簡(jiǎn)單的夸赫,一言蔽之就是把 item 拆分為單獨(dú)的控件更新,更細(xì)粒度出刷新咖城,這也與我們局部刷新的目標(biāo)一致茬腿。
自定義 View 局部刷新
當(dāng)某些 View 比較復(fù)雜層級(jí)比較多時(shí),可以根據(jù)實(shí)際情況用自定義 View 替代宜雀,如果這時(shí)我們需要只刷新部分區(qū)域時(shí)切平,Android 系統(tǒng)也提供了兩個(gè)局部刷新的方法
- invalidate(Rect dirty)
- invalidate(int l, int t, int r, int b)
不過(guò),隨著系統(tǒng)版本的迭代辐董,官方已標(biāo)記這兩個(gè)方法為 @Deprecated悴品,推薦直接用 invalidate() 方法。