ListBox items change after doing scroll in Firemonkey

*爱你&永不变心* 提交于 2019-12-01 12:54:14

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.

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.

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!