Android Question ScrollView, swipe off and touch events

awoo060

Member
Licensed User
Longtime User
Hello all,

I have a little bit of a puzzle and am wondering what the best approach would be.
I've used a ScrollView component and populated it with a series of customized items. I'd like the ability to swipe any given item off the screen to delete it.

Ive implemented the Touch event on each item to animate the movement of each item in sync with my finger from left to right as I am swiping. The problem is, if I accidently move a little up or down during the swipe, crossing into the boundary of the item in the list above or below, the Touch event with an action of 'UP' is called, and the swipe is completed in splte of the fact my finger is still on the screen.

I can get round this relatively easily by putting a large panel over the entire area and calculating the desired effects manually. The kicker is that by taking that approach, I lose the ScrollView's scrolling effect. That is, I have to emulate the scrolling up and down by myself which has proven very error prone when dealing with larger lists and attempting to replicate the inertial scroll effect.

Another approach would be to disable the ScrollViews scrolling during a swipe from left to right, and continue to animate the swiping if I enter into another view's area, effectively ignoring the intermediate 'Up' action. The problem is then, how do i know when user's finger has truly left the screen to finish the swipe animation?? Is there a method I can call to determine if a user has their finger on the screen at any point in time?

I figure by this stage I've already gone too far off track and there must be an easier way.
 

MrKim

Well-Known Member
Licensed User
Longtime User
Don't use ScrollView. Use CustomListView. You can modify it as needed.

You can use the GestureDetector library to detect swipes.
CustomListView uses a scrollview and I can't get it to work?

Unless I am looking at the wrong customlistview?
https://www.b4x.com/android/forum/threads/customlistview-a-flexible-list-based-on-scrollview.19567/


B4X:
Sub Class_Globals
 ....
....
Dim GD As GestureDetector, DiscardOtherGestures As Boolean, FilterMoveEvents As Boolean


End Sub

Public Sub Initialize (vCallback As Object, vEventName As String)
    sv.Initialize2(0, "sv")
    items.Initialize
    panels.Initialize
    dividerHeight = 2dip
    EventName = vEventName
    CallBack = vCallback
    sv.Color = 0xFFD9D7DE 'this sets the dividers color
    Dim r As Reflector
    Dim idPressed As Int
      idPressed = r.GetStaticField("android.R$drawable", "list_selector_background")
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    pressedDrawable = r.RunMethod2("getDrawable", idPressed, "java.lang.int")
    DefaultTextColor = Colors.White
    DefaultTextSize = 16
    DefaultTextBackgroundColor = Colors.Black
    DefaultTextBackground = Null
   
    GD.SetOnGestureListener(sv, "Gesture")

End Sub
....
......

Sub Gesture_onTouch(Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
    Log("onTouch action=" & Action & ", x=" & X & ", y=" & Y & ", ev=" & MotionEvent)

    If FilterMoveEvents Then
        If Action = GD.ACTION_MOVE Then
            Log("[Filtered]")
            Return True
        Else
            FilterMoveEvents = False
        End If
    End If

    Return DiscardOtherGestures
End Sub

Sub Gesture_onDrag(deltaX As Float, deltaY As Float, MotionEvent As Object)
    Log("   onDrag deltaX=" & deltaX & ", deltaY=" & deltaY)

    'If the gesture is more horizontal than vertical and covered more than the minimal distance (10%x)...
    If Abs(deltaX) > Abs(deltaY) And Abs(deltaX) > 10%x And Not(FilterMoveEvents) Then
        FilterMoveEvents = True 'We handle all the touch events with Action = Move so no vertical scrolling is possible
        If deltaX > 0 Then
            CallSubDelayed3(Me, "DisplayMsg", "Swipe to the right", deltaX)
        Else
            CallSubDelayed3(Me, "DisplayMsg", "Swipe to the left", -deltaX)
        End If
    End If
End Sub'

Sub Gesture_onScroll(distanceX As Float, distanceY As Float, MotionEvent1 As Object, MotionEvent2 As Object)
    Log("   onScroll distanceX = " & distanceX & ", distanceY = " & distanceY & ", ev1 = " & MotionEvent1 & ", ev2=" & MotionEvent2)
End Sub

Sub Gesture_onFling(velocityX As Float, velocityY As Float, MotionEvent1 As Object, MotionEvent2 As Object)
    Log("   onFling velocityX = " & velocityX & ", velocityY = " & velocityY & ", ev1 = " & MotionEvent1 & ", ev2 = " & MotionEvent2)
End Sub

Sub DisplayMsg(Text As Object, Distance As Object)
    Log(Text & ": " & NumberFormat(Distance, 1, 0) & " pixels")
    Msgbox(Text & ": " & NumberFormat(Distance, 1, 0) & " pixels", Text)
End Sub

Sub CB_Discard_CheckedChange(Checked As Boolean)
    DiscardOtherGestures = Checked
End Sub




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