Android Code Snippet [B4X] xCustomListView Pull To Refresh

Discussion in 'Code Snippets' started by Erel, Jun 11, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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.
    Code:
    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(11000)}"$"")
       
    Next
       StopRefresh
    End Sub
    Project is attached.
     

    Attached Files:

    Last edited: Oct 16, 2018
  2. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    Here is a modded version of the clv_ScrollChanged event:

    Code:
    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(424955)
                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: Jun 11, 2018
    Claudio Oliveira and Erel like this.
  3. astronald

    astronald Member Licensed User

    I add this line
    Code:
    CLV_ScrollChanged (0)
    after fill list and works properly
     
  4. rscheel

    rscheel Well-Known Member Licensed User

    It does not work on android, specifically on android 8.1
     
  5. David Meier

    David Meier Member Licensed User

    As I understand xCustomListView is cross-plattform. Will thus pull to refresh also work in b4i?

    Regards

    David
     
  6. Alexander Stolte

    Alexander Stolte Well-Known Member Licensed User

    Yes. [B4X] Tag = Cross Plattform B4I,B4a and B4J.
     
    Erel and David Meier like this.
  7. David Meier

    David Meier Member Licensed User

    Thank you!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice