Android Question CustomListView WHICH BUTTON was pressed

Daniel-White

Active Member
Licensed User
Longtime User
Hi, folks


How to detect which button is pressed?. an example. open item#1 and item#2
when I press the button inside of item#1 or inside of item#2
I don't know how to detect which is one is pressed.

because the event is captured by the same "Sub Button1_Click"

The button is inside of the panel and the layout "item" is reuse in each Item#1, Item#2 and so on.

upload_2017-10-2_11-31-12.png


I a using this example https://www.b4x.com/android/forum/threads/expandable-list-based-on-customlistview.81445/

The original Code (from the example), I only added the Sub Button1_Click in the end.


B4X:
Sub Globals
    Private clv1 As CustomListView
    Private lblTitle As Label
    Private pnlTitle As Panel
    Private ExpandedHeight As Int = 240dip
    Private CollapsedHeight As Int = 60dip
    Private pnlExpanded As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    For i = 1 To 20
        clv1.Add(CreateItem(Rnd(0xFF000000, 0xFFFFFFFF), "Item #" & i), CollapsedHeight, "")
    Next
End Sub

Sub CreateItem(clr As Int, Title As String) As Panel
    Dim p As Panel
    p.Initialize("")
    Activity.AddView(p, 0, 0, 100%x, ExpandedHeight)
    p.LoadLayout("Item")
    p.RemoveView 'remove from parent
    lblTitle.Text = Title
    pnlTitle.Color = clr
    pnlExpanded.Color = ShadeColor(clr)
    p.Tag = False 'collapsed
    Return p
End Sub

Sub ShadeColor(clr As Int) As Int
    Dim argb() As Int = GetARGB(clr)
    Dim factor As Float = 0.75
    Return Colors.RGB(argb(1) * factor, argb(2) * factor, argb(3) * factor)
End Sub

Sub GetARGB(Color As Int) As Int()
    Private res(4) As Int
    res(0) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff000000), 24)
    res(1) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff0000), 16)
    res(2) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff00), 8)
    res(3) = Bit.And(Color, 0xff)
    Return res
End Sub

Sub ExpandItem (index As Int)
    clv1.ResizeItem(index, ExpandedHeight)
    clv1.GetPanel(index).Tag = True
    AnimatedArrow(index, 0, 180)
End Sub

Sub AnimatedArrow(index As Int, From As Int, ToDegree As Int)
    Dim An As AnimationPlus
    pnlTitle = clv1.GetPanel(index).GetView(0) 'pnlTitle is the first item
    Dim iv As ImageView = pnlTitle.GetView(1) 'ImageView1 is the second item
    An.InitializeRotateCenter("", From , ToDegree, iv)
    An.Duration = clv1.AnimationDuration
    An.RepeatCount = 0
    An.PersistAfter = True
    An.Start(iv)
End Sub

Sub CollapseItem(index As Int)
    clv1.ResizeItem(index, CollapsedHeight)
    clv1.GetPanel(index).Tag = False
    AnimatedArrow(index, 180, 0)
End Sub

Sub clv1_ItemClick (Index As Int, Value As Object)
    Dim p As Panel = clv1.GetPanel(Index)
    If p.Tag = True Then
        CollapseItem(Index)
    Else
        ExpandItem(Index)
    End If
End Sub

Sub Button1_Click
    LogColor("Button1_Click",Colors.Blue)
End Sub
 

Attachments

  • upload_2017-10-2_11-24-6.png
    upload_2017-10-2_11-24-6.png
    26.2 KB · Views: 389

BillMeyer

Well-Known Member
Licensed User
Longtime User
Alternatively you could Dim Button1 in Globals, then when you load your layout you add Button1.tag = "b1"&Title (assuming your Title will be different every time), then in your code above you go:

B4X:
Sub Button1_Click
       Dim b as Button = Sender
       Log("Button Id: "&b.tag)
End Sub

and there you have it !! :D:confused:
 
Upvote 0
Top