Android Question Custom PageChangeListener_Event Parameters

Discussion in 'Android Questions' started by DawningTruth, Mar 11, 2019.

  1. DawningTruth

    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.

    Code:
    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:

    [​IMG]

    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:

    Code:
    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
     
  2. agraham

    agraham Expert Licensed User

    DawningTruth likes this.
  3. DawningTruth

    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:

    Code:
    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:

    [​IMG]
     
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