温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

RecyclerView如何使用payload实现局部刷新

发布时间:2021-10-08 09:41:42 来源:亿速云 阅读:170 作者:小新 栏目:开发技术

小编给大家分享一下RecyclerView如何使用payload实现局部刷新,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

列表局部刷新:

01.notifyDataSetChanged() 刷新全部可见的item
02.notifyItemChanged(int position) 更新列表position位置上的数据可以调用
03.notifyItemInserted(int position) 列表position位置添加一条数据时可以调用,伴有动画效果
04.notifyItemRemoved(int position) 列表position位置移除一条数据时调用,伴有动画效果
05.notifyItemMoved(int fromPosition, int toPosition) 列表fromPosition位置的数据移到toPosition位置时调用,伴有动画效果
06.notifyItemRangeChanged(int positionStart, int itemCount) 列表从positionStart位置到itemCount数量的列表项进行数据刷新
07.notifyItemRangeInserted(int positionStart, int itemCount) 列表从positionStart位置到itemCount数量的列表项批量添加数据时调用,伴有动画效果
08.notifyItemRangeRemoved(int positionStart, int itemCount) 列表从positionStart位置到itemCount数量的列表项批量删除数据时调用,伴有动画效果

一、payload、notifyItemChanged()实现局部刷新:

1.在适配器中定义onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList)方法:

class NewsAdapter : ListAdapter<Data, NewsAdapter.ViewHolder>(Diff()) {

    //构建ListView的数据比较结果
    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>

看完了这篇文章,相信你对“RecyclerView如何使用payload实现局部刷新”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI