Add "Swipe to Delete,Edit,..." to TableView CustomCell

narek adonts

Well-Known Member
Licensed User
This is a small snippet showing how to add a "Swipe to ..." function to TableView using ScrollView.

B4X:
Private Sub AddCell (tbl as TableView)

For i =0 To 10
Dim tc As TableCell = Tbl.AddSingleLine("")
     tc.ShowSelection = False
     Dim pnlT As Panel
     pnlT=CreateTableCell
     pnlT.Tag=i
     tc.CustomView = pnlT
    
Next

End Sub


Private Sub CreateTableCell As Panel
Dim SideScroll As ScrollView
Dim pnl1,pnl2 As Panel ' pnl1 is main panel, pnl2 is the panel visible after swipe (delete,edit,...)


Dim pnlMain As Panel


pnl1.Initialize("pnl1")
pnl2.Initialize("pnl2")
pnlMain.Initialize("pnlMain")

pnlMain.Top=0
pnlMain.Left=0
pnlMain.Width=100%x
pnlMain.Height=100dip ' Height of Row. Must be same as Tableview.RowHeight

SideScroll.Initialize("SideScroll",pnlMain.Width,pnlMain.Height)

pnlMain.AddView(SideScroll,0,0,pnlMain.Width,pnlMain.Height)

SideScroll.Panel.AddView(pnl1,0,0,pnlMain.Width,100dip)
SideScroll.Panel.AddView(pnl2,pnl1.Width,0,40%x,100dip) '40%x is the size of the "swiping panel". Change it according your needs
SideScroll.ContentHeight=100dip
SideScroll.ContentWidth=pnl1.Width+pnl2.Width
SideScroll.ShowsHorizontalIndicator=False
SideScroll.ShowsVerticalIndicator=False
SideScroll.PagingEnabled=True
SideScroll.Bounces=False

'Below is an example of creating the panels manually
'you can use also pnl1.LoadLayout function


pnl1.Color=Colors.Cyan
pnl2.Color=Colors.Green

Dim lbl As Label
lbl.Initialize("lbl")
lbl.Text="HELLO"
lbl.TextColor=Colors.Red
lbl.SizeToFit

Dim lbl2 As Label
lbl2.Initialize("lbl2")
lbl2.Text="HELLO"
lbl2.TextColor=Colors.Red
lbl2.SizeToFit
pnl2.AddView(lbl,0,0,pnl2.Width,pnl2.Height)
pnl1.AddView(lbl2,0,0,pnl2.Width,pnl2.Height)


Return pnlMain
End Sub

' We have added one label to each panel with text "HELLO"
' now the sub which is handeling event (ex. label click)

Sub lbl_Click
    'GetSenderIndex function is returning the index of the TableCell which was the parent of the label
  
        Log(GetSenderIndex(Sender))
    
     'You can add the code for this event
End Sub


Private Sub GetSenderIndex(senderObj As Object) As Int
Dim senderCellPanel As Panel
    Dim senderCellIndex As Int
    'Dim SenderTableCell As TableCell
    Dim tmpobj As Object
    Dim l As Object=Senderobj
    Dim no As NativeObject=l
   
    Dim i As Int
   
    Do Until tmpobj Is ScrollView
    tmpobj=no.GetField("superview")
    no=no.GetField("superview")
    i=i+1
    Loop
    senderCellPanel=no.GetField("superview")
    senderCellIndex=senderCellPanel.Tag
    'SenderTableCell= Tbl.GetItems(0).Get(senderCellIndex)
    Return senderCellIndex
End Sub
 
Top