Android Question Touch Events on ScrollView

khwarizmi

Active Member
Licensed User
Longtime User
Hi all

Can I apply touch events on ScrollView ?

thanks
 

khwarizmi

Active Member
Licensed User
Longtime User
I found it!
I can handle it to ScrollView.panel
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I can handle it to ScrollView.panel
No, you cannot. The Touch event is consumed by the ScrollView!
But, you can add a Touch event to the ScrollView with the Reflection library.
Attached a small test program.
And the code:
B4X:
Sub Globals
    Private scvTest As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
   
    Private ref As Reflector
    ref.Target = scvTest
    ref.SetOnTouchListener("scvTouchRef")
   
    FillScrollView
End Sub

Private Sub FillScrollView
    Private i As Int
    Private Height0, Height1 As Int
   
    Height0 = 40dip
    Height1 = Height0 - 2dip
    For i = 0 To 50
        Private lbl As Label
        lbl.Initialize("Labels")
        scvTest.Panel.AddView(lbl, 0, i * Height0, scvTest.Width, Height1)
        lbl.Color = Colors.Blue
        lbl.TextColor = Colors.Yellow
        lbl.Text = "Test " & i
        lbl.Tag = i
    Next
    scvTest.Panel.Height = i * Height0
End Sub

Private Sub scvTouchRef(Viewtag As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
    Select Action
        Case Activity.ACTION_DOWN
            Log("DOWN")
        Case Activity.ACTION_MOVE
            Log("MOVE")
        Case Activity.ACTION_UP
            Log("UP")
    End Select
End Sub

Private Sub Labels_Click
    Private lbl As Label
   
    lbl = Sender
    Log(lbl.Text)
End Sub
 

Attachments

  • ScrollViewTouchEvent.zip
    9.3 KB · Views: 259
Upvote 0
No, you cannot. The Touch event is consumed by the ScrollView!
But, you can add a Touch event to the ScrollView with the Reflection library.
Attached a small test program.
And the code:
B4X:
Sub Globals
    Private scvTest As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
  
    Private ref As Reflector
    ref.Target = scvTest
    ref.SetOnTouchListener("scvTouchRef")
  
    FillScrollView
End Sub

Private Sub FillScrollView
    Private i As Int
    Private Height0, Height1 As Int
  
    Height0 = 40dip
    Height1 = Height0 - 2dip
    For i = 0 To 50
        Private lbl As Label
        lbl.Initialize("Labels")
        scvTest.Panel.AddView(lbl, 0, i * Height0, scvTest.Width, Height1)
        lbl.Color = Colors.Blue
        lbl.TextColor = Colors.Yellow
        lbl.Text = "Test " & i
        lbl.Tag = i
    Next
    scvTest.Panel.Height = i * Height0
End Sub

Private Sub scvTouchRef(Viewtag As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
    Select Action
        Case Activity.ACTION_DOWN
            Log("DOWN")
        Case Activity.ACTION_MOVE
            Log("MOVE")
        Case Activity.ACTION_UP
            Log("UP")
    End Select
End Sub

Private Sub Labels_Click
    Private lbl As Label
  
    lbl = Sender
    Log(lbl.Text)
End Sub
Thanks Klaus, it works, but, how can I resend the event to scrollview to make it scroll. The scroll stop working with this code
 
Upvote 0
Top