【问题标题】:Nativescript: Listview items are reusing when scrollingNativescript:滚动时重复使用 Listview 项目
【发布时间】:2023-03-12 09:17:01
【问题描述】:

我正在尝试通过单击 STORE [<Label>] 将产品添加到购物车。 然后,我正在更改该添加列表的标签颜色。

之后,我尝试通过滚动添加更多产品。自动地,其他一些ListView 列表的标签颜色发生了变化。

我知道这些行为会发生在UITableViewiOS 中。在NSDictionary 的帮助下,我可以处理这个问题。 Tableview Reusing in iOS

我不知道如何在NativeScript 中处理这个问题

编码

.js

exports.cartColorChange = function(args) {
    var lbl = args.object;
    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" text="&#xf291;" textWrap="true" tap="cartColorChange" />

  </StackLayout>
</GridLayout>
</ListView.itemTemplate>
</ListView>

.css

Label.addCart{

    text-align: right;
    color: rgb(220, 220, 220);
    margin-right: 15px;
    margin-bottom: 0px;
    font-size: 15px;
    font-family: "FontAwesome";
}

输出:

【问题讨论】:

  • 请添加nativescript游乐场演示。
  • 我不知道。
  • play.nativescript.com
  • 任何帮助@bhavinjalodara
  • 请在 play.nativescript.com 上复制演示并发布链接。

标签: javascript xml nativescript


【解决方案1】:

您正在直接更改渲染对象的颜色,因为当对象被回收时,它会保持该颜色。

您可以通过单击时更改对象属性来达到相同的效果。并基于该属性应用样式。喜欢className="{{isClicked?'clickedCart':'notClickedCart'}}"stle.color="{{isClicked?'red':'blue'}}"

这里是更新的游乐场演示:https://play.nativescript.org/?template=play-js&id=T6sna8&v=8

编码

.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="&#xf291;" textWrap="true" tap="cartColorChange" />

  </StackLayout>
</GridLayout>
</ListView.itemTemplate>
</ListView>

.css

Label.clickedCart{
    color:rgb(171, 0, 230);
}

Label.notClickedCart{
    color:gray;
}

【讨论】:

  • 它工作正常。我需要,如果用户想从购物车中删除项目,用户可以单击再次取消选择。然后,另一件事, var lbl = args.object; var item=lbl.bindingContext console.log("Row_click_number." + item);记录 [我的 iPhone]:“Row_click_number.[object Object]”
  • 如何获取 secondArray[0] 的值?如果我尝试这样做,则 [object Object] 作为输出显示。但我需要,{ title: "The Programming", subtitle: "Programming", price: "$15.0" }
  • 这可能吗?该怎么做?
  • 我不明白你的问题是什么?
  • 你可以做secondArray.getItem(index)
【解决方案2】:

是的,列表视图项确实被重复使用以节省内存。

Here您可以找到用于解决问题的方法,该方法包括将列表视图项绑定到模型中的某个属性。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
相关资源
最近更新 更多