Recyclerview 刷新「建议收藏」

2022-11-15 10:56:10 浏览数 (1)

前言:recyclerview比起listview功能上更加丰富外(如横向列表),在Item复用上也更加灵活,比如listview的某个Item数据需要更新,要通过notifyDataSetChanged方法对全部Item进行刷新,而recyclerview则可以精准刷新。

介绍:

(1)notifyItemChanged(position)

只刷新该position的Item,即只是该Item调用onBindViewHolder,因此如果对数据源进行插、移除操作不能改方法只刷新操作的Item,毕竟该Item之后的Item的position都发生了改变

(2)notifyItemChanged(int position, Object payload)

对position的Item进行局部刷新,在onBindViewHolder(ContentViewHolder viewHolder, int position, List<Object> payloads)中根据payloads.get(position)值只对需要刷新的控件进行操作

(3)notifyDataSetChanged()

全部Item进行刷新

(4)notifyItemRangeChanged(position, itemCount)

刷新position及之后ItemCount个Item

(5)notifyItemInserted(position)

插入并进行刷新

(6)notifyItemRangeInserted(int position, int itemCount)

从position开始插入itemCount个Item并进行刷新

(7)notifyItemRemoved(int position)

移除并进行刷新

(8)notifyItemRangeRemoved(int position, int itemCount)

从position开始移除itemCount个Item并进行刷新

(9)notifyItemMoved(int fromPosition, int toPosition)

移动并进行刷新

注意:

当对数据源进行移除或添加操作时需要立即执行刷新操作,将adapter内部记录的数据源状态和我们传给adapter的数据源进行同步更新,否则可能会导致RecyclerView报如下异常,同时还没有我们应用异常堆栈信息

代码语言:javascript复制
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{89b9fcf position=1 id=-1, oldPos=1, pLpos:-1 scrap [attachedScrap] tmpDetached no parent} 
android.support.v7.widget.RecyclerView{d9e5733 VFED..... ......ID 0,0-1080,364 #7f0903a2 app:id/rv_shop_bag_list}, adapter:com.lpf.demo.order.adapter.MyAdapter@5f994d1, 
layout:android.support.v7.widget.LinearLayoutManager@1423b36, context:com.lpf.demo.activity.MainActivity@f8937f9
        at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
        at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1877)
        at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:407)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029)
        at android.view.Choreographer.doCallbacks(Choreographer.java:841)
        at android.view.Choreographer.doFrame(Choreographer.java:769)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1015)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6651)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

这里列举一种导致该异常的例子

代码语言:javascript复制
错误:
val list = mutableListOf("第1条", "第2条", "第3条", "第4条")
list.removeAt(0)
mAdapter.notifyItemRangeChanged(0, list.size)

正确:
val list = mutableListOf("第1条", "第2条", "第3条", "第4条")
val size = list.size
list.removeAt(0)
mAdapter.notifyItemRangeChanged(0, size)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/234607.html原文链接:https://javaforall.cn

0 人点赞