Android Question Custom ListView - selected row color

mfstuart

Active Member
Licensed User
Hi all,
With this view, how do you keep the touched row showing as selected with a color, example: blue?
I've searched the forum with no examples. There is the Table view, but I don't need any headers and therefore sorting or searching.
I have 4 Labels on a Layout that will be used with the CustomListView, 2 displayed and 3 hidden.
Currently when the user touches a row, the view shows selected for about a second and then fades away.

I'd like the selection color to remain, to show the user that it was and is selected.
The user would then touch a menu button to take some action based on the selected row.

Thanx,
Mark Stuart
 

Mahares

Expert
Licensed User
Currently when the user touches a row, the view shows selected for about a second and then fades away.
I'd like the selection color to remain, to show the user that it was and is selected.
Perhaps something like this:
B4X:
Sub Customlistview1_ItemClick (Index As Int, Value As Object)
    Dim pnl As B4XView = CustomListView1.GetPanel(Index)
    pnl.Color=xui.Color_Blue
'    pnl.Color=Colors.Red
End Sub
 
Upvote 0

mfstuart

Active Member
Licensed User
Hi Mahares,
To use your code, which I have, you'll need to "clear" the selected row color in the CustomListView_ItemClick event, as such:

B4X:
Sub CustomListView_ItemClick (Index As Int, Value As Object)
    CustomListView_Clear    'resets all the panel colors to white
    
    Dim pnl As B4XView = CustomListView.GetPanel(Index)
    pnl.Color=xui.Color_Blue
    'pnl.Color=Colors.Red
End Sub

B4X:
Sub CustomListView_Clear
    For i = 0 To CustomListView.Size - 1
        Dim p As Panel = CustomListView.GetPanel(i)
        p.Color = Colors.White
    Next
End Sub

Next selection will show the panel color as Blue and all other rows as White.

Thanx for the code Mahares :)
Mark Stuart
 
Upvote 0

mangojack

Well-Known Member
Licensed User
To use your code, which I have, you'll need to "clear" the selected row color in the CustomListView_ItemClick event ....

Another option would be ... Create a global var to hold the index of previously clicked item (which is also updated in the Click_event)
You would then just reset the color of this clv panel(Index). It then alleviates the need to loop thru all items.
 
Upvote 0

mfstuart

Active Member
Licensed User
So the changes would be: (thanx MangoJack)
B4X:
Sub Globals
    Dim pIndex as Int
End Sub

B4X:
Sub CustomListView_ItemClick (Index As Int, Value As Object)
    CustomListView_Clear    'resets the prevously selected panel colors to white
   
    Dim pnl As B4XView = CustomListView.GetPanel(Index)
    pnl.Color=xui.Color_Blue
    'pnl.Color=Colors.Red
    pIndex = Index
End Sub

B4X:
Sub CustomListView_Clear
    If pIndex >= 0 Then
        Dim pnl As Panel = CustomListView.GetPanel(pIndex)
        pnl.Color = Colors.White
    End If
End Sub
 
Last edited:
Upvote 0

mangojack

Well-Known Member
Licensed User
So the changes would be: (thanx MangoJack)

Yes .. and the way I see it , you probably could get rid of the index check.
Even if there has been no prior click .. it will just re-color clv panel(0) .. first panel.

But on the other hand your index check is probably more ... safer ?

ps: CustomListView ... clvPools ? I am sure you are aware and this is just sample code.... maybe you could correct the last post for future views.
 
Last edited:
Upvote 0
Top