Android Question Nested CustomListView and using PreoptimizedCLV

red30

Well-Known Member
Licensed User
Longtime User
I am creating a CustomListView "allclv" with a nested CustomListView "clvPhoto". In each clvPhoto I add 30 photos stored in DirInternal. This display of 5 of these CustomListView takes more than 20s, which is very long.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    For i=0 To 5
        allclv.Add(CreatePhoto(i),"")
        clvPhoto.AsView.Tag = clvPhoto
        Dim templist As List
        templist=File.ListFiles(File.DirInternal)
        For j=0 To templist.Size-1
            AddPhoto(templist.Get(j),i)
        Next
    Next
End Sub

Sub AddPhoto (Name As String, index As Int)
    If Name.Contains(".jpg") Then
        Dim pnl As B4XView = allclv.GetPanel(index)
        Dim clv As CustomListView = pnl.GetView(1).Tag

        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0, 0, clvPhoto.AsView.Width/4, clvPhoto.AsView.Height)
        p.LoadLayout("iv")
    
        FillImageToView(xui.LoadBitmapResize(File.DirInternal, Name, clvPhoto.AsView.Width/4, clvPhoto.AsView.Height, True),iv)
            
        clv.Add(p,"")
    End If
End Sub
I tried using PreoptimizedCLV, but I understand how to use it correctly.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    For i=0 To 5
        allclv.Add(CreatePhoto(i),"")
        clvPhoto.AsView.Tag = clvPhoto
        
        PCLV.Initialize(Me, "PCLV", clvPhoto)
        Dim templist As List
        templist=File.ListFiles(File.DirInternal)
        For j=0 To templist.Size-1
            AddPhoto(templist.Get(j))
        Next
    Next
    PCLV.Commit
End Sub

Sub AddPhoto (Name As String)
    If Name.Contains(".jpg") Then
        PCLV.AddItem(100dip, xui.Color_White, Name)
    End If
End Sub

Sub clvPhoto_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    For Each i As Int In PCLV.VisibleRangeChanged(FirstIndex, LastIndex)
        Dim item As CLVItem = clvPhoto.GetRawListItem(i)
        Dim pnl As B4XView = allclv.GetPanel(i)
        Dim clv As CustomListView = pnl.GetView(1).Tag
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0, 0, clvPhoto.AsView.Width/4, clvPhoto.AsView.Height)
        p.LoadLayout("iv")
        FillImageToView(xui.LoadBitmapResize(File.DirInternal, item, clvPhoto.AsView.Width/4, clvPhoto.AsView.Height, True),iv)
        clv.Add(p,"")
    Next
End Sub

What am I doing wrong? Should I use a separate PreoptimizedCLV for each of my clvPhotos? Or one PreoptimizedCLV for all clvPhotos?
 

red30

Well-Known Member
Licensed User
Longtime User
Are you testing it in release mode?
How many images are you loading at once?

It will probably be easier to use the VisibleRangeChanged event to implement lazy loading.
Yes I am testing in release mode.
30 images in each clvPhoto.

How do you use VisibleRangeChanged? When I scroll through the list, what happens to the images that are not visible? In any case, I first need to load these images somewhere, and then display only the visible ones from the position.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top