欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

RecyclerView使用payload實(shí)現(xiàn)局部刷新

 更新時(shí)間:2021年10月03日 16:28:19   作者:獨(dú)沽一味的豬  
這篇文章主要為大家詳細(xì)介紹了RecyclerView使用payload實(shí)現(xiàn)局部刷新,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了RecyclerView使用payload實(shí)現(xiàn)局部刷新的具體代碼,供大家參考,具體內(nèi)容如下

列表局部刷新:

01.notifyDataSetChanged() 刷新全部可見的item
02.notifyItemChanged(int position) 更新列表position位置上的數(shù)據(jù)可以調(diào)用
03.notifyItemInserted(int position) 列表position位置添加一條數(shù)據(jù)時(shí)可以調(diào)用,伴有動(dòng)畫效果
04.notifyItemRemoved(int position) 列表position位置移除一條數(shù)據(jù)時(shí)調(diào)用,伴有動(dòng)畫效果
05.notifyItemMoved(int fromPosition, int toPosition) 列表fromPosition位置的數(shù)據(jù)移到toPosition位置時(shí)調(diào)用,伴有動(dòng)畫效果
06.notifyItemRangeChanged(int positionStart, int itemCount) 列表從positionStart位置到itemCount數(shù)量的列表項(xiàng)進(jìn)行數(shù)據(jù)刷新
07.notifyItemRangeInserted(int positionStart, int itemCount) 列表從positionStart位置到itemCount數(shù)量的列表項(xiàng)批量添加數(shù)據(jù)時(shí)調(diào)用,伴有動(dòng)畫效果
08.notifyItemRangeRemoved(int positionStart, int itemCount) 列表從positionStart位置到itemCount數(shù)量的列表項(xiàng)批量刪除數(shù)據(jù)時(shí)調(diào)用,伴有動(dòng)畫效果

一、payload、notifyItemChanged()實(shí)現(xiàn)局部刷新:

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

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

    //構(gòu)建ListView的數(shù)據(jù)比較結(jié)果
    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()進(jìn)行局部刷新:

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)
        //點(diǎn)擊局部刷新
        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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫功能

    Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫功能

    這篇文章主要介紹了Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫功能,感興趣的小伙伴們可以參考一下
    2015-12-12
  • android 中win10 使用uwp控件實(shí)現(xiàn)進(jìn)度條Marquez效果

    android 中win10 使用uwp控件實(shí)現(xiàn)進(jìn)度條Marquez效果

    這篇文章主要介紹了android 中win10 使用uwp控件實(shí)現(xiàn)進(jìn)度條Marquez效果,需要的朋友可以參考下
    2017-06-06
  • 修改Android中hosts文件的步驟詳解

    修改Android中hosts文件的步驟詳解

    有朋友問Android怎么修改Hosts?對(duì)于這個(gè)問題,由于手頭并沒有Android設(shè)備,所以只能從網(wǎng)上搜羅了方法并總結(jié)出來,下面這篇文章主要介紹了修改Android中hosts文件的步驟,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • 深入理解Android 5.0中的Toolbar

    深入理解Android 5.0中的Toolbar

    相信大家都有所體會(huì),搜索Toolbar相關(guān)文章滿天飛,但是大都不是很全面,每次要用到的時(shí)候又要重頭過濾一遍。而且隨著版本升級(jí)很多較早的文章的方法已經(jīng)失效,最近剛好好用到Toolbar,就將相關(guān)配置整理下,方便以后需要的時(shí)候或者有需要的朋友們參考學(xué)習(xí)。
    2017-01-01
  • Android自定義ViewFlipper實(shí)現(xiàn)滾動(dòng)效果

    Android自定義ViewFlipper實(shí)現(xiàn)滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義ViewFlipper實(shí)現(xiàn)滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解

    Android程序開發(fā)中單選按鈕(RadioGroup)的使用詳解

    在android程序開發(fā)中,無論是單選按鈕還是多選按鈕都非常的常見,接下來通過本文給大家介紹Android程序開發(fā)中單選按鈕(RadioGroup)的使用,需要的朋友參考下吧
    2016-03-03
  • Android 屏幕雙擊事件的捕獲簡(jiǎn)單示例

    Android 屏幕雙擊事件的捕獲簡(jiǎn)單示例

    本文主要介紹 Android屏幕雙擊事件的捕獲,這里整理了相關(guān)資料,并附示例代碼,有興趣的小伙伴可以參考下
    2016-08-08
  • Flutter 中的PageStorage小部件使用及最佳實(shí)踐

    Flutter 中的PageStorage小部件使用及最佳實(shí)踐

    在Flutter中,PageStorage小部件提供了一種方法來保存和恢復(fù)頁面間的信息,這對(duì)于具有多個(gè)頁面且需要在這些頁面之間共享狀態(tài)的應(yīng)用程序非常有用,本文將詳細(xì)介紹PageStorage的用途、如何使用它以及一些最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Android自定義ViewGroup的實(shí)現(xiàn)方法

    Android自定義ViewGroup的實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android 在子線程中更新UI的幾種方法示例

    Android 在子線程中更新UI的幾種方法示例

    本篇文章主要介紹了Android 在子線程中更新UI的幾種方法示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-08-08

最新評(píng)論