listview实现点击选中


Rectangle {
        x:10
        y:20
    width: 200; height: 300
    anchors.rightMargin: 4
    anchors.bottomMargin: 4
    anchors.leftMargin: 4
    anchors.topMargin: 4
    anchors.fill: parent
    ColumnLayout
    {
        //clip: true
        spacing: 2
        anchors.fill: parent
 
        Rectangle
        {
            //color:"yellow"
            Layout.fillWidth: true
            Layout.fillHeight: true
            RowLayout {
                anchors.fill: parent
                Rectangle
                {
                    Layout.fillWidth: true
                    Layout.fillHeight: true
 
                    ListView {
                        id:idLogListView
                        focus:true
                        //Keys.enabled: true
                        //highlightRangeMode: ListView.ApplyRange
                        anchors.fill: parent
                        highlight: Rectangle { color: "#00CED1" }
                        model: idListModle
                        delegate: Component
                        {
 
                        RowLayout {
 
                            id:idlistElemnet
                            height: 20
                            width: 250
                            spacing: 20
                            Layout.fillWidth: true
 
                            Rectangle {height: 16
                                width: 16
                                radius: 5
                                color:getListEleHeadColor(type)
                                Text{ anchors.centerIn: parent}
                            }
 
                            Text { text: time; font.bold: true}
                            Text { text:type }
                            Text { text:descripe; color:"blue" ; Layout.fillWidth: true}
                           //states,transitions主要用于特效的效果
                            states: State {
                                name: "Current"
                                when: idlistElemnet.ListView.isCurrentItem
                                PropertyChanges { target: idlistElemnet; x: 20 }
                            }
                            transitions: Transition {
                                NumberAnimation { properties: "x"; duration: 200 }
                            }
                            MouseArea {
                                anchors.fill: parent
                                  //这里实现的的点击选中
                                onClicked: 
{
idlistElemnet.ListView.view.currentIndex = index
                            getItem(descripe);//这里调用js函数将选定对象的值传给js函数                       
}
                            }
 
                        }
                    }
                //界面完成时设置listview的值
                    Component.onCompleted:
                    {
                        for(var idx=0;idx<10;idx++)
                        {
                            var newType=parseInt((Math.random(Math.random())*100+1)%3);
                            //idListModle.append( { "descripe": idRootPriv.m_vm.m_str[idx],"time": "2017-1-24"});
                        }
                    }
                }
                ListModel {
 
                    id:idListModle
                    ListElement {
                        descripe: "项目."
                        time: "2017-1-24"
                    }
 
                }
            }
            Rectangle
            {
                Layout.fillHeight: true
                // 滚动条
                id: scrollbar
                width: 10;
                height: 200
                color: "#D9D9D9"
                radius: 10
                // 按钮
                Rectangle {
                    id: button
                    x: 0
                    y: idLogListView.visibleArea.yPosition * scrollbar.height
                    width: 10
                    height: idLogListView.visibleArea.heightRatio * scrollbar.height;
                    color: "#979797"
                    radius: 10
                    // 鼠标区域
                    MouseArea {
                        id: mouseArea
                        anchors.fill: button
                        drag.target: button
                        drag.axis: Drag.YAxis
                        drag.minimumY: 0
                        drag.maximumY: scrollbar.height - button.height
                        // 拖动
                        onMouseYChanged: {
                            idLogListView.contentY = button.y / scrollbar.height * idLogListView.contentHeight
                        }
                    }
                }
            }
        }
    }
    Rectangle
    {
        Layout.preferredHeight: 40
        Layout.fillWidth: true
        Layout.minimumHeight:40
    }
}
 
function getListEleHeadColor(ntype)
{
    switch(ntype)
    {
 
    case 0:
        return "lightblue"
    case 1:
        return "red";
 
    case 2:
        return "yellow";
    case 3:
        return "green";
    default:
        return "black";
    }
 
}
    }
//这里加载的数据。自己定义了一个按钮来调用这个函数将数据加载到listview中
    function add_list_text() {
    for(var i=0;i<14;i++)
    {
        var newType=parseInt((Math.random(Math.random())*100+1)%3);
        idListModle.append( { "descripe": idRootPriv.m_vm.get_m_str(i),"time": "2017-1-24"});
    }
    }
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值