Android Question ScrollView - select/deselect item on click

ThePuiu

Active Member
Licensed User
Longtime User
I have a ScrollView with more items (panel) ~ 1000. When I select one of them, her background must change to another color, and the one previously selected to return her back to normal (unselected). I used this code to do that:
B4X:
Sub PanelX_Click
    Dim pnl As Panel
    pnl = Sender
   
    For Each vold As View In ScrollViewArticole.Panel.GetAllViewsRecursive
        vold.Color = Colors.ARGB(255, 30 ,100,100)
    Next
   
    pnl.Color = Colors.ARGB(255,252,101,101)
......

Can this be done otherwise, faster?
Thank you!
 

Misterbates

Active Member
Licensed User
Here's some code I added to a previous CLV version (with your colors inserted).

The concept is:
  • Each time a panel is added (or inserted), set the .Tag to be the index of the panel (CLV already does this I think)
  • Keep track of the last selected item index
  • Every time an item is clicked, if it's a different item, unselect the previous then select the new
B4X:
Sub Class_Globals
    Private LastSelectedItem As Int = -1
End Sub

Private Sub Panel_Click
    Dim v As View = Sender
    SelectItem(v.Tag)
End Sub

private Sub SelectItem(Index As Int)
    If LastSelectedItem >= 0 And LastSelectedItem <> Index Then
        Dim pnl As Panel = GetPanel(LastSelectedItem)
        pnl.color = Colors.ARGB(255, 30 ,100,100) 'Remove highlight
        LastSelectedItem = -1
    End If
    Dim pnl As Panel = GetPanel(Index)
    pnl.color = Colors.ARGB(255,252,101,101) 'Add highlight
    LastSelectedItem = Index
End Sub
 
Upvote 0

ThePuiu

Active Member
Licensed User
Longtime User
because I don't use CustomListView, I must adapt the code, but something is not well:
B4X:
Sub PanelX_Click
    Dim pnl As Panel
    pnl = Sender
   
    'deselect old panel
    If LastSelectedItem >= 0 And LastSelectedItem <> pnl.Tag Then
        For Each vold As View In ScrollViewArticole.Panel.GetAllViewsRecursive
            If vold Is Panel And vold.Tag = LastSelectedItem Then
                vold.Color = Colors.ARGB(255, 30 ,100,100)            <<========= NEVER COME HERE!!!
            End If           
        Next   
    End If

    'select new panel
    pnl.Color = Colors.ARGB(255,252,101,101)   
    For Each v As View In pnl.GetAllViewsRecursive
        v.Color = Colors.ARGB(255,252,101,101)           
    Next
   
    LastSelectedItem = pnl.Tag
   
End Sub

although the conditions are met (verified with the debugger), never deselect the panel!
 
Upvote 0

Misterbates

Active Member
Licensed User
Quite difficult to say what's wrong without the complete code, but a few things to check:
1) Did you add LastSelectedItem to the appropriate _Globals sub?
2) When you add panels to the scrollview, do you set the pnl.Tag to a unique ID that you can later use with LastSelectedIndex?
3) Is the type of your .Tag (when you .Tag the panel) the same as the type of your LastSelectedItem?
4) In your code above, your "deselect" and "select" handle child views differently (deselect only changes the panel, no children) - is that intentional?

And then an optimisation - when examining the views in ScrollViewArticle, why not just check the panels? Would be much quicker than checking the panels and their child views ...

If you could post your code, would make helping you easier :)
 
Upvote 0

ThePuiu

Active Member
Licensed User
Longtime User
I'm ashamed of the mistake! The type of variable that initiates the Tag (string) differs from LastSelectedItem (Int). Now everything is ok!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You don't need to go through all the views in the Panel.
The code below does it too.
B4X:
Sub PanelX_Click
    Private pnl As Panel

    'deselect old panel
    If LastSelectedItem >= 0 Then
          pnl = ScrollViewArticole.Panel.GetView(LastSelectedItem)
          pnl.Color = Colors.ARGB(255, 30 ,100,100)
    End If

    'select new panel
    pnl = Sender
    pnl.Color = Colors.ARGB(255,252,101,101)
    LastSelectedItem = pnl.Tag
End Sub

Another solution:
Define a Panel in Globals
Private pnlSelected As Panel

When you fill the ScrollView:
pnlSelected = ScrollViewArticole.Panel.GetView(0)

And the Click event routine
B4X:
Sub PanelX_Click
    'deselect old panel
    pnlSelected.Color = Colors.ARGB(255, 30 ,100,100)

    'select new panel
    pnlSelected = Sender
    pnlSelected.Color =Colors.ARGB(255,252,101,101)
End Sub
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
In the solution i suggest you, the Tag of the Panels is the row index and not the last selected Panel index.
Attached my test project.
You can switch between the two solutions in the InitScollView routine.
 

Attachments

  • ScrollViewSelect.zip
    7 KB · Views: 205
Last edited:
Upvote 0
Top