Android Code Snippet [B4X] xCustomListView Pull To Refresh

Better implementation is available here: https://www.b4x.com/android/forum/t...tview-swipe-actions-and-pull-to-refresh.98252


You can use this code to add a pull to refresh feature to the standard xCustomListView.
The code should be added to the activity.
B4X:
Sub CLV_ScrollChanged (Offset As Int)
   ScrollIndex = ScrollIndex + 1
   Dim MyIndex As Int = ScrollIndex
   If TopPanel.Parent.IsInitialized Then
       If Offset < TopPanel.Height / 3 Then
           Refresh
       End If
       TopPanel.GetView(1).TextSize = 20 * (TopPanel.Height - Offset) / TopPanel.Height
   Else If Offset = 0 And TopPanel.Parent.IsInitialized = False Then
       Sleep(200)
       If MyIndex = ScrollIndex Then
           If TopPanel.Parent.IsInitialized = False Then
               CLV.InsertAt(0, TopPanel, "")
               TopPanel.GetView(1).Text = "Pull to refresh"
               CLV.sv.ScrollViewOffsetY = TopPanel.Height + CLV.DividerSize
           End If
       End If
   End If
End Sub

Private Sub StopRefresh
   Refreshing = False
   If TopPanel.Parent.IsInitialized Then
       TopPanel.RemoveViewFromParent
       CLV.RemoveAt(0)
       B4XLoadingIndicator1.Hide
   End If
End Sub

Private Sub Refresh
   If Refreshing Then Return
   Refreshing = True
   B4XLoadingIndicator1.Show
   TopPanel.GetView(1).Text = "Loading..."
   Log("Refresh")
   'refresh items...
   Sleep(2000)
   For i = CLV.Size - 1 To 1 Step -1
       CLV.RemoveAt(i)
   Next
   For i = 1 To 20
       CLV.AddTextItem($"Item #${Rnd(1, 1000)}"$, "")
   Next
   StopRefresh
End Sub

Project is attached.
 
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime User
Here is a modded version of the clv_ScrollChanged event:

B4X:
Sub CLV_ScrollChanged (Offset As Int)
    Dim hh As Int = TopPanel.Height
    If Offset < -30 Then
  
        If TopPanel.Parent.IsInitialized = False Then
            CLV.InsertAt(0, TopPanel, "loading_" & "-5")
            TopPanel.GetView(1).Text = "Pull to refresh"
            TopPanel.Color = Colors.RGB(42, 49, 55)
            CLV.sv.ScrollViewOffsetY = TopPanel.Height + CLV.DividerSize
      
        Else
          
            If TopPanel.Parent.IsInitialized Then
                If Offset < TopPanel.Height Then
                    Refresh
      
      
                End If
              
                TopPanel.GetView(1).TextSize = 20 * (TopPanel.Height - Offset) / TopPanel.Height
      
            End If
          
          
        End If
  
    Else If Offset > hh  Then
        If TopPanel.Parent.IsInitialized = True Then
        TopPanel.RemoveViewFromParent
        CLV.RemoveAt(0)
        refreshloading.Hide
      
    End If
    End If
  
End Sub

with this code, you dont need scroll the list first down to add the refresh layout.

I have test it in my IOS App and it works very well, thanks!
 
Last edited:

rscheel

Well-Known Member
Licensed User
Longtime User
Here is a modded version of the clv_ScrollChanged event:

B4X:
Sub CLV_ScrollChanged (Offset As Int)
    Dim hh As Int = TopPanel.Height
    If Offset < -30 Then
 
        If TopPanel.Parent.IsInitialized = False Then
            CLV.InsertAt(0, TopPanel, "loading_" & "-5")
            TopPanel.GetView(1).Text = "Pull to refresh"
            TopPanel.Color = Colors.RGB(42, 49, 55)
            CLV.sv.ScrollViewOffsetY = TopPanel.Height + CLV.DividerSize
     
        Else
         
            If TopPanel.Parent.IsInitialized Then
                If Offset < TopPanel.Height Then
                    Refresh
     
     
                End If
             
                TopPanel.GetView(1).TextSize = 20 * (TopPanel.Height - Offset) / TopPanel.Height
     
            End If
         
         
        End If
 
    Else If Offset > hh  Then
        If TopPanel.Parent.IsInitialized = True Then
        TopPanel.RemoveViewFromParent
        CLV.RemoveAt(0)
        refreshloading.Hide
     
    End If
    End If
 
End Sub

with this code, you dont need scroll the list first down to add the refresh layout.

I have test it in my IOS App and it works very well, thanks!

It does not work on android, specifically on android 8.1
 

David Meier

Active Member
Licensed User
Longtime User
As I understand xCustomListView is cross-plattform. Will thus pull to refresh also work in b4i?

Regards

David
 
Top