Nativescript RadListView:如何以编程方式更改所选项目的背景色?

时间:2018-12-15 01:13:47

标签: nativescript nativescript-telerik-ui radlistview

在后面的代码中以编程方式执行此操作(不绑定到XML),除了选择项目时我的背景色不显示之外,其他所有操作都有效。没有错误,我的图像显示正常。我的代码搞砸了什么? (谢谢!)

我正在运行{N} CLI "tns-android":"version": "3.4.2", "nativescript-ui-listview": "3.5.1"

<GridLayout columns="auto, *">
    <StackLayout col="0" id="containers">
    </StackLayout>
    <StackLayout col="1" id="notes">
    </StackLayout>
</GridLayout>


function setContainers() {
    myCntnrPics.forEach(function (element) { // Need to it in ObservableArray
        pathToCntnrBitmapsArray.push(element);
    });

    let radListLeft = new listview.RadListView();
    // iOS NEEDS height set
    radListLeft.height = 2000;
    radListLeft.items = pathToCntnrBitmapsArray;
    let gridLayout = new listview.ListViewGridLayout();
    gridLayout.scrollDirection = "Vertical";
    radListLeft.selectionBehavior = "Press";
    radListLeft.itemSelected = "selected";
    radListLeft.on("itemSelected", function (ListViewEventData) {
        const item = pathToCntnrBitmapsArray.getItem(args.index);
        item.selected = true;
        console.log("itemSelected is true -----"); // This does not show up
    });
    radListLeft.on("itemTap", function (element){
        var selectedItemPath = pathToCntnrBitmapsArray.getItem(element.index).toString();
        console.log("on itemTap selectedItemPath: " + selectedItemPath); // shows correctly
    });
    if (application.android) {
        // Android needs H & W
        radListLeft.itemTemplate = "<StackLayout backgroundColor='{{ selected ? 'red' : 'white'}}'><Image src='{{ $value }}' height='123' width='123' loadMode='async' class='items'/></StackLayout>";
    } else if (application.ios) {
        radListLeft.itemTemplate = "<StackLayout backgroundColor='{{ selected ? 'red' : 'white'}}'><Image src='{{ $value }}' class='items'/></StackLayout>";
        gridLayout.itemHeight = 123;
        gridLayout.itemWidth = 123;
    }
    gridLayout.spanCount = 1;
    radListLeft.listViewLayout = gridLayout;
    let myContainers = page.getViewById("containers");
    myContainers.removeChildren();
    myContainers.addChild(radListLeft);
}

0 个答案:

没有答案
相关问题