iOS Question [B4X] CLVSwipe PullToRefresh not working

Mike1970

Well-Known Member
Licensed User
Longtime User
hi everyone, i'm trying to use this class: https://www.b4x.com/android/forum/t...view-swipe-actions-and-pull-to-refresh.98252/
to add the pulltorefresh functionallity to my customlistview, but i noticed that if the number of elements in the list does not fill the entire screen, the PullToRefresh does not work :(

To replicate the error just download the example in that post and change the CreateItems sub, decreasing the number of element to 3 or 4 for example.

B4X:
Sub CreateItems
    Dim cs As CSBuilder
    For i = 1 To 4
        cs.Initialize.Color(Rnd(0xff000000, -1))
        If i Mod 3 = 0 Then
            cs.Append($"Important item ${i} ..."$).PopAll
            CustomListView1.AddTextItem(cs, Swipe.CreateItemValue("", Array("Delete", "Do Something Else")))
        Else If i Mod 3 = 1 Then
            cs.Append($"Very important item ${i} ..."$).PopAll
            CustomListView1.AddTextItem(cs, Swipe.CreateItemValue("", Array("Action 1", "Action 2", "Action 3")))
        Else
            cs.Append($"Nothing to see here ${i}"$).PopAll
            CustomListView1.AddTextItem(cs, Null)
        End If
    Next
End Sub

Then if you try to swipe down nothing happends :(

P.S. The B4A Version works
 
Last edited:

Mike1970

Well-Known Member
Licensed User
Longtime User
This is the expected behavior as this feature relies on the bounce effect. You can use XUIViewsUtils.AddStubItemIfNeeded (something like that) to add a stub item at the end of the CLV if needed.
Ok Erel, Thanks.
Doing like so it works as expected :D

B4X:
Sub CreateItems
    Dim cs As CSBuilder
    For i = 1 To 5
        cs.Initialize.Color(Rnd(0xff000000, -1))
        If i Mod 3 = 0 Then
            cs.Append($"Important item ${i} ..."$).PopAll
            CustomListView1.AddTextItem(cs, Swipe.CreateItemValue("", Array("Delete", "Do Something Else")))
        Else If i Mod 3 = 1 Then
            cs.Append($"Very important item ${i} ..."$).PopAll
            CustomListView1.AddTextItem(cs, Swipe.CreateItemValue("", Array("Action 1", "Action 2", "Action 3")))
        Else
            cs.Append($"Nothing to see here ${i}"$).PopAll
            CustomListView1.AddTextItem(cs, Null)
        End If
    Next
    XUIViewsUtils.AddStubToCLVIfNeeded(CustomListView1, CustomListView1.DefaultTextBackgroundColor)
End Sub
 
Upvote 0

mike2ppc

Member
Licensed User
Longtime User
Hi,
I've tried this command out, but it works only once, second time no pull down of clv possible. Do you have any ideas of this behavior ?
Best Regards
Michael

B4X:
Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private xui As XUI
    Private swipe As CLVSwipe
    Private lblPullToRefresh As B4XView
    Private ProgressBar1 As B4XView
    Private clv1 As CustomListView
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    swipe.Initialize(clv1, Me, "swipe")
    swipe.ActionColors = CreateMap("Delete": xui.Color_Red, "Do Something Else": xui.Color_Green, _
        "Action 1": xui.Color_Red, "Action 2": xui.Color_Blue, "Action 3": xui.Color_Yellow)
    Dim PullToRefreshPanel As B4XView = xui.CreatePanel("")
    PullToRefreshPanel.SetLayoutAnimated(0, 0, 0, 100%x, 70dip)
    PullToRefreshPanel.LoadLayout("PullToRefresh")
    swipe.PullToRefreshPanel = PullToRefreshPanel
    NavControl.ShowPage(Page1)
    fillclv
End Sub

Sub fillclv
    clv1.AddTextItem("Row 1", swipe.CreateItemValue("", Array("Delete", "Do Something Else")))
    clv1.AddTextItem("Row 2", swipe.CreateItemValue("", Array("Delete", "Do Something Else")))
    clv1.AddTextItem("Row 3", swipe.CreateItemValue("", Array("Delete", "Do Something Else")))
    clv1.Add(clv1_CreateItem,swipe.CreateItemValue("", Array("Delete", "Duplizieren")))
    clv1.Add(clv1_CreateItem,swipe.CreateItemValue("", Array("Delete", "Duplizieren")))
    XUIViewsUtils.AddStubToCLVIfNeeded(clv1, Colors.White)
End Sub

Sub clv1_CreateItem() As B4XView
    Dim varPanel As B4XView = xui.CreatePanel(""), varColor As Int
    varPanel.LoadLayout("line")
    varPanel.Height = 80
    Return varPanel
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)

End Sub


Sub clv1_ItemClick (Index As Int, Value As Object)
    swipe.CloseLastSwiped
End Sub

Sub clv1_ScrollChanged (Offset As Int)
    If xui.IsB4i Then
        swipe.ScrollChanged(Offset)
    End If
    swipe.CloseLastSwiped
End Sub


Sub swipe_ActionClicked (Index As Int, ActionText As String)
    Log($"Action clicked: ${Index}, ${ActionText}"$)
    If ActionText = "Delete" Then
        clv1.RemoveAt(Index)
    Else If ActionText = "Do Something Else" Then
        Dim p As B4XView = clv1.GetPanel(Index)
        Dim lbl As B4XView = p.GetView(0)
        lbl.Text = "Done!!!"
    End If
End Sub

Sub swipe_RefreshRequested
    lblPullToRefresh.Text = "Loading..."
    ProgressBar1.Visible = True
    'example!!!
    Sleep(3000)
    clv1.Clear
    fillclv
    swipe.RefreshCompleted '<-- call to exit refresh mode
    lblPullToRefresh.Text = "Pull to refresh"
    ProgressBar1.Visible = False
End Sub
 

Attachments

  • testSwipe.zip
    174.4 KB · Views: 194
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
AddStubToCLVIfNeeded tries to make the stub item fill the available space. In this case we need to make it a bit taller:
B4X:
Dim ClvSize As Int = clv1.Size
XUIViewsUtils.AddStubToCLVIfNeeded(clv1, Colors.Blue)
If clv1.Size > ClvSize Then
    clv1.ResizeItem(clv1.Size - 1, clv1.GetPanel(clv1.Size - 1).Height + 20dip)
End If
 
Upvote 0
Top