B4A Class [B4X] SnapCLVAdvanced - Move to the nearest item - Instant snap

I took the original CLVSnap code from @Erel and modified it to create a new class.

With this class you can decide if you want to have an instant snap or if you want the list to snap only when the list stops moving.

I spend a lot of time in creating views, some views i need by my self, but some views not and to create a high quality view cost a lot of time. If you want to support me, then you can do it here by Paypal or with a coffee.

It is compatible with B4A, B4i and B4J.
Note that you need to call snap.ScrollChanged from the ScrollChanged event.
On B4I you need GestureRecognizer Download the .bas file in the attachment

B4X:
AdvancedSnap.InstantSnap = True
Default: False
If False then the snap works like the original CLVSnap Class.



Example:
    For i = 0 To 50 -1
   
        Dim xpnl As B4XView = xui.CreatePanel("")
        xpnl.Color = Rnd(0xff000000, 0xffffffff)
        xpnl.SetLayoutAnimated(0, 0, 0, CustomListView1.AsView.Width, Rnd(200dip, 300dip))
        CustomListView1.Add(xpnl, "")
   
    Next
 
    AdvancedSnap.Initialize(CustomListView1)
    AdvancedSnap.InstantSnap = True

...
Sub CustomListView1_ScrollChanged (Offset As Int)
    AdvancedSnap.ScrollChanged(Offset)
End Sub

SnapCLVAdvanced
Author: Alexander Stolte
Version: 1.00

Changelog

  • 1.00
    • Release
Have Fun
 

Attachments

  • GestureRecognizer.bas
    11.2 KB · Views: 322
  • Example CLVSnapAdvanced.zip
    10.3 KB · Views: 353
  • CLVSnapAdvanced.bas
    4.6 KB · Views: 272
  • CLVSnapAdvanced.b4xlib
    1.7 KB · Views: 289
Last edited:

mcqueccu

Well-Known Member
Licensed User
Longtime User
Hi @Alexander Stolte Thanks for a great Library as always.

I get this error using the library with Customlistview in horizontal mode

B4X:
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 110 (CLVSnapAdvanced)
java.lang.ClassCastException: anywheresoftware.b4a.objects.HorizontalScrollViewWrapper$MyHScrollView cannot be cast to android.widget.ScrollView
    at b4a.example.clvsnapadvanced$ResumableSub_HandleTouch.resume(clvsnapadvanced.java:333)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1717)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
** Activity (main) Pause event (activity is not paused). **
** Service (starter) Destroy (ignored)**
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
Download again
Downloaded. Horizontal mode working fine. There is a new Error though.
When you have one item in the list, or you have multiple items and you scroll to the last time, you get an index out of bounds error.

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 107 (CLVSnapAdvanced)
java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.get(ArrayList.java:411)
    at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
    at b4a.example3.customlistview._getrawlistitem(customlistview.java:447)
    at b4a.example.clvsnapadvanced$ResumableSub_HandleTouch.resume(clvsnapadvanced.java:348)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1717)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6190)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
** Activity (main) Pause event (activity is not paused). **
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…