Android Question CLV custom selection

Ilya G.

Active Member
Licensed User
Longtime User
I need to make a selection in CLV on an Android TV with a remote control. My attempt works if the number of elements is less than the height of the screen, if there are more elements, some nonsense starts to happen. Maybe there are other ways to organize the correctly selection of an item?
 

Attachments

  • clvtest.zip
    9.9 KB · Views: 122

Lucas Siqueira

Active Member
Licensed User
Longtime User
I need to make a selection in CLV on an Android TV with a remote control. My attempt works if the number of elements is less than the height of the screen, if there are more elements, some nonsense starts to happen. Maybe there are other ways to organize the correctly selection of an item?
try to use
CustomListView1.ScrollToItem(CustomListViewIndex_1)
to position it on the panel you need.


B4X:
#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
    Private xui As XUI
End Sub

Sub Globals
    Private CustomListViewIndex_1 As Int = -1
    Private CustomListView1 As CustomListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    For i = 0 To 7
        Private p As Panel
        p.Initialize("")
        p.SetLayoutAnimated(0, 0, 0, CustomListView1.AsView.Width, 60dip)
        p.LoadLayout("Item")
        
        Private cd As ColorDrawable
        cd.Initialize(Rnd(0xFF000000, -1), 15dip)
        p.GetView(0).As(Button).Background = cd
        p.GetView(0).As(Button).Text = i

        CustomListView1.Add(p, "")
    Next
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    Log(KeyCode)
    
    If KeyCode = KeyCodes.KEYCODE_DPAD_UP Then
        If CustomListViewIndex_1 > 0 Then
            CustomListViewIndex_1 = CustomListViewIndex_1 - 1
            
            For i = 0 To CustomListView1.Size - 1
                CustomListView1.GetPanel(i).GetView(1).Visible = False
                CustomListView1.GetPanel(i).GetView(2).Visible = False
            Next
            
            CustomListView1.ScrollToItem(CustomListViewIndex_1)
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(0).RequestFocus
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(1).Visible = True
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(2).Visible = True
        End If
        
        Return True
    Else If KeyCode = KeyCodes.KEYCODE_DPAD_DOWN Then
        If CustomListViewIndex_1 < CustomListView1.Size - 1 Then
            CustomListViewIndex_1 = CustomListViewIndex_1 + 1
            
            For i = 0 To CustomListView1.Size - 1
                CustomListView1.GetPanel(i).GetView(1).Visible = False
                CustomListView1.GetPanel(i).GetView(2).Visible = False
            Next
            
            CustomListView1.ScrollToItem(CustomListViewIndex_1)
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(0).RequestFocus
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(1).Visible = True
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(2).Visible = True
        End If
        
        Return True
    End If

    Return False
End Sub
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
try to use
CustomListView1.ScrollToItem(CustomListViewIndex_1)
to position it on the panel you need.

Hi Lucas, try to set For i = 0 To 20 and you will understand what I'm talking about, it looks like pressing the down and up keys is not processed correctly
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I need to make a selection in CLV on an Android TV with a remote control. My attempt works if the number of elements is less than the height of the screen, if there are more elements, some nonsense starts to happen. Maybe there are other ways to organize the correctly selection of an item?
Comment the RequestFocus out and Add the ScrollToItem
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    Log(KeyCode)
    
    If KeyCode = KeyCodes.KEYCODE_DPAD_UP Then
        If CustomListViewIndex_1 > 0 Then
            CustomListViewIndex_1 = CustomListViewIndex_1 - 1
            
            For i = 0 To CustomListView1.Size - 1
                CustomListView1.GetPanel(i).GetView(1).Visible = False
                CustomListView1.GetPanel(i).GetView(2).Visible = False
            Next
            
'            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(0).RequestFocus  '<--remove it
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(1).Visible = True
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(2).Visible = True
        End If
        CustomListView1.ScrollToItem(CustomListViewIndex_1)  '<---Add  ScrollToItem
        Return True
    Else If KeyCode = KeyCodes.KEYCODE_DPAD_DOWN Then
        If CustomListViewIndex_1 < CustomListView1.Size - 1 Then
            CustomListViewIndex_1 = CustomListViewIndex_1 + 1
            
            For i = 0 To CustomListView1.Size - 1
                CustomListView1.GetPanel(i).GetView(1).Visible = False
                CustomListView1.GetPanel(i).GetView(2).Visible = False
            Next
            
'            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(0).RequestFocus  <--remove it
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(1).Visible = True
            CustomListView1.GetPanel(CustomListViewIndex_1).GetView(2).Visible = True
        End If
        CustomListView1.ScrollToItem(CustomListViewIndex_1)  '<---Add  ScrollToItem
        Return True
    End If

    Return False
End Sub
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
The first video shows that if all the items fit into the height of the screen, then everything works as it should

C0AFMiFSMOKJHyGeTmZFA-2wlE2PGgNI9nGavYdUD3-cNCoZCDLh4CKpF8_SaJgOlsFBbd7oQAEM6SZxAGgebJ00S1DvBlMn-4byhygAt70


In the second video, the problem I encountered both in the emulator and on TV

zMEt2zXpP9poQXB5Oi73MGgax6H5l1WbvMwGoIXfsk8TazPImNN9aLbhatJG0yxllsFBbd7oQAEM6SZxAGgebF9zD_Oclbpnxa_8GMym6Vw
 
Upvote 0
Top