B4J Tutorial Synchronize ListView Scrolling

I needed to keep two ListViews scrolling in sync, so searching the internet I found this: http://stackoverflow.com/questions/16937470/listen-to-scrollbar-change-in-listview

which I have ported to B4j, and added a few extra tweaks. There is a bit of JavaObject jiggery pokery like using a Java Iterator, and setting up a value change listener.

It also syncs the selection of the items between the two lists.

The example syncs the Vertical scroll bar, it is also possible to sync the Horizontal scroll bar or both.


I thought I'd share as it may be useful to someone.

Requires JavaObject.
 

Attachments

  • LVSyncClass.zip
    2.8 KB · Views: 435
  • LVSync-2.zip
    3.1 KB · Views: 397
Last edited:

stevel05

Expert
Licensed User
Longtime User
I've created this functionality as a class (added to the first post), you now just need to Create a ListViewControl Object:

B4X:
Private LVC As ListViewControl

Initialize it with an array of listview objects and the orientation you want to control:

B4X:
    'Add the listviews to be synced to the list, the first added is assumed to be the master and will display the scroll bar
    LVC.Initialize(Array As ListView(ListView2,ListView3,ListView1),"VERTICAL")

And add two Subroutines for each ListView added to the control in the module that creates the ListViewControl Object:

B4X:
Sub ListView1_SelectedIndexChanged(Index As Int)
    LVC.SelectedIndexChanged(Index,Sender)
    'Add additional code below here
End Sub
Sub ListView1_MousePressed (EventData As MouseEvent)
    LVC.MousePressed(Sender)
    'Add additional code below here
End Sub

The Calls to the ListViewControl in the first line of the subs are required for it to function.

The code has been improved to handle the scrollwheel , scrolling on any of the lists will move them all.
 

stevel05

Expert
Licensed User
Longtime User
Unfortunately not, it would have to be written specifically for B4a.
 
Top