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

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
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The touch event is handled here:
B4X:
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:
B4X:
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.
 
Upvote 0

Christian García S.

Active Member
Licensed User
Thanks @Erel,

I implemented it like this:

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

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

B4X:
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:
Upvote 0

Christian García S.

Active Member
Licensed User
Thanks @Erel

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

B4X:
#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:

B4X:
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
 
Upvote 0

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.
 
Upvote 0
Top