如何在第三级中使用无线电组实现三级可扩展列表/回收者视图/树视图

时间:2019-04-02 08:40:42

标签: java android kotlin treeview radio-group

我被困在必须使用3级可扩展列表的情况下,并且在最低3级中将有无线电组。第二层可以有多个子层。换句话说,第二级可以有多个孩子。

我已经尝试了Recycler View内的Expandable List View,但是由于Recycler View使OnCreateView中的视图膨胀。我无法在OnBindHolder中更改视图的大小。类似的问题是ExpandableListView内部的ExpandableListView。

我尝试了AndroidTreeView库。 https://github.com/bmelnychuk/AndroidTreeView

它对于复选框非常有效,因为我不必处理其他复选框。但是如果是单选按钮。我无法创建一个广播组,因此在第三级中一次只能选择一个项目。由于我无法控制其他单选按钮。

我试图为第三级修改OnViewHolder类。但即使我这样做,也无法正确处理其他单选按钮的检查。必须重新启动该活动以应用会产生故障的更改。那是我不想要的。

这是我的第三级OnViewHolderClass:


class ThirdListViewHolder(context: Context, var reloadView: () -> Unit) :
    TreeNode.BaseNodeViewHolder<ThirdListViewHolder.IconTreeItem>(context) {

    override fun createNodeView(node: TreeNode, value: IconTreeItem): View {
        val inflater = LayoutInflater.from(context)
        val view = inflater.inflate(R.layout.item_child_view_package_order_items_expandable_list_view, null, false)
        val tvValue = view.findViewById(R.id.select_item_check_box) as RadioButton
        tvValue.text = value.text
        tvValue.tag = value.tag


        tvValue.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {

                val getTag = tvValue.tag.toString().split("_")

                for (i in 0 until checkListControl.size) {
                    val grandParentID = checkListControl.get(i).grandParentID
                    val parentID = checkListControl.get(i).parentID
                    val childID = checkListControl.get(i).childID
                    val isChecked = checkListControl.get(i).isChecked

                    if (grandParentID.equals(getTag[0]) && parentID.equals(getTag[1]) && childID.equals(getTag[2])) {

                        val oldItem = CheckListControl(getTag[0], getTag[1], getTag[2], tvValue.text.toString(), false)
                        checkListControl.remove(oldItem)

                        val newItem = CheckListControl(getTag[0], getTag[1], getTag[2], tvValue.text.toString(), true)
                        tvValue.isChecked = true

                        checkListControl.add(newItem)
                        break

                    }
                }
            }
            reloadView()
        }
        return view
    }

    class IconTreeItem {
        var icon: Int = 0
        var tag: String? = null
        var text: String? = null
        var isChecked: Boolean = false
    }


    fun manageList(tag: String, name: String, isChecked: Boolean) {

        Log.d("tag_value", tag)
        var parentId: String? = null
        var itemId: String? = null

        val ids = tag.split("_")
        parentId = (ids.get(0).toInt() + 1).toString()


        for (i in 0 until PackageOrderItemsActivity.itemIdsList.size) {
            if (PackageOrderItemsActivity.itemIdsList.get(i).name.equals(name)) {
                itemId = PackageOrderItemsActivity.itemIdsList.get(i).item_id
            }
        }

        if (isChecked) {
            //Add
            if (PackageOrderItemsActivity.ParentChildHashMap.containsKey(parentId)) {
                val array = PackageOrderItemsActivity.ParentChildHashMap.get(parentId)
                if (!array!!.contains(itemId)) {
                    array.add(itemId!!)
                    PackageOrderItemsActivity.ParentChildHashMap.put(parentId, array)
                }
            } else {
                val array: ArrayList<String> = ArrayList()
                array.add(itemId!!)
                PackageOrderItemsActivity.ParentChildHashMap.put(parentId, array)
            }
        } else {
            if (PackageOrderItemsActivity.ParentChildHashMap.containsKey(parentId)) {
                val array = PackageOrderItemsActivity.ParentChildHashMap.get(parentId)
                if (array!!.contains(itemId)) {
                    array.remove(itemId)

                    if (array.isEmpty())
                        PackageOrderItemsActivity.ParentChildHashMap.remove(parentId)
                    else
                        PackageOrderItemsActivity.ParentChildHashMap.put(parentId, array)

                }
            }

        }

        Log.d("hashMap", PackageOrderItemsActivity.ParentChildHashMap.toString())

    }


}

这是我在Activity中创建TreeView的代码:

 fun populateTree() {
        val root = TreeNode.root()
        var parent: TreeNode? = null

//            val parent = TreeNode("parentList")
//            val child0 = TreeNode("secondList")
//            val child1 = TreeNode("thirdList")

        for (i in 0 until parentList.size) {
            var child1: TreeNode? = null
            var child2: TreeNode? = null

            val nodeItem = ParentListViewHolder.IconTreeItem()
            nodeItem.text = parentList.get(i)
            parent = TreeNode(nodeItem).setViewHolder(ParentListViewHolder(this@PackageOrderItemsActivity))

            for (j in 0 until secondList.size) {
                val nodeItem1 = SecondListViewHolder.IconTreeItem()
                nodeItem1.text = secondList.get(j)
                child1 = TreeNode(nodeItem1).setViewHolder(SecondListViewHolder(this@PackageOrderItemsActivity))
                parent.addChild(child1)

                if (thirdList.containsKey(secondList.get(j))) {

                    val list = thirdList.get(secondList.get(j))!!

                    Log.d("firstList", list.toString())
                    for (l in 0 until list.size) {
                        val nodeItem2 = ThirdListViewHolder.IconTreeItem()

                        nodeItem2.text = list.get(l)
                        nodeItem2.tag = i.toString() + "_" + j + "_" + l

                        if(firstTime) {
                            val checkListItem =
                                CheckListControl(i.toString(), j.toString(), l.toString(), list.get(l), false)
                            checkListControl.add(checkListItem)
                        }

                        for (m in 0 until checkListControl.size) {
                            val grandParentId = checkListControl.get(m).grandParentID
                            val parentId = checkListControl.get(m).parentID
                            val childId = checkListControl.get(m).childID
                            val isChecked = checkListControl.get(m).isChecked

                            if (grandParentId.equals(i.toString()) && parentId.equals(j.toString()) &&
                                childId.equals(l.toString())
                            ) {
                                nodeItem2.isChecked = isChecked
                            }
                        }

                        child2 =
                            TreeNode(nodeItem2).setViewHolder(
                                ThirdListViewHolder(
                                    this@PackageOrderItemsActivity, reloadView = {
                                        //  populateTree()
                                        // init()
                                        ParentChildHashMap.clear()
                                        parentList.clear()
                                        secondList.clear()
                                        thirdList.clear()
                                        container.removeAllViews()

                                        getSubscriptionDetails()
                                    })
                            )

                        child1.addChild(child2)
                        if (nodeItem2.isChecked) {
                            parent.isExpanded = true
                            child1.isExpanded = true
                        }

                    }
                }


            }
            firstTime = false
            root.addChild(parent)

        }



        tView = AndroidTreeView(this@PackageOrderItemsActivity, root)
        container.addView(tView!!.view)


    }

0 个答案:

没有答案