Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notifyItemRemoved删除存在分割线的staggered瀑布流崩溃 #157

Closed
newlai913 opened this issue Apr 21, 2022 · 18 comments
Closed

notifyItemRemoved删除存在分割线的staggered瀑布流崩溃 #157

newlai913 opened this issue Apr 21, 2022 · 18 comments
Labels
Bug Something isn't working

Comments

@newlai913
Copy link

newlai913 commented Apr 21, 2022

删除item时调用bindingAdapter?.notifyItemRemoved(modelPosition)产生的

2022-04-21 19:39:47.364 30326-30326/com.lsmh.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.lsmh.app, PID: 30326
    java.lang.NullPointerException
        at com.drake.brv.DefaultDecoration$Edge$Companion.computeEdge(DefaultDecoration.kt:732)
        at com.drake.brv.DefaultDecoration.getItemOffsets(DefaultDecoration.kt:298)
        at androidx.recyclerview.widget.RecyclerView.getItemDecorInsetsForChild(RecyclerView.java:5293)
        at androidx.recyclerview.widget.RecyclerView$LayoutManager.calculateItemDecorationsForChild(RecyclerView.java:10002)
        at androidx.recyclerview.widget.StaggeredGridLayoutManager.measureChildWithDecorationsAndMargin(StaggeredGridLayoutManager.java:1201)
        at androidx.recyclerview.widget.StaggeredGridLayoutManager.measureChildWithDecorationsAndMargin(StaggeredGridLayoutManager.java:1162)
        at androidx.recyclerview.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1606)
        at androidx.recyclerview.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:691)
        at androidx.recyclerview.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:604)
        at com.drake.brv.layoutmanager.HoverStaggeredGridLayoutManager.onLayoutChildren(HoverStaggeredGridLayoutManager.java:189)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4255)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4010)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
        at android.view.View.layout(View.java:23561)
        at android.view.ViewGroup.layout(ViewGroup.java:6452)
        at com.scwang.smart.refresh.layout.SmartRefreshLayout.onLayout(SmartRefreshLayout.java:651)
        at android.view.View.layout(View.java:23561)
        at android.view.ViewGroup.layout(ViewGroup.java:6452)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:23561)
        at android.view.ViewGroup.layout(ViewGroup.java:6452)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:23561)
        at android.view.ViewGroup.layout(ViewGroup.java:6452)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:23561)
        at android.view.ViewGroup.layout(ViewGroup.java:6452)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:23561)
        at android.view.ViewGroup.layout(ViewGroup.java:6452)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:23561)
        at android.view.ViewGroup.layout(ViewGroup.java:6452)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:857)
        at android.view.View.layout(View.java:23561)
        at android.view.ViewGroup.layout(ViewGroup.java:6452)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3851)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3301)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2265)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9019)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1142)
        at android.view.Choreographer.doCallbacks(Choreographer.java:946)
        at android.view.Choreographer.doFrame(Choreographer.java:875)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1127)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
@newlai913 newlai913 added the 寻求帮助 Extra attention is needed label Apr 21, 2022
@liangjingkanji
Copy link
Owner

你有删除models里面的数据吗?

@newlai913
Copy link
Author

newlai913 commented Apr 21, 2022

你有删除models里面的数据吗?

onClick(R.id.delete) {
    val model = getModel<ChannelItem>()
    rv?.mutable?.remove(model)
    this.bindingAdapter?.notifyItemRemoved(modelPosition)
}

我是这样调用的

@liangjingkanji
Copy link
Owner

你是不是添加了Header, 这会导致modelPosition无法对应真实位置

@newlai913
Copy link
Author

你是不是添加了Header, 这会导致modelPosition无法对应真实位置

没有添加

@liangjingkanji
Copy link
Owner

liangjingkanji commented Apr 21, 2022

modelPosition改为layoutPosition试下, 另外给我看下你如何添加的分割线

@newlai913
Copy link
Author

newlai913 commented Apr 21, 2022

modelPosition改为layoutPosition试下, 另外给我看下你如何添加的分割线

bd.rv.staggered(2)
            .divider {
                setDrawable(R.drawable.divider_horizontal)
                orientation = DividerOrientation.GRID
                startVisible = true
            }

@liangjingkanji
Copy link
Owner

你就不能用代码块包裹代码吗? 你这可读性太差

@newlai913
Copy link
Author

你就不能用代码块包裹代码吗? 你这可读性太差

我换成layoutPosition也是一样的崩溃诶

@liangjingkanji
Copy link
Owner

liangjingkanji commented Apr 21, 2022

这个问题不是很好复现, 你能否fork仓库在demo中复现该崩溃. 利于加快解决速度

@newlai913
Copy link
Author

这个问题不是很好复现, 你能否fork仓库在demo中复现该崩溃. 利于加快解决速度

好,我一会把完整的代码拷过去,那我如果只是想删除item并更新需要怎么操作

@liangjingkanji
Copy link
Owner

liangjingkanji commented Apr 21, 2022

虽然你代码写的啰嗦了但是的确是这么写的

R.id.delete.onClick {
    mutable.removeAt(layoutPosition) // 鉴于我怕你models里面塞得数据类有相同的就不使用mutable.remove(getModel())
    notifyItemRemoved(layoutPosition)
}

虽然这个说法可能有点侮辱你, 但是mutable.remove(element)是使用equals来判断是否为指定删除的元素(data class的参数相同会被判定为equals为true)这个问题你知道的吧? 因为这是语法基础

@newlai913
Copy link
Author

newlai913 commented Apr 21, 2022

虽然你代码写的啰嗦了但是的确是这么写的

R.id.delete.onClick {
    mutable.removeAt(layoutPosition) // 鉴于我怕你models里面塞得数据类有相同的就不使用mutable.remove(getModel())
    notifyItemRemoved(layoutPosition)
}

虽然这个说法可能有点侮辱你, 但是mutable.remove(element)是使用equals来判断是否为指定删除的元素(data class的参数相同会被判定为equals为true)这个问题你知道的吧? 因为这是语法基础

我使用你给的这个删除还是崩了....
bd.rv.staggered(2)
.divider {
setDrawable(R.drawable.divider_horizontal)
orientation = DividerOrientation.GRID
startVisible = true
}
.setup {
addType(R.layout.item_common)
onBind {}
onClick(R.id.ivCollect) {
mutable.removeAt(layoutPosition)
notifyItemRemoved(layoutPosition)
}
}.models = mutableListOf(...)

@newlai913
Copy link
Author

虽然你代码写的啰嗦了但是的确是这么写的

R.id.delete.onClick {
    mutable.removeAt(layoutPosition) // 鉴于我怕你models里面塞得数据类有相同的就不使用mutable.remove(getModel())
    notifyItemRemoved(layoutPosition)
}

虽然这个说法可能有点侮辱你, 但是mutable.remove(element)是使用equals来判断是否为指定删除的元素(data class的参数相同会被判定为equals为true)这个问题你知道的吧? 因为这是语法基础

我使用你给的这个删除还是崩了.... bd.rv.staggered(2) .divider { setDrawable(R.drawable.divider_horizontal) orientation = DividerOrientation.GRID startVisible = true } .setup { addType(R.layout.item_common) onBind {} onClick(R.id.ivCollect) { mutable.removeAt(layoutPosition) notifyItemRemoved(layoutPosition) } }.models = mutableListOf(...)

我把瀑布流换成grid就不会崩了

@liangjingkanji
Copy link
Owner

liangjingkanji commented Apr 21, 2022

使用notifyItemRemoved的确会崩溃, 暂时不要使用带动画更新吧, 使用以下代码不会崩溃

R.id.delete.onClick {
    mutable.removeAt(layoutPosition)
    notifyDataSetChanged()
}

该问题已修复请等待新版本发布

@liangjingkanji liangjingkanji changed the title 大佬,贴一个崩溃 notifyItemRemoved删除存在分割线的staggered瀑布流崩溃 Apr 21, 2022
@liangjingkanji
Copy link
Owner

如何了?

@newlai913
Copy link
Author

如何了?

我现在先用grid没问题

@liangjingkanji
Copy link
Owner

grid可以替代staggered? 我问题已经修复了, 使用notifyDataSetChanged或者notifyItemRemoved都可以

@newlai913
Copy link
Author

grid可以替代staggered? 我问题已经修复了, 使用notifyDataSetChanged或者notifyItemRemoved都可以

不能替代,但是我们那个列表可以不使用瀑布流,因为item高度一样的,我更新到你的最新版本

@liangjingkanji liangjingkanji added Bug Something isn't working and removed 寻求帮助 Extra attention is needed labels May 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants