自定义ListView item包含checkbox实现单选和多选

CheckBox本为复选框,仍然可以当单选框用

自定义ListView item中包含一个textview 和checkbox,checkbox选中级别高于item,故在xml中进行屏蔽

单选方法:(该方法同样适用于radiobutton)


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    >
       <ImageView
        android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"    
        android:src="@drawable/triangle16blue"/>

    <ListView
        android:id="@+id/lvGroup"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/downbutton_corner"/>

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/item_tv"
        android:textSize="18sp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical" />
    <CheckBox 
        android:id="@+id/item_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
实现这个功能,可以使用 QML 中的 `ListView` 组件,通过嵌套两个 `ListView` 组件来实现双层嵌套。其中,外层 `ListView` 用于展示一级选项,内层 `ListView` 用于展示二级选项。同时,可以使用 `Delegate` 来自定义选项的样式和布局。 下面是一个简单的实现示例: ```qml import QtQuick 2.0 import QtQuick.Controls 2.5 ListView { id: outerListView width: 200 height: 400 model: outerModel delegate: ItemDelegate { text: model.label width: outerListView.width ListView { id: innerListView width: 200 height: 300 model: model.children visible: expanded delegate: ItemDelegate { text: model.label width: innerListView.width highlight: checked ? Rectangle { color: "lightblue" } : null onClicked: { if (multiSelect) { checked = !checked; } else { outerListView.currentIndex = -1; checked = true; } } } } Rectangle { id: expandIcon anchors.right: parent.right anchors.verticalCenter: parent.verticalCenter width: 10 height: 10 color: "black" transform: expanded ? Rotation { origin.x: 5; origin.y: 5; angle: 90 } : Rotation { origin.x: 5; origin.y: 5; angle: 0 } MouseArea { anchors.fill: parent onClicked: expanded = !expanded } } property bool expanded: false property bool multiSelect: false property bool checked: false } } ``` 该示例中,外层 `ListView` 的数据模型为 `outerModel`,内层 `ListView` 的数据模型为每个选项的 `children` 属性。对于每个选项,使用一个 `ItemDelegate` 来展示其文本和展开/收起按钮。展开/收起按钮使用一个黑色正方形来表示,并通过 `Rotation` 转换来实现箭头指向。同时,为 `ItemDelegate` 添加了一些属性,包括:`expanded` 表示选项是否展开,`multiSelect` 表示是否支持多选,`checked` 表示当前选项是否被选中。对于单选,当点击一个选项时,将取消外层 `ListView` 的选中状态,同时选中当前选项;对于多选,当点击一个选项时,将切换当前选项的选中状态。 这样,就可以通过嵌套 `ListView` 组件来实现 ElementUi 的 Cascader 功能了。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值