Android Code Snippet CLVSwipe with images

MrKim

Well-Known Member
Licensed User
I modified CLVSwipe to allow images, not just text on the Swipe. Relevant Code is below (Hope I got it all;))

REQUIRES AN IMAGE IN ASSETS Trash.jpg here it is if you want to copy it.
upload_2019-8-21_1-3-46.png

note that I am not doing anything at this point to scale the images. Should probably add the aspect ratio of the image to the TYPE and use that to scale it.

upload_2019-8-21_0-47-9.png

'New Type in CLVSwipe - required for the text return value as bitmaps don't have a tag.
B4X:
'New Type in CLVSwipe
Sub Class_Globals
    Type SwipeImage(Bmp As Bitmap, RetVal As String)
New code in CreateActionButtons
and New SubTo handle the Panel Type instead of the Label
NOTE: I also modified this for unlimited buttons. The just keep getting narrower and narrower:p
B4X:
Private Sub CreateActionButtons (actions As List) As Int
    ActionsPanel.RemoveAllViews
    Dim LastX As Int = 0
    Dim width As Int = ActionsPanel.Width / actions.Size   'kim
    For Each action As Object In actions
        If action Is String Then
            Dim l As Label
            l.Initialize("lbl")
            Dim xlbl As B4XView = l
            xlbl.Text = action
            xlbl.Color = ActionColors.GetDefault(action, xui.Color_White)
            xlbl.SetTextAlignment("CENTER", "CENTER")
            xlbl.Font = xui.CreateDefaultBoldFont(20)
            xlbl.TextColor = xui.Color_Black
            'Dim width As Int = ActionsPanel.Width / actions.Size   'kim
            'Dim width As Int = Max(40dip, cvs.MeasureText(action, xlbl.Font).Width + 20dip)   'original
            ActionsPanel.AddView(xlbl, ActionsPanel.Width - width - LastX, 0, width, ActionsPanel.Height)
        Else If action Is SwipeImage Then
            Dim SI As SwipeImage = action
            Dim Img As Panel
            Img.Initialize("Img")
            Img.Tag = SI.RetVal
            Dim ximg As B4XView = Img, CV As Canvas, R As Rect
            R.Initialize(0, 0, width, ActionsPanel.Height)
            ActionsPanel.AddView(ximg, ActionsPanel.Width - width - LastX, 0, width, ActionsPanel.Height)
            CV.Initialize(ximg)
            CV.DrawBitmap(SI.Bmp, Null, R)
        End If
        LastX = LastX + width
    Next
    Return LastX
End Sub



Private Sub Img_Click
    Dim Img As B4XView = Sender
    Dim index As Int = LastSwipedItem
    CloseLastSwiped
    CallSub3(mCallback, mEventName & "_ActionClicked", index, Img.Tag)
End Sub



New Code in main for the demo
B4X:
Sub CreateItems
    Dim cs As CSBuilder
 
    For i = 1 To 100
        cs.Initialize.Color(Rnd(0xff000000, -1))
        If i Mod 3 = 0 Then
            'Dim trash As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "Trash.jpg", 5, 5, True)
            Dim trash As SwipeImage
            trash.Initialize
            trash.Bmp.Initialize(File.DirAssets, "Trash.jpg")
            trash.RetVal = "Trash"
            ' = xui.LoadBitmapResize(File.DirAssets, "Trash.jpg", 5, 5, True)
            cs.Append($"Important item ${i} ..."$).PopAll
            CustomListView1.AddTextItem(cs, Swipe.CreateItemValue("", Array("Delete", "Do Something Else", trash)))
        Else If i Mod 3 = 1 Then
            cs.Append($"Very important item ${i} ..."$).PopAll
            CustomListView1.AddTextItem(cs, Swipe.CreateItemValue("", Array("Action 1", "Action 2", "Action 3", "Action 4", "Action 5")))
        Else
            cs.Append($"Nothing to see here ${i}"$).PopAll
            CustomListView1.AddTextItem(cs, Null)
        End If
    Next
End Sub
Swipe_ActionClicked event in main needs
Or (ActionText = "Trash")
added to have the image work

B4X:
Sub Swipe_ActionClicked (Index As Int, ActionText As String)
    Log($"Action clicked: ${Index}, ${ActionText}"$)
    If (ActionText = "Delete") Or (ActionText = "Trash") Then
        CustomListView1.RemoveAt(Index)
    Else If ActionText = "Do Something Else" Then
        Dim p As B4XView = CustomListView1.GetPanel(Index)
        Dim lbl As B4XView = p.GetView(0)
        lbl.Text = "Done!!!"
    End If
End Sub
 

Attachments

Last edited:

MrKim

Well-Known Member
Licensed User
I just made mods to the
CreateActionButtons section - it was wrong.
 
Top