iOS Tutorial Add UIRefreshControl To CustomListView

Original Tutorial by @narek adonts

Create a standard class module (Let assume the name of the class is UIRefreshControl),
B4X:
Sub Class_Globals
    Private CSB As CSBuilder
    Private cb As Object
    Private en As String
    Private RefreshControl As NativeObject
End Sub

Public Sub Initialize(callback As Object, EventName As String, sv As ScrollView)
    cb = callback
    en=EventName
    Dim no As NativeObject=Me
    RefreshControl = no.RunMethod("AddRefresh:",Array(sv))
    RefreshControl.SetField("attributedTitle",CSB.Initialize.Append("Pull to refresh").PopAll) 'You can add AttributedString
End Sub

Public Sub EndRefreshing
    RefreshControl.RunMethod("endRefreshing",Null)
    RefreshControl.SetField("attributedTitle",CSB.Initialize.Append("Pull to refresh").PopAll) 'You can add AttributedString
End Sub

Private Sub RC_Refresh(RC As Object)
    RefreshControl.SetField("attributedTitle",CSB.Initialize.Append("Refreshing...").PopAll) 'You can add AttributedString
    CallSub(cb, en&"_PullRefresh")
End Sub

#If OBJC
-(UIRefreshControl *)AddRefresh: (UIScrollView*)scrollView {
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [scrollView addSubview:refreshControl];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    return refreshControl;
}

- (void)refresh:(id)sender {
    UIRefreshControl *refreshControl=sender;
    [self.bi raiseEvent:nil event:@"rc_refresh:" params:@[(refreshControl)]];

}
#end if

Now you can use this class to add refresh control to any CLV of any page you want. like this,
B4X:
'Code module

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public MainPage As Page
    Private MainPageScroll As CustomListView
    Private MainRC As UIRefreshControl
End Sub

Public Sub ShowModule
    MainPage.Initialize("MainPage")
    Main.NavControl.ShowPage(MainPage)
    MainPage.RootPanel.LoadLayout("layout1")

    MainRC.Initialize(Me,"MainPageScroll",MainPageScroll.sv) 'add refresh control to CLV scrollview
End Sub

Sub MainPageScroll_PullRefresh
    Sleep(3000) 'do whatever you want
    MainRC.EndRefreshing
End Sub

Happy Coding
 
Last edited:

webhost.company

Active Member
Licensed User
Longtime User
There is a bad problem
When I have only two items in CLV, the refreshcontrol not working
I understood when clv inner panel height is lower than clv height, refreshcontrol not working
I try to add the fake items to CLV and good working but that is not a good solution
Can I change the default inner panel height to max value?
 

webhost.company

Active Member
Licensed User
Longtime User
My problem was solved
Use below code:
B4X:
    sv.Bounces = True
    Dim no As NativeObject = sv
    no.RunMethod("setAlwaysBounceVertical:", Array(True))
 

webhost.company

Active Member
Licensed User
Longtime User
Also you can change activityindicator color with below code:
B4X:
Dim no As NativeObject = RefreshControl
 no.RunMethod("setTintColor:", Array(x.ColorToUIColor(Color)))
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…