Android Example [B4X] AS ViewPager - Infinite scrolling


A small example project on how to achieve endless scrolling in both directions with the as viewpager.

Important
You need AS ViewPager V2.01+
and for B4J, you need V1.02+ of the jPager

AS ViewPager - Infinite scrolling.gif
AS ViewPager - Infinite scrolling.gif

The best thing is, it works in both directions

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("frm_main")
  
    ASViewPager1.LazyLoading = True
    ASViewPager1.LazyLoadingExtraSize = 4
  
    For i = 0 To 20
        Dim tmp_xpnl As B4XView = xui.CreatePanel("tmp_xpnl")
        tmp_xpnl.Color = xui.Color_ARGB(255,Rnd(1,255),Rnd(1,255),Rnd(1,255))
        tmp_xpnl.SetLayoutAnimated(0,0,0,ASViewPager1.Base.Width,ASViewPager1.Base.Height)
      
        ASViewPager1.AddPage(tmp_xpnl,"Test" & i)
    Next
    #IF B4A
    Sleep(250)
    #Else
    Sleep(0)
    #End If
    ASViewPager1.CurrentIndex = 10
    ASViewPager1.Commit
    B4XPages.SetTitle(Me,"Page " & (ASViewPager1.CurrentIndex +1) & "/" & ASViewPager1.Size)
End Sub

Private Sub ASViewPager1_PageChanged2(NewIndex As Int, OldIndex As Int)
    If NewIndex <= OldIndex Then
        If NewIndex <= 2 Then
            Dim tmp_xpnl As B4XView = xui.CreatePanel("tmp_xpnl")
            tmp_xpnl.Color = xui.Color_ARGB(255,Rnd(1,255),Rnd(1,255),Rnd(1,255))
            tmp_xpnl.SetLayoutAnimated(0,0,0,ASViewPager1.Base.Width,ASViewPager1.Base.Height)
      
            ASViewPager1.AddPageAt(0,tmp_xpnl,"")
        End If
    Else
        If NewIndex = ASViewPager1.Size -2 Then
            Dim tmp_xpnl As B4XView = xui.CreatePanel("tmp_xpnl")
            tmp_xpnl.Color = xui.Color_ARGB(255,Rnd(1,255),Rnd(1,255),Rnd(1,255))
            tmp_xpnl.SetLayoutAnimated(0,0,0,ASViewPager1.Base.Width,ASViewPager1.Base.Height)
      
            ASViewPager1.AddPage(tmp_xpnl,"")
        End If
    End If
    Sleep(0)
    B4XPages.SetTitle(Me,"Page " & (NewIndex +1) & "/" & ASViewPager1.Size)
End Sub

Private Sub ASViewPager1_LazyLoadingAddContent(Parent As B4XView, Value As Object)
  
    Dim xlbl As B4XView = CreateLabel
    xlbl.Text = "Lazyloading Label"
    xlbl.TextColor = xui.Color_White
    xlbl.Font = xui.CreateDefaultBoldFont(20)
    xlbl.SetTextAlignment("CENTER","CENTER")
    Parent.AddView(xlbl,0,Parent.Height/2 - 50dip/2,Parent.Width,50dip)
  
End Sub

Private Sub CreateLabel As B4XView
    Dim lbl As Label
    lbl.Initialize("")
    Return lbl
End Sub

Have Fun :)
 

Attachments

  • AS ViewPager Infinite scrolling Example.zip
    11.5 KB · Views: 276
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime User
I will change all views that I have created with the AS ViewPager to this feature in the next few days. This will drastically improve loading times and performance.
 
Top