#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private lv As ListView
Private selectedItemPos As Int = -1
Private selectedItemValue As Object
Private lasttoucheditem As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
lv.Initialize("lv")
Activity.AddView(lv,0,0,100%x,100%y)
For i = 0 To 100
lv.AddSingleLine("item : " & i)
Next
Dim ref As Reflector
ref.Target = lv
ref.SetOnTouchListener("lv_Touch")
End Sub
Sub lv_ItemLongClick (Position As Int, Value As Object)
selectedItemPos = Position
selectedItemValue = Value
End Sub
Private Sub lv_Touch (o As Object, ACTION As Int, x As Float, y As Float, motion As Object) As Boolean
If ACTION = 0 Then
lasttoucheditem = Floor(y/lv.SingleLineLayout.ItemHeight)
Else if ACTION = 2 Then
If selectedItemPos > -1 Then
If Floor(y/lv.SingleLineLayout.ItemHeight) <> lasttoucheditem Then
lasttoucheditem = Floor((y+getfirstitemtop)/lv.SingleLineLayout.ItemHeight)
updateLV(lasttoucheditem)
End If
End If
else if ACTION = 1 Then
selectedItemPos = -1
End If
Return False
End Sub
Sub getfirstitemtop As Int
Dim jo As JavaObject
jo.InitializeContext
Dim pos As Int = jo.RunMethod("topLv",Array(lv))
Log(pos)
Return Abs(pos)
End Sub
'
#If Java
import android.widget.ListView;
import android.view.View;
public static int topLv(ListView mListView) {
View v = mListView.getChildAt(0);
int top = (v == null) ? 0 : (v.getTop());
return top;
}
#End If
Sub updateLV(pos As Int)
Dim l As List
l.Initialize
For i = 0 To lv.Size-1
Dim item As Object = lv.GetItem(i)
l.Add(item)
Next
lv.Clear
l.RemoveAt(selectedItemPos)
l.InsertAt(pos+getfirstvisibleItem,selectedItemValue)
selectedItemPos = pos+getfirstvisibleItem
For j = 0 To l.Size-1
lv.AddSingleLine(l.Get(j))
Next
End Sub
Sub getfirstvisibleItem As Int
Try
Dim ref As Reflector
ref.Target = lv
Return(ref.RunMethod("getFirstVisiblePosition"))
Catch
Return(0)
End Try
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub