Android Question Custom PageChangeListener_Event Parameters

DawningTruth

Active Member
Licensed User
With reference to this post:

https://www.b4x.com/android/forum/threads/tabstrip-access-events.80845/#post-512196

I have tried to get the sample code working on the demo TabStripViewPager app.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    Activity.LoadLayout("Main")
   
    TabStrip1.LoadLayout("Page1", "")
    TabStrip1.LoadLayout("Page2", "")
    TabStrip1.LoadLayout("Page3", "")

    For i = 1 To 100
        Page3ListView1.AddSingleLine($"Item ${i}"$)
    Next
   
    Dim jo As JavaObject = TabStrip1
    Dim event As Object =  jo.CreateEventFromUI("android.support.v4.view.ViewPager.OnPageChangeListener", "PageChangeListener", Null)
    jo.GetFieldJO("tabStrip").RunMethod("setOnPageChangeListener", Array(event))

End Sub

Sub TabStrip1_PageSelected (Position As Int)
    Log($"Current page: ${Position}"$)
End Sub

Sub PageChangeListener_Event (MethodName As String, Args() As Object) As Object
    Log("Method: " & MethodName)
    Dim ArgsList As List = Args
    Log("Args: " & ArgsList)
   
    Return Null
End Sub
I am finding that I am getting a whole lot of Args, but:

1. One movement of the swipe is generating a large number of events. Screenshot below:



2. I don't know which of these events gives me the ID for the current Activated Tab.

Clearly there is something missing in my understanding of the code.

Can you please advise how I can get the ID?

I would envision an algorithm like this:

B4X:
PseudoCode:

1. User Swipes to New Tab
2. PageChangeListener Event Fires
3. PageChangeListener Event Handler provides:
    - PageID that was swipped from
   - PageID that was sipped to
4. Handle actions accordingly
 

DawningTruth

Active Member
Licensed User
Thx, after further tinkering with the code, going through the logs and rereading x 10 the following note:

Note that the PageSelected event will not be raised. PageChangeListener_Event will be raised instead with method = "onPageSelected"

I managed to get it working.

Here is the updated code that works:

B4X:
Sub PageChangeListener_Event (MethodName As String, Args() As Object) As Object
    
    If MethodName = "onPageSelected" Then
        Log("Method: " & MethodName)
        Dim ArgsList As List = Args
        Log ("Index: "&ArgsList.Get(0))
    End If
    
    Return Null
End Sub
Which gives output:

 
Top