Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use payload to achieve Local Refresh in RecyclerView

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report