2016-07-06 82 views
1

我正在Firemonkey中開發一個多設備應用程序,其中Main class中有一些ListBox組件和一些項目。每個這些項目都具有相同的自定義樣式。ListBox項目在Firemonkey中滾動後發生變化

我的問題是,當我有這麼多的項目在列表框中,我必須做垂直滾動查看其餘項目。在這種情況下,ListBox有一個奇怪的行爲,當我向下滾動項目的組件(例如一個按鈕)時,它已經改變了他的背景顏色,並且項目已經改變了它在ListBox內的順序。

舉例來說,如果我有:

  1. 項目1
  2. 項目2
  3. 項目3

我做滾動後,我有:

  1. 項目2
  2. 商品3
  3. 項目1

這種變化是隨機的。每次都不一樣。

真實的例子(處理步驟):

  1. 加載主類,其中是列表框。

enter image description here

  • 待辦事項垂直向下滾動以查看物品的其餘部分。

  • 垂直向上滾動以返回列表頂部。

  • enter image description here

  • 的項目已經在列表框改變位置和按鈕(每個項目的成分)改變其背景顏色。
  • 爲什麼我在ListBox中有這種行爲?我怎樣才能解決它和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; 
    

    非常感謝您的關注。

    +0

    請您介紹儘可能簡單的重現步驟。你能在Android和Win平臺上重現這一點嗎? –

    +0

    @Kerem D我編輯了我的文章。 XD – KryNaC

    +0

    目前還沒有足夠的信息。我問你在哪個平臺上有這種行爲。你能用預定義的ItemStyle來重現它嗎?你能用更簡單的自定義風格重現它嗎? –

    回答

    1

    經過一些日子和一些測試,我已經找到了解決我的問題。

    我不明白爲什麼,但有一些代碼行,他們干擾我的自定義樣式。

    例如,當我把:

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

    滾動後,項目改變順序的位置和它們的顏色背景。我直接在自定義樣式的「TRectangle」組件中應用此屬性。

    此外,我改變了所有要素的分配是這樣的:

    -Before我:

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

    - 現在,我有:

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

    有了這些改變我獲取項目不會在ListBox中更改其位置,也不會在滾動後改變背景顏色。

    我有一個問題呢,按鈕不會顯示自己的背景顏色和它這些線是由於:

    //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」方法的項目,因爲當你做的Visible屬性滾動一些項目元素而不用控制就改變它們的屬性因此,我做的,而不是把假的知名度在我的代碼進行一些修改:

    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的元素。

    完成所有這些更改後,我得到了我想要的結果,也就是說,當我滾動時,我的項目列表框不會更改。 XD

    相關問題