Add the triggering view to ListView_ItemClick and ItemLongClick events

corwin42

Expert
Licensed User
Longtime User
The onItemClick event for a ListView in Java provides the view (that represents the pressed item) that triggered the event. Can you please pass this to the event sub as parameter? Then it would be possible to position the AHQuickAction popup over the pressed item.

public void onItemClick(AdapterView<?> parent, View view, int position, long id)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use a transparent panel which is overlaid over the ListView to get the actual position.
See this example:
B4X:
Sub Process_Globals
   Dim touchPosY As Int
End Sub
Sub Globals        
   Dim ListView1 As ListView
   Dim Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   For i = 1 To 1000
      ListView1.AddSingleLine(i)
   Next
End Sub
Sub Activity_Pause(UserClosed As Boolean)

End Sub
Sub Panel1_Touch (Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
   touchPosY = Y
   Return False
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
   Log(touchPosY)
End Sub
 

corwin42

Expert
Licensed User
Longtime User
Nice workaround, thank you.

But I would prefer the much simpler way if you would add the view to the parameters of the ItemClick and ItemLongClick events. This should be even backwards compatible to older versions of the core library.

This

B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object, View as View)
    AHQuickAction.Show(View)
End Sub

looks to me much more comprehensive than this

B4X:
Sub Panel1_Touch (Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
    touchPosY = Y
    Return False
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
   Dim DummyPanel As Panel
   
   DummyPanel.Initialize("")
   DummyPanel.Color = Colors.Transparent
   Activity.AddView(DummyPanel, ListView1.Left, touchPosY, ListView1.Width, 1dip)
   DoEvents
   AHQuickAction.Show(DummyPanel)
   DummyPanel.RemoveView
End Sub

and it is not the same result because the QuickAction popup positions itself above or below the ListView Item when using the view for its parameter and with this solution the popup points to the position the user pressed which sometimes looks a bit strange.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Changing the signature of an existing event is possible. However it can be confusing. I try to avoid changing signatures.

The returned view is usually not needed and exposing it will create all kinds of problems due to the way ListView is built. ListView is a pretty complicated view.

You can achieve the same goal with reflection:
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
   AHQuickAction.Show(GetViewAtPos(Sender, Position))
End Sub

Sub GetViewAtPos(LV As ListView, Position As Int) As View
   Dim r As Reflector
   Dim v As View
   r.Target = LV
   Dim first As Int
   first = r.RunMethod("getFirstVisiblePosition")
   v = r.RunMethod2("getChildAt", Position - first, "java.lang.int")
   Return v
End Sub

Note that the LayoutParams of this view is of a different type than the other views.
 

corwin42

Expert
Licensed User
Longtime User
Thanks.

I agree with you that changing signature is always a bad thing.

Your solution with reflection is great and should be the way to go on this problem.

Gesendet von meinem LG-P500 mit Tapatalk
 
Top