B4A Library [Lib] Gesture Detector

Informatix

Expert
Licensed User
Longtime User
I tried the GestureDetector_Drag demo in Debug(rapid) mode and had no issue with onFling (and never had an issue in the other modes) so it's a bit difficult to say what happens in your app without seeing the code of your event handler.
 
Last edited:

andrewj

Active Member
Licensed User
Longtime User
Thanks, that worked. It would be worth getting Erel to add it to the standard list. Unfortunately I've realised that it's not going to work for what I want to do, as I'm trying to send the event back to the same control which causes an endless loop! I've found another solution, but thanks anyway.
Andrew
 

siddsg

Member
Licensed User
Longtime User
 

siddsg

Member
Licensed User
Longtime User
Hello Informatix,
I used your library, and its working fine as far as scrolling is concerned.
However, I want to stop the scrolling at a point.

Basically, I have a flashcards app where on scrolling from right to left, I fire a query to the db and retrieve a question and its answer.
So when I scroll once, it should retrieve Q1. On scrolling again, it should retrieve the corresponding answer.
However, when I scroll, it jut scrolls indefinitely and retrieves some random question/answer.

Could you please tell how to stop the scrolling?

Thank you,
Sidd
 

Informatix

Expert
Licensed User
Longtime User
Sorry, but I can't see why my library is involved in this problem. It's the view (and the corresponding library) that manages the scrolling that should be concerned. My library is mainly an event listener for touch actions; it has no code to start or stop a scrolling.
 

Joacim

Member
Licensed User
Longtime User
All of these events are of type boolean. When should I return true from the event?
 

Informatix

Expert
Licensed User
Longtime User
Thank you, but how do I do that: "Set the OnTouch listener to null"?
An example of the code I use is: GD.SetOnTouchListener(Button1, "GD_Button1")
Do you mean: GD.SetOnTouchListener(Button1, null) ??
Sorry, I thought you were talking of the Reflection library because, in my library, the function name is not SetOnTouchListener but SetOnGestureListener. To remove this listener, you have to call RemoveListener (in the donationware version) or use the Reflection lib (see this post).
10 gesture listeners are not a lot. It's even a very low number. You will probably start facing problems when you have hundreds of GD.
 

Informatix

Expert
Licensed User
Longtime User
Starting from September, the full version of the following classes and libraries will be available for free (but your donations will still be welcome):
- Gesture Detector
- File Explorer
- SlidingSidebar
I don't have enough time to continue to support them (and FileExplorer is completely outdated by the class provided with UltimateListView).
 

postasat

Active Member
Licensed User
Longtime User
I have a problem...
I read all the posts but I didn't find a full working solution.
I have a vertical scrollview and I want to do the following action:

If I swipe vertical, the event must be passed to scrollview, if I scroll horizontal (left to right) I will do an action (launching a sub), if I scroll horizontal (right to left) I must do a second action (launching another sub).

I tried using onFling event.
The horizontal swipe is working correctly, but if I have a vertical swipe and pass event to scrollview I have strange effects.
Using motionevent1 (null) give me errors, using motionevent2 (like in the code below) the scroll is slow and unpredictable.
I used reflector because with gesturedetector "passevent_to" I have an anomaly app stop error.

The code I use is the following:


B4X:
Sub Globals
Dim Gesture1 As GestureDetector

Sub Activity_Create
Gesture1.SetOnGestureListener(scvMain,"Gesture1")

Sub Gesture1_onFling(velocityX As Float, velocityY As Float, MotionEvent1 As Object, MotionEvent2 As Object)

'if horizontal speed is low, I'm scrolling vertical, I pass the event to scrollview (scvMain)
If Abs(velocityX) < positive_speed Then
    Dim r As Reflector
    r.Target = scvMain
    r.RunMethod4("onTouchEvent", Array As Object(MotionEvent2), Array As String("android.view.MotionEvent"))
Else
'if I have a vertical scroll from right to left
    If velocityX < negative_speed Then
            Button1_click
    Else    'from left to right
            Button2_click
    End If
End If
End Sub


Someone can help ?
Thank you !

Roberto.
 

Informatix

Expert
Licensed User
Longtime User
A ScrollView (or a ListView) intercepts touch events, so unless you block this interception with requestDisallowInterceptTouchEvent (search in the forum), you'll get unexpected results.

I used reflector because with gesturedetector "passevent_to" I have an anomaly app stop error.
You should try to understand why you get an error because that does not come from my code.
 

klaus

Expert
Licensed User
Longtime User
The code below works.
It uses the ScrollView Touch event added with the reflection library.
B4X:
Sub Globals
    Dim scvTest As ScrollView
    Dim x0, y0 As Float
    Dim swipe As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    scvTest.Initialize(100)
    Activity.AddView(scvTest, 0, 0, 100%x, 100%y)
   
    Dim r As Reflector
    r.Target = scvTest
    r.SetOnTouchListener("scvTest_Touch")
   
    FillScrollView
End Sub

Sub scvTest_Touch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
    Select action
    Case 0 ' DOWN
        x0 = X
        y0 = Y
    Case 2 ' MOVE
        If Abs(X - x0) > Abs(Y - y0) Then
            swipe = True
        Else
            swipe = False
        End If
    Case 1 ' UP
        If swipe = True Then
            If X - x0 > 40dip Then ' right swipe
                ToastMessageShow("Right swipe", False)
            Else If X - x0 < 40dip Then ' left swipe
                ToastMessageShow("Left swipe", False)
            End If
        End If
    End Select
    Return False
End Sub

Sub FillScrollView
    Dim i As Int
    Dim height = 50dip As Int
    Dim height_1 = height - 1dip As Int
   
    For i = 0 To 50
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Text = "Test " & i
        scvTest.Panel.AddView(lbl, 0, i * height, 100%x, height_1)
    Next
    scvTest.Panel.Height = i * height
End Sub

Attached a small test program.
 

Attachments

  • ScrollViewHSwipe.zip
    6.4 KB · Views: 187

Informatix

Expert
Licensed User
Longtime User
Note that's easier to do with GestureDetector:
B4X:
Sub Globals
    Dim scvTest As ScrollView
    Dim GD As GestureDetector
End Sub

Sub Activity_Create(FirstTime As Boolean)
    scvTest.Initialize(100)
    GD.SetOnGestureListener(scvTest, "GD")
    Activity.AddView(scvTest, 0, 0, 100%x, 100%y)
    FillScrollView
End Sub

Sub GD_onScroll(distanceX As Float, distanceY As Float, MotionEvent1 As Object, MotionEvent2 As Object)
    If Abs(distanceX) > Abs(distanceY) Then
        Log("horizontal")
    Else
        Log("vertical")
    End If
End Sub
But it seems that postasat is trying to do something different as he wants to send touch events to the scrollview.
 

postasat

Active Member
Licensed User
Longtime User
Informatix, you're right.

The code above is perfect, but I need the correct code to pass event to scrollview only when Abs(distanceX) < Abs(distanceY).
So I need a code to place in place of "Log("vertical")" that send/pass motion event to scrollview.

Thank you.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…