Problem with ListView ItemHeight

Discorez

Member
Licensed User
Longtime User
Hi All!
I need dynamically update ListView ItemHeight which depends from SecondLabel height (lines count), but not all items receive the new size.
Screenshots and demo project is attached.
Where troubles?
:sign0085:
Code:
B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim EditText1 As EditText
   Dim LV As ListView
   Dim Button1 As Button
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
   Dim Count, i As Int
   LV.Clear
   SetItemHeight(EditText1.Text)
   For i = 0 To 50
      LV.AddTwoLines("Header " & i, SetLines(EditText1.Text))
   Next
End Sub


Sub SetLines(Count As Int) As String
   Dim i As Int
   Dim SB As StringBuilder

   SB.Initialize
   For i = 1 To Count
      If i <> Count Then SB.Append("Line " & i & CRLF) Else SB.Append("Line " & i)
   Next
   Return SB.ToString
End Sub

Sub SetItemHeight(Lines As Int)
   Dim C As Canvas
   
    C.Initialize(LV)
   LV.TwoLinesLayout.Label.Height = 2 * C.MeasureStringHeight("W", Typeface.DEFAULT, LV.TwoLinesLayout.Label.TextSize)
   LV.TwoLinesLayout.SecondLabel.Height = 2 * Lines * C.MeasureStringHeight("W", Typeface.DEFAULT, LV.TwoLinesLayout.SecondLabel.TextSize)
   LV.TwoLinesLayout.ItemHeight = (LV.TwoLinesLayout.Label.Height + LV.TwoLinesLayout.SecondLabel.Height)
   Label1.Text = "Items height: " & LV.TwoLinesLayout.ItemHeight
End Sub
 

Attachments

  • 1.png
    1.png
    34.4 KB · Views: 397
  • 2.png
    2.png
    34.4 KB · Views: 411
  • Test.zip
    6.9 KB · Views: 283
Last edited:

Discorez

Member
Licensed User
Longtime User
That won't solve your "Refresh" problem, but here's a perfect computation of the itemheight (tested with different number of lines and different devices):
Results for 3 and 7 lines (scrshots) is attached.


By the way, what's your hardware and Android version?
Hardware - Explay Informer 702 (Android v4.0.3) & Emulator (Android v2.3)
 

Attachments

  • 1.png
    1.png
    34.7 KB · Views: 304
  • 2.png
    2.png
    35.3 KB · Views: 273
Upvote 0

Informatix

Expert
Licensed User
Longtime User

By looking at your screenshots, I see that the second label height is correctly computed but the item height not. If you look at the formula, you see there are only two culprits: "SecondLabel.Top" or "Margin". Since the first one isn't modified by the sub and the second one is a constant (5dips), your screenshots are totally incomprehensible. I give up. Sorry.
 
Upvote 0
Top