B4J Question Drag and Drop in TableView

How can I replace the content of two cells using the drag and drop (and also apply the changes in the database), the content of my cells is a label with a different text & tag.
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
You asked this question a while ago and you might have found an answer by now, but in case you have not here are some ideas.

1. Cover the TableView with a transparent panel and use this to detect mouse movements connected with the drag-drop operation.
2. Do not try to drag the labels themselves - use a proxy label to give the visual appearance of the drag operation and, at the end of the drag-drop, simply switch the contents of the two labels involved.

I attach a demo program. I did not want to spend too much time if you had already found an answer, so I used a simple button grid rather than a TableView. With a TableView you will have to check the scrolled position to determine which labels are under the cursor at the start and end of the operation, and if you want to be able to drag labels outside of the current TableView area then you will have to sense the mouse position as it approaches the edges of the view and scroll appropriately. That will probably involve a timer.

Finally, this is only a demo program - it might contain bugs.
 

Attachments

  • DragDrop.zip
    3.5 KB · Views: 142
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
You asked this question a while ago and you might have found an answer by now, but in case you have not here are some ideas.
The problem I see with this is you have the luxury of knowing each buttons location. While I am not familiar with the tableview I'm guessing getting the coordinates of each item in the list would be problematic since they can move, and what about items that are above or below the visible?
Nice job though! I will remember this in the future for simple D&Ds where the items don't move. Using the D&D libs is a bit of a pain.
The topic of this thread is Tableview not B4XTable.
I deleted it. It was published inadvertently, wasn't even close to finished.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Hi @MrKim. Thanks for your comments.
I'm guessing getting the coordinates of each item in the list would be problematic since they can move, and what about items that are above or below the visible?
I don't think that that is a problem. After answering the original post I decided to animate a row-and-column dragging operation in an old spreadsheet style program of mine. True all elements are constant size, and it was on a ScrollView rather than a TableView, but I don't think that makes things so different. I also implemented autoscrolling (really quite simple when you look into it) so that dragging rows beyond the initially visible limits of the list works as well. Here, just for completeness, is the autoscroller implementation ...

B4X:
Sub Class_Globals

  Private autoScroller As Timer                   ' Used to scroll page when mouse approaches edges

  Private mouseOk As Boolean                      ' TRUE if mouse is over grid area
  Private autoUp, autoDown As Int                 ' Autoscrolling counting indicators

End Sub


  . . .
  autoScroller.Initialize("autoScroller", 50)    
  autoScroller.Enabled = True
  . . .

' Auto scroll page if pointer is close to top or bottom edges and stationary
' [rawY] is mouse Y-position over view area (ie value without any scroll element)
Public Sub autoScroller_Tick
  Dim dY As Double = 16                           ' Set auto-scroll margin size
  If (rawY < dY) And mouseOk Then autoUp = autoUp + 1 Else autoUp = 0
  If (autoUp > 5) And (host.VPosition > 0) Then host.VPosition = host.VPosition - 0.01
  If (rawY > host.Height - dY) And mouseOk Then autoDown = autoDown - 1 Else autoDown = 0
  If (autoDown < -5) And (host.VPosition < 1.0) Then host.VPosition = host.VPosition + 0.01
End Sub

' Identify if mouse is no longer over scroll view
Private Sub mouse_MouseExited(EventData As MouseEvent)
  mouseOk = False                                 ' The mouse has left the building
End Sub
 
Upvote 0
Top