在 Firemonkey 中滚动后,ListBox 项目发生变化 [英] ListBox items change after doing scroll in Firemonkey

查看:23
本文介绍了在 Firemonkey 中滚动后,ListBox 项目发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Firemonkey 中开发一个多设备应用程序,其中 Main 类有一个带有一些项目的 ListBox 组件.这些项目中的每一个都具有相同的自定义样式.

I am developing a Multi-Device Application in Firemonkey where Main class has a ListBox component with some items. Each of these items has the same custom style.

我的问题是当我在 ListBox 中有这么多项目时,我必须垂直滚动才能看到其余项目.在这种情况下,ListBox 有一个奇怪的行为,当我在向下滚动后向上滚动时,项目的组件(例如按钮)已经改变了他的背景颜色并且项目已经改变了他在 ListBox 中的顺序.

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. 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.

例如,如果我有:

  1. 第 1 项
  2. 第 2 项
  3. 第 3 项

滚动后我有:

  1. 第 2 项
  2. 第 3 项
  3. 第 1 项

这种变化是随机的.每次都不一样.

This change is random. Each time is different.

实例(流程步骤):

  1. 加载 ListBox 所在的 Main 类.

  1. 垂直向下滚动以查看其余项目.

  1. Do vertical scrolling down to see the rest of items.

向上垂直滚动以返回列表顶部.

Do vertical scrolling upward to return to the top of the list.

  1. 项目在 ListBox 中的位置发生了变化,并且按钮(每个项目的组件)改变了它的背景颜色.

为什么我在 ListBox 中有这种行为?我该如何解决它并且 ListBox 不改变项目顺序也不改变其组件的背景颜色?

Why I have this behaviour in the 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 项的代码:

This is the code to create and initialize the ListBox items:

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;

非常感谢您的关注.

推荐答案

经过几天和一些测试,我已经找到了解决问题的方法.

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.

例如,当我输入:

//Item background
   itemBackgr := item.FindStyleResource('background_item') as TRectangle;
   if Assigned(itemBackgr) then begin
     **itemBackgr.Fill.Kind := TBrushKind.Solid;**
     ...

滚动后,项目更改了订单位置及其颜色背景.我已经在自定义样式的TRectangle"组件中直接应用了这个属性.

After scroll, items changed order position and their color background. I have applied this property directly in the 'TRectangle' component in the custom style.

另外,我把所有元素的分配改成这样:

Moreover, I changed the allocation of all elements to this way:

-之前我有:

   itemTitle := item.FindStyleResource('txtstyle_title') as TText;
   if Assigned(itemTitle) then begin
     itemTitle.Text := jsonTitle;
   end;

-现在,我有:

   item.StylesData['txtstyle_title'] := jsonTitle;

通过这些更改,我得到的项目在滚动后不会改变它们在 ListBox 中的位置,也不会改变背景颜色.

With these changes I get items do not change their position in ListBox neither background color after scroll.

我遇到了一个问题,按钮没有显示它们的背景颜色,这是由于这些行:

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;

显然,您无法在FormCreate"方法中更改项目的可见属性,因为当您滚动某些项目时,某些元素会在不受控制的情况下更改其属性.因此,我对我的代码进行了一些修改,而不是将可见性设置为 false:

Apparently, you cannot change visible property from item in 'FormCreate' method because when you do scroll some items elements change their property without control. Therefore, I have done some modifications in my code instead of putting to false the visibility:

    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;

在这个表单中,我将文本''(空)和背景颜色放在与他的项目(TRectangle)相同的颜色中,该元素应该具有可见属性为false.

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.

在所有这些更改之后,我得到了我想要的结果,也就是说,我的项目 ListBox 在我滚动时不会改变.呵呵

After all these changes I get the result that I wanted, that is to say, my items ListBox do not change when I scroll. XD

这篇关于在 Firemonkey 中滚动后,ListBox 项目发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆