I am trying to add products to Cart by clicking STORE []. Then, I am changing label\'s color for that added list.
After that,
Yes listview items are indeed reused in order to save up memory.
Here you can find the approach used to save your problem which consists in binding the listview item to a certain property in your model.
you are directly changing color of the rendered object because of which when object is recycled it persist that color.
you can achieve same effect by changing object property when clicked. and based on that property applying styles. like className="{{isClicked?'clickedCart':'notClickedCart'}}"
or stle.color="{{isClicked?'red':'blue'}}"
here is updated playground demo:https://play.nativescript.org/?template=play-js&id=T6sna8&v=8
Coding
.js
exports.cartColorChange = function(args) {
var lbl = args.object;
var item=lbl.bindingContext;
var index = secondArray.indexOf(item)
console.log("Clicked item with index " + index);
item.isClicked = !item.isClicked;
secondArray.setItem(index,item);
// lbl.color = "rgb(171, 0, 230)";
};
.xml
<ListView col="0" row="2" items="{{ mySecondItems }}" id="myListVw" itemLoading="listViewItemsLoading" itemTap="secondListViewItemTap" class="list-group" separatorColor="transparent">
<ListView.itemTemplate>
<GridLayout class="listGrid" columns="75,*" rows="*" width="100%" height="90" >
<Label col="0" row="0" class="roundCircle" text="{{ price }}" textWrap="true" />
<StackLayout col="1" row="0" orientation="vertical" verticalAlignment="center">
<Label class="booksubtitle" text="{{ subtitle }}" textWrap="true" id="wow" />
<Label class="bookTitle" text="{{ title }}" textWrap="true" />
<Label class="addCart" className="{{isClicked?'clickedCart':'notClickedCart'}}" text="" textWrap="true" tap="cartColorChange" />
</StackLayout>
</GridLayout>
</ListView.itemTemplate>
</ListView>
.css
Label.clickedCart{
color:rgb(171, 0, 230);
}
Label.notClickedCart{
color:gray;
}