B4A Library [B4X] [XUI] AS ViewPager based on xCustomListView

Hey,
thanks to @KZero for his good zPager class, i was able to extract a few things from it to make this cross platform view.

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. :)

This library is compatible and tested with B4A,B4I and B4J!
If you have bugs or a wish then tell me it in the comments.

On B4J use the LEFT and RIGHT keys on your keyboard to change the page.
On B4I you need the GestureRecognizer class. Download it down below.
B4j: jXUI,JavaObject,xCustomListView,jReflection
B4a: XUi,Reflection,xCustomListView
B4i: iXUI,xCustomListView
Features
  • cross-platform compatible
  • based on a cross-platform ListView
  • add and remove pages
  • scroll to pages
  • a good swipe feeling
  • NEW - Vertical
  • NEW - Carousel
Horizontal and Vertical
20-04-22-20-36-012.gif
20-08-15-11-33-40.gif

B4A Carousel and B4I Carousel
B4A Carousel.gif
B4I Carousel.gif

ASViewPager
Author: Alexander Stolte
Version: 1.20

  • ASViewPager
    • Events:
      • PageChange (Index As Int)
      • PageChanged (Index As Int)
      • PageClick (Index As Int, Value As Object)
      • ReachEnd
      • ScrollChanged (Offset As Int)
      • TouchBegin
      • TouchEnd
    • Fields:
      • Tag As Object
    • Functions:
      • AddPage (xpnl_layout As B4XView, value As Object) As String
      • AddPageAt (index As Int, xpnl_layout As B4XView, value As Object) As String
        Adds an page at a special position
      • Base_Resize (Width As Double, Height As Double) As String
      • Class_Globals As String
      • Clear As String
      • DesignerCreateView (Base As Object, Lbl As Label, Props As Map) As String
        Base type must be Object
      • getBase As B4XView
      • getCurrentIndex As Int
      • getCustomListView As b4a.example3.customlistview
      • getisScrollEnabled As Boolean
        Checks if the swipe/scroll is enabled or disabled
      • getSize As Int
      • Initialize (Callback As Object, EventName As String) As String
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • NextPage As String
      • NextPage2 As String
        Jump to next page
      • PreviousPage As String
      • PreviousPage2 As String
        jump to previous page
      • RemovePage (index As Int) As String
      • setCurrentIndex (index As Int) As String
      • setCurrentIndex2 (index As Int) As String
        sets the current index - jumps to the item
      • setIgnorePageChangedEvent (ignore As Boolean) As String
      • setIgnorePageChangeEvent (ignore As Boolean) As String
      • setScroll (enabled As Boolean) As String
        B4I and B4A only
        enabled - False the scroll is deactivated
    • Properties:
      • Base As B4XView [read only]
      • CurrentIndex As Int
      • CurrentIndex2
        sets the current index - jumps to the item
      • CustomListView As b4a.example3.customlistview [read only]
      • IgnorePageChangedEvent
      • IgnorePageChangeEvent
      • isScrollEnabled As Boolean [read only]
        Checks if the swipe/scroll is enabled or disabled
      • Scroll
        B4I and B4A only
        enabled - False the scroll is deactivated
      • Size As Int [read only]
Changelog
  • 1.00
    • Release
  • 1.01
    • Base_Resize is now public
    • NextPage and PreviousPage Bug Fix
    • setCurrentIndex Bug Fix
    • PageChangeEvent Bug Fix
  • 1.02
    • B4I Bug Fixes
    • NextPage and PreviousPage Bug Fix
  • 1.03
    • Resize BugFix
  • 1.04
    • B4I Page-Height BugFix
    • B4I Page-Swipe BugFix
  • 1.05
    • BugFix
  • 1.06
    • Add Designer Property "Orientation" - Vertical is now supported
  • 1.07
    • BugFix getCurrentIndex
    • BugFix PageChanged event is now only firing if the index is changed
    • Add setIgnorePageChangedEvent Property
    • Add TouchBegin Event
    • Add TouchEnd Event
    • BugFix PageChanged is now firing if you press on a emulator, for example the arrow up or down keys on yor keyboard
  • 1.08
  • 1.09
    • B4I BugFixes for Release Mode - swiping is now better
  • 1.10
    • Add AddPageAt (experimental it works for my need, if you have issuses, then tell me) - Adds an page at a special position
    • Add set CurrentIndex2 - sets the current index - jumps to the item
    • Better resize handling
  • 1.11
    • RemovePage Bugfixes
    • B4J ScrollPane under the hood is now Transparent
    • Add NextPage2 -Jump to next page
    • Add PrevoiusPage2 - Jump to previous page
  • 1.12
    • [BETA] Add DesignerProperty Carousel - infinite swipe
      • On B4I Bounce Effect is disabled
    • B4I ScrollView Paging is now activated, this can improve the handling
  • 1.13
    • Add PageClick Event
  • 1.14 - read more about this update here
    • [B4I only] Add Scroll - enable or disable scroll with finger
      • ASViewPager1.Scroll(False)'disable scroll with finger
  • 1.15
    • Intern Function IIF renamed to iif2
  • 1.16
    • set Scroll is now B4X - disable the swipe/scroll
      • In B4J the arrow keys (Left/Right/Up/Down) are disabled if you deactivate the swipe/scroll
    • New property isScrollEnabled - checks if the swipe/scroll is enabled or disabled
    • Intern Function iif2 removed and the core iif is now used
      • B4A V11+ - B4J V9.10+ - B4I V7.50+
  • 1.18
    • PageChanged Event is now firing in some cases with a delay, because of a scroll animation
    • BugFix B4A setScroll - Events from other views are now no longer blocked
  • 1.19
    • BugFix on NextPage and PreviousPage
  • 1.20 (read more about this update)
    • Add Event PageChange - This event is triggered immediately after the page is changed, no delay because of animations
    • Add setIgnorePageChangeEvent - prevents the PageChange event from being triggered
  • 1.21
    • BugFixes
  • 1.22
    • B4I Scroll = False BugFix
  • 1.24
    • BugFix AddPageAt is now working better on B4A
Have Fun :)
 

Attachments

  • B4X Example.zip
    9.9 KB · Views: 418
  • GestureRecognizer.bas
    11.2 KB · Views: 218
  • ASViewPager.b4xlib
    5 KB · Views: 84
Last edited:

Alexander Stolte

Expert
Licensed User
Update
  • 1.20
    • Add Event PageChange - This event is triggered immediately after the page is changed, no delay because of animations
    • Add setIgnorePageChangeEvent - prevents the PageChange event from being triggered
PageChange
The event is triggered as soon as the action to change the page is received. e.g. The user releases the viewpager and the page is changed, as soon as the user releases, this event is triggered, even before scrolling. So things can be done in advance, before the complete page is visible.
PageChanged
With the event you can be sure that the page is done scrolling and the page has changed. This may be necessary if you do some things in the background on the viewpager, for example, dynamically add another page, without that the scrolling should be affected.
setIgnorePageChangeEvent
Sometimes you don't always need an event, then just dynamically set this property to false
 

Alexander Stolte

Expert
Licensed User
When sliding to the sides the tab line has a little delay, I liked it better before.
But this was necessary, because I had performance losses with my ASCalendarAdvanced after the page was changed, because I dynamically add more pages in the background and therefore the scrolling looked not so good.

Use the PageChange event.
 

angel_

Well-Known Member
Licensed User
So if you got it right, if I want the bar to move the same as the movement, I have to do this:

B4X:
Sub ASViewPager1_PageChange(Index As Int)
    TabStrip.ViewPager_PageChanged(Index)
End Sub

B4X:
Sub ASViewPager1_PageChanged(Index As Int)
'Code at the end of the action (end of scrolling)
End Sub
 

angel_

Well-Known Member
Licensed User
I think I explained myself wrong, this code works fine:

B4X:
Sub ASViewPager1_PageChange(Index As Int)
    TabStrip.ViewPager_PageChanged(Index)
End Sub

So I can use PageChange unless I have performance issues in transition in that case I should use PageChanged, is this interpretation correct?
 

angel_

Well-Known Member
Licensed User
I have noticed that when you swipe with rapid movement instead of going to the next tab it goes to the last

B4X:
Sub ASViewPager1_PageChange(Index As Int)
    TabStrip.ViewPager_PageChanged(Index)
    'code...
End Sub
 

angel_

Well-Known Member
Licensed User
Not every swipe the user makes should also go to the next page, if the gesture is too short or too fast, then the process stops.
With a newer device (Oneplus 8T, android 11) it is quite fluid but some lag in some demanding transition, with another older device (Oneplus one, android 10) the movement is erratic, with fast movements it jumps several tabs and goes to the last, sometimes it does not jump to the next tab as you say
 

wimpie3

Well-Known Member
Licensed User
Suppose you have loaded a page from a layout, like this:
B4X:
    Dim page1 As B4XView=xui.CreatePanel("")
    page1.LoadLayout("layout_page1")
    page1.SetLayoutAnimated(0,0,0,ASViewPager1.Base.Width,ASViewPager1.Base.Height)
    ASViewPager1.addpage(page1,"")

And later on in your code, you remove that page with
B4X:
ASViewPager1.RemovePage(1)

What happens with the variables used in layout_page1? Will they still exist in the activity or will they be empty?
 

angel_

Well-Known Member
Licensed User
Unfortunately still not working well, it takes a very long swipe almost from side to side to get it to the next tab
 

Alexander Stolte

Expert
Licensed User
In release mode it seems to work fine
You don't publish your app in debug mode... That's why Erel has been preaching "test it in release mode first" for years. In debug mode everything runs slowly. You can scroll a list in debug mode, or let it scroll, you will see, it is extremely jerky.

Even on my Iphone 12 Pro max I have problems to get to a next page in debug mode.
I'll test it later with my old 5s and I can tell you right now, in release mode, it will run just as fast as on the 12 pro.
 

angel_

Well-Known Member
Licensed User
When I do a quick swipe up (release mode, B4i) it jumps to the next tab, can it be disabled?
 
Top