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

查看:113
本文介绍了在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.

实际示例(处理步骤):

Real example (process steps):

  1. 在MainBox中加载ListBox.

  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中有此行为?我该如何解决,列表框不更改项目顺序,也不改变其组件的背景色?

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'方法中更改项目的可见属性,因为在滚动某些项目元素时,它们的属性将不受控制地更改.因此,我在代码中做了一些修改,而不是假使可见性为假:

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)相同的颜色中,该元素应具有visible属性,该元素应为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不会更改. XD

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天全站免登陆