Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use payload to achieve Local Refresh in RecyclerView

Shulou Source: shulou.com Published: 2022-06-03 07:58:28 09月14日 Update

Editor to share with you how RecyclerView uses payload to achieve local refresh. I hope you will get something after reading this article. Let's discuss it together.

List local refresh:

01.notifyDataSetChanged () refreshes all visible item

02.notifyItemChanged (int position) the data on the update list position location can be called

03.notifyItemInserted (int position) list position location can be called when adding a piece of data, with animation effects

Called when the 04.notifyItemRemoved (int position) list position position removes a piece of data, with an animation effect

Called when the data of the fromPosition location of the 05.notifyItemMoved (int fromPosition, int toPosition) list is moved to the toPosition location, with animation effects

The 06.notifyItemRangeChanged (int positionStart, int itemCount) list refreshes the data from the positionStart location to the number of itemCount list items

07.notifyItemRangeInserted (int positionStart, int itemCount) list is called when data is added in batches from the positionStart location to the number of itemCount list items, with animation effects

Called when the 08.notifyItemRangeRemoved (int positionStart, int itemCount) list is deleted in batches from the positionStart location to the itemCount number of list items, with animation effects

1. Payload and notifyItemChanged () to achieve local refresh:

1. Define the onBindViewHolder (holder: ViewHolder, position: Int, payloads: MutableList) method in the adapter:

Class NewsAdapter: ListAdapter (Diff ()) {/ / data comparison result of building ListView class Diff: DiffUtil.ItemCallback () {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.TV Play2)} override fun onCreateViewHolder (parent: ViewGroup ViewType: Int): ViewHolder {val view = LayoutInflater.from (parent.context) .propagate (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) {if (payloads.isEmpty ()) {onBindViewHolder (holder) Position)} else {for (I in 0 until payloads.size) {when (payloads [I] .toString ()) {"aaa"-> {holder.tvContent.text = "000,000"} "bbb"-> { Holder.tvPlay.text = "222"}}

two。 Use notifyItemChanged () for a local refresh:

Class MainActivity: AppCompatActivity () {private lateinit var recycler: RecyclerView private lateinit var mAdapter: NewsAdapter val data = listOf (Data), Data (456, 456, 1, 456), Data (789, 789, 1 ) 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 (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:

4. List Item layout file:

After reading this article, I believe you have a certain understanding of "how RecyclerView uses payload to achieve partial refresh". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: Location data part animation effects tone quantity layout document article finished method more knowledge result industry information information channel adapter channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info OPPO Reno Shulou Information Apple