Android Example Highlight selection in a standard ListView

I found some code on the internet that works with a standard ListView, an example is attached. I use ListView's all the time with large lists >3000 items and I will find this one useful.

It uses StateListDrawable. Code is:

Code:
Dim CDPressed,CDNormal AsColorDrawable
CDPressed.Initialize(Colors.Gray,0) ' Normal color CDNormal.Initialize(Colors.Yellow,0) 'Selected color Dim SLD AsStateListDrawable
SLD.Initialize
SLD.AddState(SLD.State_Pressed,CDPressed)
SLD.AddState(-SLD.State_Pressed,CDNormal)
Dim LVO As JavaObject = ListView1
LVO.RunMethod("setSelector",ArrayAsObject(SLD))

ListView1.FastScrollEnabled=True
Requires the new JavaObject Library
 

Attachments

  • ListSelectColor.zip
    7.1 KB · Views: 1,706

stevel05

Expert
Licensed User
Longtime User
Hadn't noticed that,will check it out
 

stevel05

Expert
Licensed User
Longtime User
That's really odd, it doesn't happen if you scroll slowly :confused:
 

stevel05

Expert
Licensed User
Longtime User
I've had the time to play with this a little more and have recreated this as a Library as it needed some massaging to get it to display a little better. It's still not perfect. That's what you get for picking up code from the internet I suppose, it seemed like a good idea.

Basically, if the item is currently displayed, the highlighting is turned on, of not it's turned off. the original suffered from the fact that once the item had gone from the screen, it was no longer drawn (or not) and the highlight was left behind.

The new code required is reduced a little as more is done in the library:

B4X:
    Dim CDPressed,CDNormal As ColorDrawable
    CDPressed.Initialize(Colors.Blue,0)  ' Normal color
    CDNormal.Initialize(Colors.Yellow,0) 'Selected color
   
    Dim SV As SLLVOnScroll
    SV.Initialize(ListView1,"ListView1",CDPressed,CDNormal)
   
   
    For i = 0 To 1000
        ListView1.AddSingleLine("SingleLine #"&i)
        ListView1.AddTwoLines("TwoLines","#"&i)
        ListView1.AddSingleLine2("Singleline2 #"&i,"SL2 "&i)
        ListView1.AddTwoLines2("TwoLines2","#"&i,"TL "&i)
    Next
   
    ListView1.FastScrollEnabled=True

You need to provide the ListView name, the eventName ,the BackGround and Foreground drawables for the highlight.

The eventName is required as the Library HiJacks the onItemClick Listener so it can find out which item is touched before the highlight is displayed. It then sends the onItemClick to the eventName&"_ItemClicked" as normal.
 

Attachments

  • SLLVOnScroll-App.zip
    7.1 KB · Views: 973
  • SLLVOnScroll.zip
    4.6 KB · Views: 992

NJDude

Expert
Licensed User
Longtime User
Now it's better, but, the highlight disappears if you use the fast scroll tab, however, if you scroll slowly the highlight magically appears again.

The steps I followed:

1- Run the app
2- Highlight the first item
3- Scroll fast up and down not using the fast scroll tab (the highlight remains as it should), no problem so far.
4- Scroll using the fast scroll tab
5- Go back to the first item, NO HIGHLIGHT
6- Slowly scroll, the highlight appears again on the first item.

I would also suggest you change the name to something more meaningful than SLLVOnScroll.

Keep up the good work.
 

le_toubib

Active Member
Licensed User
Longtime User
The eventName is required as the Library HiJacks the onItemClick Listener so it can find out which item is touched before the highlight is displayed. It then sends the onItemClick to the eventName&"_ItemClicked" as normal.[/QUOTE]

it doesn't seem to be very sensitive to click event
 
Last edited:

GuerillaProgrammer

Member
Licensed User
Longtime User
Hi all,
This works really well with only one exception that I see, but it is a serious one. If you drop your finger and lift it, it works perfectly. However, if you drop your finger and then slide away after the item is highlighted, the item is highlighted but not selected(no "click" occurs). This is very bad because now the user thinks he has selected a(n) (new) item because it is highlighted, but the item selected is not the (new) one that he just touched.

Any workarounds or fixes?

Also, I too would seriously appreciate an answer to Rick's question.

Thanks for all your hard work and help.
 

stevel05

Expert
Licensed User
Longtime User
I haven't looked at this for a long time, the code I based it on was not totally compatible with the structure we have. As we are all learning, I may now have simpler way to do this which I'll put on my list to look at. I can't promise when this will be though. It currently doesn't respond to setting the selection by code.
 

mshafiee110

Active Member
Licensed User
Longtime User
I found some code on the internet that works with a standard ListView, an example is attached. I use ListView's all the time with large lists >3000 items and I will find this one useful.

It uses StateListDrawable. Code is:

Code:
Dim CDPressed,CDNormal AsColorDrawable
CDPressed.Initialize(Colors.Gray,0) ' Normal color CDNormal.Initialize(Colors.Yellow,0) 'Selected color Dim SLD AsStateListDrawable
SLD.Initialize
SLD.AddState(SLD.State_Pressed,CDPressed)
SLD.AddState(-SLD.State_Pressed,CDNormal)
Dim LVO As JavaObject = ListView1
LVO.RunMethod("setSelector",ArrayAsObject(SLD))

ListView1.FastScrollEnabled=True
Requires the new JavaObject Library
tnx :)
 

Smee

Well-Known Member
Licensed User
Longtime User
I haven't looked at this for a long time, the code I based it on was not totally compatible with the structure we have. As we are all learning, I may now have simpler way to do this which I'll put on my list to look at. I can't promise when this will be though. It currently doesn't respond to setting the selection by code.
Did this problem ever get solved?
 
Top