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

Staggered布局+header+分割线,addModels 后出现的问题 #402

Closed
LeonShannon opened this issue Nov 9, 2023 · 1 comment
Closed

Comments

@LeonShannon
Copy link

LeonShannon commented Nov 9, 2023

描述

瀑布流布局+分割线+header,然后再添加一些数据,会导致分割线计算的时候 layoutParams 拿到是 null,导致 NPE

理由

参考实现

以下代码通过 demo 增加 header 和 addModels 可稳定复现

class StaggeredDividerFragment :
    BaseDividerFragment<FragmentStaggeredDividerBinding>(R.layout.fragment_staggered_divider) {

    override fun initView() {
        binding.rv.staggered(3, RecyclerView.VERTICAL).divider {
            setDrawable(R.drawable.divider_horizontal)
            // includeVisible = true
        }.setup {
            addType<HeaderFooterFragment.Header>(R.layout.item_header)
            addType<DividerModel>(R.layout.item_divider_vertical)
            onBind {
                when (itemViewType) {
                    R.layout.item_divider_vertical -> {
                        // 设置动态高度
                        val layoutParams = itemView.layoutParams
                        layoutParams.height = getModel<DividerModel>().height
                        itemView.layoutParams = layoutParams
                    }

                    R.layout.item_header -> {
                        val layoutParams =
                            itemView.layoutParams as? StaggeredGridLayoutManager.LayoutParams
                        layoutParams?.isFullSpan = true
                        itemView.layoutParams = layoutParams
                    }
                }

            }
        }.models = getData()
        CoroutineScope(Dispatchers.Main).launch {
            val data = mutableListOf<Any>()
            delay(500)
            data.add(DividerModel(300))
            delay(500)
            data.add(DividerModel(2000))
            binding.rv.addModels(data, index = 3)
        }
    }

    private fun getData(): List<Any> {
        val data = mutableListOf<Any>()
        // 生成动态宽高
        for (i in 0..9) {
            when (i) {
                2 -> data.add(DividerModel(400))
                3 -> data.add(DividerModel(500))
                4 -> data.add(DividerModel(700))
                5 -> data.add(DividerModel(1000))
                8 -> data.add(DividerModel(200))
                else -> data.add(DividerModel())
            }
        }
        data.add(HeaderFooterFragment.Header())
        return data
    }

    override fun initData() {
    }
}
image image

是否可以参与实现

由于本项目参与者的技术及精力有限无法满足大部分开发者的需求, 欢迎开发者参与贡献

@liangjingkanji
Copy link
Owner

已修复在1.5.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants