Android Question ScrollView2D

Nickelgrass

Active Member
Licensed User
Longtime User
Hello,
Im having some trouble with the ScrollView2D library.
In my app I have ScrollView2D that is used for a table where there are several columns each as wide as the screen.
When I scroll around I would like to center the scrollview to the nearest column viw smoothscroll. For this I made a sub ScrollChanged.
The first problem is that this event is raised about 20 to 30 times in a go if I scroll around.
The second problem is that I can not set the scrollview position. The App just hangs. I suppose that I somwehow create a never ending loop.

B4X:
Sub sview_ScrollChanged(PosX As Int, PosY As Int)
    Dim pos As Float
    pos = PosX / columnwidth
    If pos < 0.5 Then
        pos = 0'center first column
    Else If pos < 1.5 Then
        pos = 1'center second column
    Else If pos < 2.5 Then
        pos = 2'center third column
    Else If pos < 3.5 Then
        pos = 3'center forth column
    Else If pos < 4.5 Then
        pos = 4'center fith column
    Else If pos < 5.5 Then
        pos = 5'center sixth column
    End If
    sview.SmoothScrollTo((pos*columnwidth), currentY)
DoEvents
End Sub

How could I solve this in a stable manner?
Thanks a lot!
 

sorex

Expert
Licensed User
Longtime User
Never use that viw so I don't know how it behaves...

but can't you simplify it like this?

B4X:
Sub sview_ScrollChanged(PosX As Int, PosY As Int)
    Dim pos As Float
    pos = round(PosX / columnwidth)
    sview.SmoothScrollTo((pos*columnwidth), currentY)
DoEvents
End Sub
 
Upvote 0

Nickelgrass

Active Member
Licensed User
Longtime User
Hi, thanks for the reply. It would be possible to use round. But the columnwidth may vary in future so I decided for this method to be able to adjust the values as needed.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I see, then you might be better of with a width array and a simple loop going through the width and add it to a temp value to detect ranges.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…