Android 二级列表实现

2020-01-07  本文已影响0人  坐怀灬不乱
[{
        "list1": "list",
        "child": [{
                "child1": "child1"
            },
            {
                "child2": "child2"
            }, {
                "child3": "child3"
            }
        ]
    }
]

一级列表中包含二级列表

 fun addData(bean: MutableList<Bean>, position: Int, size: Int, isScroll: Boolean = true) {
        for (i in 0 until size) {
            add(bean[i], position + 1 + i)
        }
        if (isScroll) {
            //等于空代表Item不可见,在屏幕外,就移动进屏幕
            if (recyclerView.layoutManager?.findViewByPosition(position + 1)?.getGlobalVisibleRect(Rect()) == null) {
                recyclerView.scrollToPosition(position + 1)
            }
        }
    }
open fun add(data: L, index: Int = -1) {
        if (index != -1) {
            l.add(index, data)
            notifyItemInserted(index)
            notifyItemRangeChanged(index, itemCount)
        } else {
            l.add(data)
            notifyItemInserted(l.size)
            notifyItemRangeChanged(l.size, itemCount)
        }
    }
private fun hideData(position: Int, size: Int) {
        val intList = mutableListOf<Int>()
        for (i in 0 until size) {
            intList.add((position + 1 + i))
        }
        remove(intList) {
            
        }
    }

批量使用下标移除方法
list是待移除的下标集合

    open fun remove(list: MutableList<Int>) {
        list.sortWith(Comparator { o1, o2 ->
            return@Comparator Integer.compare(o1, o2)
        })

        for (i in 0 until list.size) {
            var posi = list[i]
            posi -= i
            remove(posi) {}
        }
    }

以上就是大概实现思路,如果有问题评论告诉我哦~
顺利使用的老铁帮点个赞,谢谢啦~

上一篇下一篇

猜你喜欢

热点阅读