[英]ListBox items change after doing scroll in Firemonkey
I am developing a Multi-Device Application in Firemonkey where Main class has a ListBox component with some items.我正在 Firemonkey 中开发一个多设备应用程序,其中 Main 类有一个包含一些项目的 ListBox 组件。 Each of these items has the same custom style.
这些项目中的每一个都具有相同的自定义样式。
My problem is when I have so many items in the ListBox and I have to do scroll vertical to see the rest of items.我的问题是当我在 ListBox 中有这么多项目时,我必须垂直滚动才能看到其余项目。 In this case, the ListBox has a strange behaviour and when I do scroll up after doing scroll down the item's components (a button for example) have changed his background colour and the items have changed his order inside ListBox.
在这种情况下,ListBox 有一个奇怪的行为,当我在向下滚动后向上滚动时,项目的组件(例如一个按钮)改变了他的背景颜色,并且这些项目改变了他在 ListBox 中的顺序。
For example, if I had:例如,如果我有:
after I do scroll I have:在我滚动后,我有:
This change is random.这种变化是随机的。 Each time is different.
每一次都不一样。
Real example (process steps):真实示例(流程步骤):
Do vertical scrolling down to see the rest of items.垂直向下滚动以查看其余项目。
Do vertical scrolling upward to return to the top of the list.垂直向上滚动以返回列表顶部。
Why I have this behaviour in the ListBox??为什么我在 ListBox 中有这种行为?? How I can solve it and the ListBox do not change items order neither background colour of his components?
我如何解决它并且 ListBox 不会更改项目顺序,也不会更改其组件的背景颜色?
I do not know if there is any property to block items inside ListBox or similar ...我不知道是否有任何属性可以阻止 ListBox 或类似内容中的项目...
EDIT编辑
This is the code to create and initialize the ListBox items:这是创建和初始化 ListBox 项的代码:
procedure TRooms_Form.FormCreate(Sender: TObject);
var
...
begin
i := 0;
while i < numItems do begin
//Create ListBox item
item := TListBoxItem.Create(nil);
item.Parent := myListBox;
item.StyleLookup := 'styleLBox';
//Number
itemNumber := item.FindStyleResource('btt_number') as TButton;
if Assigned(itemNumber) then begin
itemNumber.Text := jsonNumber;
case jsonColor of
0 : itemNumber.TintObject.TintColor := TAlphaColors.Chocolate;
1 : itemNumber.TintObject.TintColor := TAlphaColors.Gold;
2 : itemNumber.TintObject.TintColor := TAlphaColors.Darkgreen;
3 : itemNumber.TintObject.TintColor := TAlphaColors.Deeppink;
end;
end;
//Title
itemTitle := item.FindStyleResource('txtstyle_title') as TText;
if Assigned(itemTitle) then begin
itemTitle.Text := jsonTitle;
end;
//Occupation
itemOccup := item.FindStyleResource('txt_occupation') as TText;
if Assigned(itemOccup) then begin
itemOccup.Text := jsonOccup;
end;
//Dates
itemDay := item.FindStyleResource('txt_day') as TText;
if Assigned(itemDay) then itemDay.Text := displayDay;
itemDateStart := item.FindStyleResource('txt_start') as TText;
if Assigned(itemDateStart) then itemDateStart.Text := jsonTimeStart;
itemDateEnd := item.FindStyleResource('txt_end') as TText;
if Assigned(itemDateEnd) then itemDateEnd.Text := jsonTimeEnd;
//Item background
itemBackgr := item.FindStyleResource('background_item') as TRectangle;
if Assigned(itemBackgr) then begin
itemBackgr.Fill.Kind := TBrushKind.Solid;
case jsonStatus of
0 : itemBackgr.Fill.Color := TAlphaColors.White;
1 : itemBackgr.Fill.Color := TAlphaColors.Lightgreen;
2 : itemBackgr.Fill.Color := TAlphaColors.Palegoldenrod;
3 : itemBackgr.Fill.Color := TAlphaColors.Lightcoral;
4 : itemBackgr.Fill.Color := TAlphaColors.Lightseagreen;
5 : itemBackgr.Fill.Color := TAlphaColors.Lightblue;
6 : itemBackgr.Fill.Color := TAlphaColors.Lightgrey;
end;
end;
//Empty item
if (StrToInt(jsonEmpty) = 1) or (StrToInt(jsonNull) = 1) then begin
startDetail[i] := False;
if Assigned(itemNumber) then itemNumber.Visible := False;
if Assigned(itemOccup) then itemOccup.Visible := False;
end
else begin
startDetail[i] := True;
end;
Inc(i);
end;
Thanks so much for your attention.非常感谢您的关注。
After some days and some tests, I have already found a solution to my problem.经过几天和一些测试,我已经找到了解决我的问题的方法。
I do not understand well why but there was some code lines that they were interfering with my custom style.我不太明白为什么,但有一些代码行干扰了我的自定义样式。
For example, when I put:例如,当我把:
//Item background
itemBackgr := item.FindStyleResource('background_item') as TRectangle;
if Assigned(itemBackgr) then begin
**itemBackgr.Fill.Kind := TBrushKind.Solid;**
...
After scroll, items changed order position and their color background.滚动后,项目更改了顺序位置及其颜色背景。 I have applied this property directly in the 'TRectangle' component in the custom style.
我已经直接在自定义样式的“TRectangle”组件中应用了这个属性。
Moreover, I changed the allocation of all elements to this way:此外,我将所有元素的分配更改为这种方式:
-Before I had: - 在我之前:
itemTitle := item.FindStyleResource('txtstyle_title') as TText;
if Assigned(itemTitle) then begin
itemTitle.Text := jsonTitle;
end;
-Now, I have: - 现在,我有:
item.StylesData['txtstyle_title'] := jsonTitle;
With these changes I get items do not change their position in ListBox neither background color after scroll.通过这些更改,我得到的项目在滚动后不会改变它们在 ListBox 中的位置,也不会改变背景颜色。
I had a problem yet, buttons do not show their background color and it was due to these lines:我还有一个问题,按钮没有显示它们的背景颜色,这是由于以下几行:
//Empty item
if (StrToInt(jsonEmpty) = 1) or (StrToInt(jsonNull) = 1) then begin
startDetail[i] := False;
**if Assigned(itemNumber) then itemNumber.Visible := False;**
**if Assigned(itemOccup) then itemOccup.Visible := False;**
end
else begin
startDetail[i] := True;
end;
Apparently, you cannot change visible property from item in 'FormCreate' method because when you do scroll some items elements change their property without control.显然,您无法在 'FormCreate' 方法中更改 item 的可见属性,因为当您滚动某些 item 元素时,它们会在没有控制的情况下更改它们的属性。 Therefore, I have done some modifications in my code instead of putting to false the visibility:
因此,我对代码进行了一些修改,而不是将可见性设为 false:
if (StrToInt(jsonEmpty) = 1) or (StrToInt(jsonNull) = 1) then begin
startDetail[i] := False;
item.StylesData['btt_number.Text'] := '';
item.StylesData['txt_occupation'] := '';
if (StrToInt(jsonEmpty) = 1) then item.StylesData['btt_number.TintObject.TintColor'] := TAlphaColors.White;
if (StrToInt(jsonNull) = 1) then item.StylesData['btt_number.TintObject.TintColor'] := TAlphaColors.Lightblue;
end
else begin
startDetail[i] := True;
item.StylesData['btt_number.Text'] := jsonNumber;
item.StylesData['txt_occupation'] := jsonOccup;
end;
At this form, I put the text ' ' (empty) and the background color at the same color that his item (TRectangle) in the elements who should to have the visible property to false.在这种形式中,我将文本“ ”(空)和背景颜色与他的项目(TRectangle)在元素中的颜色相同,该元素应该具有可见属性为false。
After all these changes I get the result that I wanted, that is to say, my items ListBox do not change when I scroll.在所有这些更改之后,我得到了我想要的结果,也就是说,当我滚动时,我的项目 ListBox 不会改变。 XD
XD
将表单上的样书更改为 Resourse1 对我有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.