When the TreeView is displayed for the first time, the focus is given to the second visible item. Why? The only way to get rid of it is by clicking on another item. By code, I did not find a way to move it or clear it.
You can use this code to explicitly set the focus:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show
For i = 1 To 10
Dim ti As TreeItem
ti.Initialize("", "Item " & i)
TreeView1.Root.Children.Add(ti)
Next
CallSubDelayed3(Me, "SetFocus", TreeView1, 0)
End Sub
Private Sub SetFocus(tv As TreeView, index As Int)
Dim jo As JavaObject = tv
jo.RunMethodJO("getFocusModel", Null).RunMethod("focus", Array(index))
End Sub
You can use this code to explicitly set the focus:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show
For i = 1 To 10
Dim ti As TreeItem
ti.Initialize("", "Item " & i)
TreeView1.Root.Children.Add(ti)
Next
CallSubDelayed3(Me, "SetFocus", TreeView1, 0)
End Sub
Private Sub SetFocus(tv As TreeView, index As Int)
Dim jo As JavaObject = tv
jo.RunMethodJO("getFocusModel", Null).RunMethod("focus", Array(index))
End Sub