Android Question Proper way to download and lazy load images in CustomListView

Ferdari

Active Member
Licensed User
Longtime User
Hi everyone i have a custom listview that loads items from the web, im using the VisibleRangeChanged to download the images and show in a CustomListView:
B4X:
Sub CLV1_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Dim ExtraSize As Int = 3
    For i = 0 To CLV1.Size - 1
        Dim p As B4XView = CLV1.GetPanel(i)
        If i > FirstIndex - ExtraSize And i < LastIndex + ExtraSize Then
            'visible+
            If p.NumberOfViews = 0 Then
                Dim cd As CardData = CLV1.GetValue(i)
                p.LoadLayout("PackBox")
                lblTitle.Text = cd.Title
                lblCreator.Text = cd.Creator
                'SetColorStateList(AddBtn, xui.Color_LightGray, AddBtn.TextColor)
                'SetColorStateList(lblAction2, xui.Color_LightGray, lblAction2.TextColor)
              
                'Download Image Preview
                If File.Exists(File.DirInternalCache, cd.BitmapFile) = False Then
                    Dim j As HttpJob
                    j.Initialize("", Me) 'name is empty as it is no longer needed
                    j.Download("https://server.com/"& cd.BitmapFile) 'changed to some server for the example, not the server problem.
                    Log("Descargando IMG: "& cd.BitmapFile)
                    Wait For (j) JobDone(j As HttpJob)
                    If j.Success Then
                        Log("Descargado "& cd.BitmapFile)
                        Dim out As OutputStream = File.OpenOutput(File.DirInternalCache, cd.BitmapFile, False)
                        File.Copy2(j.GetInputStream, out)
                        out.Close '<------ very important
                        'ImageView1.SetBitmap(j.GetBitmap)
                        Dim img As ImageView
                        img.Initialize("Img")
                        img.Bitmap=LoadBitmapResize(File.DirInternalCache,cd.BitmapFile,ImageView1.Width,ImageView1.Height,True)
                        Panel1.AddView(img,ImageView1.Left,ImageView1.Top,ImageView1.Width,ImageView1.Height)
                        img.BringToFront
                      

                        Log(cd.Filename.SubString2(0,cd.BitmapFile.LastIndexof(".")))
                        AddBtn.Tag=cd.Filename
                        ImageView1.Visible=False
                        ImageView1.Enabled=False
                    End If
                    j.Release
                Else
                    'ImageView1.Bitmap=LoadBitmapResize(File.DirInternalCache,cd.BitmapFile,ImageView1.Width,ImageView1.Height,True)
                    Dim img As ImageView
                    img.Initialize("ImgPack")
                    img.Bitmap=LoadBitmapResize(File.DirInternalCache,cd.BitmapFile,ImageView1.Width,ImageView1.Height,True)
                    Panel1.AddView(img,ImageView1.Left,ImageView1.Top,ImageView1.Width,ImageView1.Height)
                    img.BringToFront
                End If
            End If
        Else
            'not visible
            If p.NumberOfViews > 0 Then
                p.RemoveAllViews
            End If
        End If
    Next
End Sub
it downloads ok, but are not showing/adding the image if items are visible, the images appears only when i scroll down and return back to the items downloaded,,
i want it to add the image and show when it are downloaded.

Im generating the list this way:
B4X:
Sub DownloadData
    Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download("https://server.com/content.json") 'Not server problem, example server.
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        wjson = j.GetString
    End If
    j.Release
   
    Dim json As JSONParser
    json.Initialize(wjson)
   
    Dim wpacks As List = json.NextArray
    Dim i As Int = 0
   
   
    For Each WPack As Map In wpacks
       
        'Log("Pack "& i)
        Dim PackId As Int = i
        i = i+1
   
        'Log("populating names..")
        Dim cd As CardData
        cd.Initialize
        cd.Title = WPack.Get("title")
        Log(cd.Title)
        cd.Creator = WPack.Get("author")
        cd.Filename =  WPack.Get("filename")
        Log("Pre Filename: "&cd.Filename)
        cd.BitmapFile = WPack.Get("preview-image")
        'cd.BitmapFile = bitmaps.Get((i - 1) Mod bitmaps.Size)
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0, 0, CLV1.AsView.Width, 190dip)
        CLV1.Add(p, cd)
           
    Next
End Sub

What is the best way to accomplish this function?

Thanks :D
 
Last edited:

Ferdari

Active Member
Licensed User
Longtime User
Sounds like a big mistake.
aye, i modified it when working on the huawei libraries, it needed a newer version of the dependencies, now i changed the okhttp with the new ones so the huawei libs works too.
 
Upvote 0
Top