liangjingkanji / BRV

[使用文档] Android 快速构建 RecyclerView, 比 BRVAH 更简单强大
http://liangjingkanji.github.io/BRV/
MIT License
2.49k stars 317 forks source link

网格布局在使用分割线代替addItemDecoration()实现时,会出现一些预料之外的bug。 #356

Closed PengShaoLei1997 closed 1 year ago

PengShaoLei1997 commented 1 year ago

问题描述

使用分割线代替 addItemDecoration() 实现时,在预先添加的数据生成视图时,能够正常生成尺寸不溢出的分割线,但是如果在新增数据后使用 notifyItem* 刷新RecyclerView会导致每行的item在添加后存在尺寸溢出的情况。

  1. 通过issue中查找类似问题,发现在新增数据后需要手动调用 invalidateItemDecorations() 去刷新分隔效果,修改后发现问题还是存在,并在新增每行第二个item时第一个item会恢复正常尺寸,但是第二个item并不会按照预先设置的列表动画去触发动画效果。
  2. 在不调用 invalidateItemDecorations() 情况下,notifyItemRemoved删除数据后尺寸也会恢复正常,这是什么原因?

相关代码

数据源: private val dataList = mutableListOf()

初始化Data和View: private fun initRecyclerView() { repeat(20) { dataList.add("$it") } binding.rvMain.grid(3) .divider { setDivider(12, true) setColor(Color.RED) orientation = DividerOrientation.GRID includeVisible = true } .setup { addType(R.layout.item_bluetooth) onBind { getBinding().run { tvItemBluetoothName.text = getModel() } } setAnimation(AnimationType.SLIDE_RIGHT) animationRepeat = true } .models = dataList }

新增事件: dataList.add("test") binding.rvMain.bindingAdapter.notifyItemInserted(dataList.size) binding.rvMain.invalidateItemDecorations()

删除事件: dataList.removeAt(dataList.size - 1) binding.rvMain.bindingAdapter.notifyItemRemoved(dataList.size)

视频录制

问题1: https://user-images.githubusercontent.com/49141931/233582901-68ac382c-871f-4b7a-bdfd-0703c25d0f68.mp4

问题2: https://user-images.githubusercontent.com/49141931/233589228-5c1fa55d-30da-401c-b485-d90fd4c44179.mp4

版本

liangjingkanji commented 1 year ago

麻烦fork仓库复现此问题或者将你的demo附上

工作繁忙非严重bug暂时可能不能马上修复, 你可以尝试pr解决此问题

PengShaoLei1997 commented 1 year ago

麻烦fork仓库复现此问题或者将你的demo附上

工作繁忙非严重bug暂时可能不能马上修复, 你可以尝试pr解决此问题

已fork项目并复现问题,您有时间的话希望帮忙看一下,感谢。🤪

liangjingkanji commented 1 year ago

fork仓库发我地址就行了, 不需要pr, 我说的pr是你修复此问题然后pr.....

因为这是开源项目每个用户都应当是作者之一, 我的工作实在是非常繁忙毕竟是小黄人

PengShaoLei1997 commented 1 year ago

不好意思,我理解错了。😂 仓库地址:https://github.com/PengShaoLei1997/BRV

PengShaoLei1997 commented 1 year ago

尝试了下通过addModels去替代add+notifyItemInserted+invalidateItemDecorations,可以正常显示item动画,并且尺寸不溢出,请问下这是为什么呀? image

PengShaoLei1997 commented 1 year ago

我查看了一下addModels的源码,并与我的实现代码进行了对比,发现了一处差异,我在修改差异后现在可以正常显示了。 image

liangjingkanji commented 1 year ago

记得好像之前文档有写过提醒开发者使用invalidateItemDecorations()但是我也没看到不知道我写哪去了

liangjingkanji commented 1 year ago

如果你有心可以修改下文档发起pr, 帮助后来人避坑