RecyclerView uses payload for local refresh
- 2021-12-21 04:58:15
- OfStack
In this paper, we share the specific code of RecyclerView using payload to realize local refresh for your reference. The specific contents are as follows
List partial refresh:
01. notifyDataSetChanged () refreshes all visible item
02. notifyItemChanged (int position) Update list data at position location can be called
03. notifyItemInserted (int position) can be called when one piece of data is added to the position of position in the list, accompanied by animation effect
04. notifyItemRemoved (int position) Called when 1 piece of data is removed from the position position of the list, with animation effect
05. notifyItemMoved (int fromPosition, int toPosition) is invoked when the data in the fromPosition position is moved to the toPosition position with animation effect
06. notifyItemRangeChanged (int positionStart, int itemCount) list items from positionStart location to itemCount number are refreshed
07. notifyItemRangeInserted (int positionStart, int itemCount) list is called when batch data is added from the position of positionStart to the number of list items of itemCount, accompanied by animation effect
08. notifyItemRangeRemoved (int positionStart, int itemCount) list is called when the list items from positionStart position to itemCount number are deleted in batches, accompanied by animation effect
1. payload and notifyItemChanged () implement local refresh:
1. Define the onBindViewHolder (holder: ViewHolder, position: Int, payloads: MutableList) method in the adapter:
class NewsAdapter : ListAdapter<Data, NewsAdapter.ViewHolder>(Diff()) {
// Build ListView Data comparison results of
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 = " Play "
holder.tvPlay1.text = " Play "
holder.tvPlay2.text = " Play "
}
// Local refresh 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. Use notifyItemChanged () for local refresh:
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)
// Click Local Refresh
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 layout file:
<?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=" Local refresh "
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. List Item layout files:
<?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=" Ha ha " />
<TextView
android:id="@+id/tvPlay1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text=" Ha ha " />
<TextView
android:id="@+id/tvPlay2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text=" Ha ha " />
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>