Android Question [B4X][XUI] ImageSlider - Click event

Discussion in 'Android Questions' started by Christian García S., Oct 3, 2018.

  1. Christian García S.

    Christian García S. Active Member Licensed User

    Hello,

    I implemented CustomListView with multiple ImageSliders (https://www.b4x.com/android/forum/threads/b4x-customlistview-with-multiple-imagesliders.97219/), based on this library (https://www.b4x.com/android/forum/threads/b4x-xui-imageslider.87128/page-2#post-616684) and it works great.

    I am with an application that needs to show several promotional items in the slider, and I would like it when I click on these items to go to the description of each one for purchase, at this moment as I saw it there is only one event (ItemClick) in custom list view, but when you click on the middle of the slider it does not do anything.

    Can you please help us with click event?

    Thanks
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The touch event is handled here:
    Code:
    Private Sub WindowBase_Touch (Action As Int, X As Float, Y As Float)
       
    Dim p As Panel = WindowBase
       
    If Action = p.ACTION_DOWN Then
           MousePressedX = X
       
    Else If Action = p.ACTION_UP Then
           CheckTouchGesture(X)
       
    End If
    End Sub
    You can change CheckTouchGesture to:
    Code:
    Private Sub CheckTouchGesture(EndX As Float) As Boolean
       
    If EndX > MousePressedX + 50dip Then
           PrevImage
           
    Return True
       
    Else if EndX < MousePressedX - 50dip Then
           NextImage
           
    Return True
       
    End If
     
    Return False
    End Sub
    This will allow you to check whether there was a swipe gesture or not. If not then you can raise a "click" event with CallSub.
     
  3. Christian García S.

    Christian García S. Active Member Licensed User

    Thanks @Erel,

    I implemented it like this:

    Code:
    Public Sub GetIndex As Int
        
    Return CurrentIndex
    End Sub

    Private Sub WindowBase_Touch (Action As Int, X As Float, Y As Float)
        
    Dim p As Panel = WindowBase
        
    Dim res As Boolean
        
    If Action = p.ACTION_DOWN Then
            MousePressedX = X
        
    Else If Action = p.ACTION_UP Then
            res = CheckTouchGesture(X)
            
    If res= False Then
                
    CallSub(act_principal,"imgSlider_Click")
            
    End If
        
    End If
    End Sub
    and in principal activity I have

    Code:
    Sub imgSlider_Click
        
    Log ("Click" & "Index: " & imgSlider.GetIndex & "Tag: " & imgSlider.Tag )
    '
    End Sub
    Click event works, but now, I do not know how to get in which row the CustomListView I am or the Currentindex of the slider.

    I need to know row and column like in CustomListView

    Code:
    Sub clvcards_ItemClick (Index As Int, Value As Object)
        
    Dim si As SliderAndImages = Value
        si.Slider.NextImage
        
        
    Log("Card Index: " & Index &" Slider Index: " & si.Slider.GetIndex)
    End Sub
    Thanks
     
    Last edited: Oct 5, 2018
  4. Erel

    Erel Administrator Staff Member Licensed User

    The Sender keyword will return the ImageSlider instance that raised the event.
     
  5. Christian García S.

    Christian García S. Active Member Licensed User

    Thanks @Erel

    Now the code works and is the following (class ImageSlider):

    Code:
    #if B4J
    Private Sub WindowBase_MousePressed (EventData As MouseEvent)
        MousePressedX = EventData.X
    End Sub

    Private Sub WindowBase_MouseReleased (EventData As MouseEvent)
        CheckTouchGesture(EventData.X)
    End Sub
    #else
    Private Sub WindowBase_Touch (Action As Int, X As Float, Y As Float)
        Dim p As Panel = WindowBase
        Dim res As Boolean
        If Action = p.ACTION_DOWN Then
            MousePressedX = X
        Else If Action = p.ACTION_UP Then
            res = CheckTouchGesture(X)
            If res= False Then
                Click
            End If
        End If
    End Sub
    #End If

    Private Sub Click
        
    CallSub2(act_principal,"imgSlider_Click",Me)
    End Sub
    and in activity act_principal is:

    Code:
    Sub imgSlider_Click(SenderK As Object)
        
    Dim slider As ImageSlider = SenderK
        
    Dim Index As Int = slider.GetIndex + 1
     
        
    Log ("Click" & " CurrentRow: " & slider.CurrentRow & " CurrentIndex: " & Index )
    End Sub
    I had to send the object Me, because when I sent Sender it was another object that was in WindowBase_Touch.

    Thanks
     
  6. Christian García S.

    Christian García S. Active Member Licensed User

    You can help me with something, what you load Slider, the images start from index 1, but should start from 0.

    I think it's because after loading the object, you immediately call the NextImage event.

    How can I do to load the index (0) ?

    Thanks for your help.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You should start a new thread for a new question.
     
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