Android Question WebChromeClient pull to refresh [solved]

eps

Expert
Licensed User
Has anyone managed to get this to work?

I'm using @warwound webviewextras2 library and looked at the Flingable web view as well.

I can detect a fling, but I want to be able to detect a pull, when at the top of a given web page, such as found in the standard Android web browser..

Is the use of a separate library necessary? As it stands the ChromeClient 'knows' or detects a pull from the top or bottom - it indicates this visually, but does 'nothing' with it..

I can't help feeling that I'm missing something obvious here.. please help or point me in the right direction.

Cheers!
 

eps

Expert
Licensed User
Okay.... so this almost works now :) Thanks to the help from the Library Thread!

But I still can't get it to detect a pull whilst at the top of the web view - e.g. an 'over scroll' - can anyone point me in the right direction to work out how to do this please?

Code is as follows...

B4X:
Sub Globals

    'These global variables will be redeclared each time the activity is created.

    'These variables can only be accessed from this module.


    Dim wv AsWebView

    Dim WebChromeClient1 AsDefaultWebChromeClient

    Dim wve AsWebViewExtras

    Dim wvs AsWebViewSettings

    Private ime1 AsIME


    Private ActivityParent As JavaObject

    Dim VP AsAHViewPager

    Dim PC AsAHPageContainer

    Dim STR AsAHSwipeToRefresh

    Dim LVs(4) As ListView 

    Dim PageRefreshing As Int

EndSub


Sub Activity_Create(FirstTime As Boolean)

    STR.Initialize("STR")

    Activity.AddView(STR, 0, 0, 100%x, 100%y)


    STR.SetColorScheme2(Array As Int(Colors.Red, Colors.Green, Colors.Blue, Colors.Magenta, Colors.Cyan, Colors.Yellow))

    Dim url_selected = "http://www.bbc.co.uk/" As String

    wv.Initialize("wv")

    wve.Initialize(wv)

    WebChromeClient1.Initialize("WebChromeClient1")

    wve.SetWebChromeClient(WebChromeClient1)

    wve.LoadUrl(url_selected)


    STR.AddView(wve)
   

    'PC.AddPage(wv,"Test bbc")

EndSub


Sub Activity_Resume

EndSub


Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub STR_Refresh

    Log("Refresh started for Page " & VP.CurrentPage)


    'Start the timer. Normally you will start your asynchronous job here

    tm.Initialize("Timer", 3000)

    tm.Enabled = True
   

    'Disable the STR object so we can not start a new refresh process

    STR.Enabled = False


    'We have to remember which page caused the refreshing

    PageRefreshing = VP.CurrentPage

End Sub


'The timer tick simulates the end of the refreshing process

Sub Timer_Tick

    Log("Refresh stopped for Page " & PageRefreshing)

   
    'Stop Timer and refresh the listview data

    tm.Enabled = False

    RefreshListView(PageRefreshing, LVs(PageRefreshing))


    'Stop the spinning disc and enable the STR object again.

    STR.Refreshing = False

    STR.Enabled = True

End Sub


Sub RefreshListView(page As Int, lv As ListView)

    lv.Clear

    For i = 0 To 100

        lv.AddTwoLines("Page: " & page & " - Item: " & i, "Value: " & Rnd(0, 100))

    Next

End Sub


' The STR object "asks" if the refresh should be triggered.

Sub STR_CanChildScrollUp As Boolean

    'Is the Listview of the current page at top. If yes, we can swipe to refresh, otherwise we have to scroll up first.

    Dim jo AsJavaObject

    Dim pos, offset As Int


    'We are not at the top of the LV. So the LV can scroll up further and we return True here.

    Return True

End Sub

So 'something' needs to go in the STR_CanChildScrollUp sub I've looked at Reflector and some of the over scroll api calls but I can't seem to work out what to do. As per most of the scroll or fling methods - detecting a scroll or fling isn't the problem it's getting the context of the scroll or fling - e.g. at the top of a webpage to refresh the page...

Has anyone completed that part or got any ideas please?
 
Upvote 0

eps

Expert
Licensed User
Job done, phew!

:)

B4X:
Sub STR_CanChildScrollUp As Boolean

    'Is the Listview of the current page at top. If yes, we can swipe to refresh, otherwise we have to scroll up first.

    Dim jo AsJavaObject

    Dim pos, offset As Int


    pos = RunMethod(wve,"getScrollY",Null)


    If pos = 0 Then

        ReturnFalse

    EndIf


    'We are not at the top of the LV. So the LV can scroll up further and we return True here.

    ReturnTrue

End Sub

Sub RunMethod(n As JavaObject,method As String,args() As Object) As Object

    Return n.RunMethod(method,args)

End Sub

Added a RunMethod sub - to ease the pain of calling run methods and then called getscrollY from the webviewextra and if it's zero, return false to call the Refresh STR subroutine and then load the current url again.

:)
 
Upvote 0

eps

Expert
Licensed User
This is all a little simplified from the end result I want to achieve but hopefully will give others the barebones code to carry this out as well for themselves. I am going to tidy some of it up as well and adjust some of the variables and so on.
 
Upvote 0
Top