Android Question Reflection V.S. Click

catyinwong

Active Member
Licensed User
Longtime User
Tried on the reflection function (as draggable views).. If the draggable control is a panel, the click event will be disabled but the click event will not be affect for other types of control (e.g. label).. Why is that and how to solve this problem for panel?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've just tried it with a label and panel and in both cases the click event is not fired because the touch gesture is handled by the added panel (see DraggableView code).

You can use this code to add a Click event to this class:
B4X:
Sub Globals

   Private Panel1 As Panel
   Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Dim dv1, dv2 As DraggableView
   dv1.Initialize(Activity, Panel1, Me, "dv1")
   dv2.Initialize(Activity, Label1, Me, "dv2")
End Sub

Sub dv1_Click
   Log("click")
End Sub

Sub dv2_Click
   Log("click")
End Sub

DraggableView class:
B4X:
Sub Class_Globals
   Private innerView As View
   Private panel1 As Panel
   Private downx, downy As Int
   Private ACTION_DOWN, ACTION_MOVE As Int
   Public GridX = 10dip, GridY = 10dip As Int
   Private moved As Boolean
   Private mCallback As Object
   Private mEventName As String
End Sub

public Sub Initialize(Activity As Activity, v As View, Callback As Object, EventName As String)
   innerView = v
   panel1.Initialize("")
   panel1.Color = Colors.Transparent
   Activity.AddView(panel1, v.Left, v.Top, v.Width, v.Height)
   ACTION_DOWN = Activity.ACTION_DOWN
   ACTION_MOVE = Activity.ACTION_MOVE
   Dim r As Reflector
   r.Target = panel1
   r.SetOnTouchListener("Panel1_Touch")
   mCallback = Callback
   mEventName = EventName
End Sub



Private Sub Panel1_Touch (o As Object, ACTION As Int, x As Float, y As Float, motion As Object) As Boolean
   If ACTION = ACTION_DOWN Then
     downx = x
     downy = y
     moved = False
   Else if ACTION = ACTION_MOVE Then
     Dim l As Int = innerView.Left + x - downx
     Dim t As Int = innerView.Top + y - downy
     Dim deltaX = (l Mod GridX), deltaY = (t Mod GridY) As Int
     innerView.Left = l - deltaX
     innerView.Top = t - deltaY
     If deltaX <> 0 Or deltaY <> 0 Then moved = True
     panel1.Left = innerView.Left
     panel1.Top = innerView.Top
   Else
     If moved = False Then
       CallSub(mCallback, mEventName & "_click")
     End If
   End If
   Return True
End Sub
 
Upvote 0
Top