Android Question Add drag and drop to multiple views

apty

Active Member
Licensed User
Longtime User
I am creating panels programatically based on number of entries in a database table, e.g if the table has 10 entries, i create 10 panels. Now i want the panels to be draggable around the screen. I have tried the DraggableView class but i keep getting NullPointerException error. Below is my code, please assist

B4X:
Dim Cursor1 As Cursor=sql1.ExecQuery("SELECT foodname,imaged FROM tastes")
        Dim ttrow As Int=Cursor1.RowCount
    Dim dv(ttrow) As DraggableView
For i = 0 To Cursor1.RowCount - 1
        Cursor1.Position = i
'        Log("************************")
  
      Dim pnl As Panel
 
    pnl.Initialize("pnl2")
        pnl.LoadLayout("panel1")
  
         dv(i).Initialize(Activity, pnl)
Activity.AddView(pnl,lft,tp,Activity.Width/5,Activity.Width/5+20dip)
Next
 

apty

Active Member
Licensed User
Longtime User
ok thanks Erel. I managed to do it by also initializing the draggable view after Activity.Adview...

By the way, i realised after adding the draggable view i can't get the pnl2_Click event. If i remove the draggableview, the click event works. How can i get the panel click event with the draggable view?
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
i modified the class and included a click event as shown below but it still doesn't work. I can only drag and drop but clicking on the panels doesn't do anything
B4X:
Sub Initialize(Activity As Activity, v As View)
   innerView = v
   panel1.Initialize("pnl2")
   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
   ACTION_UP = Activity.ACTION_UP
   'action_click=activity.
   Dim r As Reflector
   r.Target = panel1
  r.SetOnClickListener("IV_onClick")
   r.SetOnTouchListener("Panel1_Touch") 'why reflection instead of the regular Panel_Touch event? Good question which deserves a forum thread of its own (not related to classes)...
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
   Else
      innerView.Left = innerView.Left + x - downx
      innerView.Top = innerView.Top + y - downy     
      panel1.Left = innerView.Left
      panel1.Top = innerView.Top
   End If
   Return True
End Sub

Public Sub IV_onClick(o As Object)
    Msgbox("This click has been trapped by your class", "Clicked")
End Sub
 
Upvote 0
Top