Android Question customlistview index on panel longpress

leitor79

Active Member
Licensed User
Longtime User
Hi,

I have a customlistview which I load with panels with stuff. Each customlistview's items has a panel with stuff.

I want some things done when the user long press one of the customlistview's items, but only to that item. I need the index of the item in order to remove them from a list.

So far, I capture the panel longclick event, because 1) customlistview don't have longclick event and 2) since the panel takes all the item's space, the customlistivew's click event isn't fired.

So far I use the tag property of the panel to set the index, but I don't like this option because I need to recalculate the indexes when the user inserts or deletes an item. Is there a way to get the customlistview's index where the panel belongs?

Thank you!
 

npsonic

Active Member
Licensed User
If you don't need events from views do not use event names when initializing new views.

B4X:
Dim p As Panel : p.Initialize("")
Dim lb As Label : lb.Initialize("")

You can do same with designer.

ItemLongClick is easy to be added just add these to your customlistview class

B4X:
Private Sub Panel_LongClick
    PanelLongClickHandler(Sender)
End Sub
Private Sub PanelLongClickHandler(SenderPanel As B4XView)
    Dim clr As Int = GetItem(SenderPanel.Tag).Color
    SenderPanel.SetColorAnimated(50, clr, PressedColor)
    If xui.SubExists(CallBack, EventName & "_ItemLongClick", 2) Then
        CallSub3(CallBack, EventName & "_ItemLongClick", SenderPanel.Tag, GetItem(SenderPanel.Tag).Value)
    End If
    Sleep(200)
    SenderPanel.SetColorAnimated(200, PressedColor, clr)
End Sub
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi npsonic, thay you for your answer!

Adding the LongClick event is something I've considered... thank you for sharing your code, now It would be easier. However, my CustomListView module (v1.64) does not have the GetItem function... what does it do?

However, I'm picking the panel I'm using from a layout file, not built from code, so I don't initialize it... this would work too?

Thank you!
 
Upvote 0

npsonic

Active Member
Licensed User
Hi npsonic, thay you for your answer!

Adding the LongClick event is something I've considered... thank you for sharing your code, now It would be easier. However, my CustomListView module (v1.64) does not have the GetItem function... what does it do?

However, I'm picking the panel I'm using from a layout file, not built from code, so I don't initialize it... this would work too?

Thank you!
Sorry, didn't realize that clv was changed.

Created this for new clv class

B4X:
Private Sub Panel_LongClick
    PanelLongClickHandler(Sender)
End Sub
Private Sub PanelLongClickHandler(SenderPanel As B4XView)
    Dim clr As Int = GetRawListItem(SenderPanel.Tag).Color
    SenderPanel.SetColorAnimated(50, clr, PressedColor)
    If xui.SubExists(CallBack, EventName & "_ItemLongClick", 2) Then
        CallSub3(CallBack, EventName & "_ItemLongClick", SenderPanel.Tag, GetRawListItem(SenderPanel.Tag).Value)
    End If
    Sleep(200)
    SenderPanel.SetColorAnimated(200, PressedColor, clr)
End Sub

For the views in layout, remove everything from "Event Name" field it's under "Name" field.
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Thank you very much, npsonic! Now it works.

However, the ItemLongClick event seems it's not being fired:

B4X:
Sub clvLista_ItemLongClick(Index As Int, Value As Object)
    
    Log("itemlongclick")
    Log(Index)
    
End Sub

I've added the #Event: ItemLongClick (Index As Int, Value As Object) in the header of the customlistview class (I understand It's declarative only, for the designer) and I'm not initializing int (since I understand views declared in the designer must not be manually initialized...)

Do you know if I should make some other changes?

Thank you very much!
 
Upvote 0

npsonic

Active Member
Licensed User
Yeah, didn't remember to mention about #Event line. It should work, just tested.
When you use views from layout it's true that views are initialized, but there is also field in Visual Editor called "Event Name" it's under "Main" group.
You have to remove event names from views if you want to get touch event through them and use clv events.

9Afmx4x
 
Upvote 0
Top