B4A Library AHViewPager library - Sliding panels now perfect

This library is a B4A implementation of the ViewPager object provided by the compatibility class from google. With this library it is possible to add sliding panels to your apps.

AHPageContainer - This object is a container for the pages of the AHViewPager. You should add Panels to this object.

AHViewPager - This is the main object which provides the sliding panels.

All the Tabs objects are deprecated now. You can use them but I don't enhance them anymore. For nice looking Tabs please use the DSTabLayout object of the DesignSupport library.
Alternatively you can use the TabStripViewPager.


Installation:
  • From V3.00 on the library requires B4A 6.0 or later.
  • This library depends on the Android Support library so you need to install the Android Support repository in the Extras section with SDK Manager.
  • Copy the AHViewPager.xml and AHViewPager.jar files to your additional libraries folder.
Example project:

For an example project for this library see the Design Support Library.

Your support:
Creating libraries and wrappers for existing library projects is a lot of work. The use of this library is totally free and you even don't need to mention in your app that you use it.
But if you use this library in your projects and you think it is useful to you please consider to make a donation:

Thanks very much for your support.

Version History:

1.00:
- initial version

1.01:
- Added AHViewPagerFixedTabs object for a fixed tab indicator
- Fixed a bug that it was impossible to get the canvas of a panel which was added to the container (It was impossible to display charts on a page)
- Example for AHViewPagerFixedTabs tab indicator

1.02: (Never released)
- Fixes problem with tab height (Line is now always on the bottom)

1.03:
- Fixes problem with Line sometimes not updating correctly on Android 4.0+
- Fix UppercaseTitle property

2.0:
- Complete Rewrite of the Swipey tabs code to fix some bugs with newer android versions.

2.01:
- New property for the AHViewPager object: PagingEnabled - Enables or disables paging for the ViewPager.

2.02:
- New events PageScrolled and PageScrollStateChanged (with SCROLLSTATE constants)
See post 227 for details

2.20:
- Use raiseEventfromUI() in most events
- New SupportTabs object
- New SupportTitles object
- Support for Designer (as a Custom View)
- some minor internal changes and bug fixes

2.21:
- Use raiseEvent() for PageCreated and PageDestroyed event again to prevent double UI refreshes
- Added @RaiseSynchronousEvents annotation to GotoPage() (to make it better compatible with rapid debugger.

3.00:
- Support for Designer Custom Properties.
- Uses maven repository to access support library. (B4A 6.0 or later required)

Attention: V2.20 has an API change which makes the ViewPager incompatible with older versions. The AHPageContainer parameter is removed from the Initialize() method. Initialize2() has the old signature. If you use Initialize() (or add the View to the Designer as a custom view) then you have to set the container object with ViewPager.PageContainer = <YourContainer>

The examples use the old Initialize method, so they are broken. I was too lazy to fix them because I recommend the tab indicator from the Design Support library for a nice looking Material Design app.
 

Attachments

  • screenshot-1336034831196.png
    screenshot-1336034831196.png
    10.1 KB · Views: 8,302
  • screenshot-1336034966953.png
    screenshot-1336034966953.png
    9.9 KB · Views: 7,568
  • AHViewPager2_21.zip
    39.8 KB · Views: 2,541
  • AHViewPager3_00.zip
    74.9 KB · Views: 2,496
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
This library is a B4A implementation of the ViewPager object provided by the compatibility class from google. With this library it is possible to add sliding panels to your apps with a very nice tab selector and page indicator like in some google apps (Market etc)

There are four objects in this library:

AHPageContainer - This object is a container for the pages of the AHViewPager. You should add Panels to this object.

AHViewPager - This is the main object which provides the sliding panels.

AHViewPagerTabs - This (optional) object provides a nice tab selector and indicator for the sliding panels. This object is based on the ViewPagerTabs implementation from Andreas Stütz and you can find it here.

AHViewPagerFixedTabs - This (optional) object provides a nice tab selector with fixed tabs. This object is based on the ViewPagerExtensions library from Andreas Stütz See here
For a detailed usage explanation see the example project.

Installation:
You need at least B4A 1.8.
Copy the jar file and the xml file to your custom libs folder.
Copy the android-support-v4.jar from the Android SDK to your custom libs folder (see below)

Attention: This library uses the android-support-v4.jar library. You have to install it with the Android SDK Manager. Please add the Extras/Android Support package. Then you will find the jar under <SDK root folder>\extras\android\support\v4. Please copy the android-support-v4.jar file to your custom libs folder!

Version History:

1.00:
- initial version

1.01:
- Added AHViewPagerFixedTabs object for a fixed tab indicator
- Fixed a bug that it was impossible to get the canvas of a panel which was added to the container (It was impossible to display charts on a page)
- Example for AHViewPagerFixedTabs tab indicator

1.02: (Never released)
- Fixes problem with tab height (Line is now always on the bottom)

1.03:
- Fixes problem with Line sometimes not updating correctly on Android 4.0+
- Fix UppercaseTitle property

2.0:
- Complete Rewrite of the Swipey tabs code to fix some bugs with newer android versions.

2.01:
- New property for the AHViewPager object: PagingEnabled - Enables or disables paging for the ViewPager.

2.02:
- New events PageScrolled and PageScrollStateChanged (with SCROLLSTATE constants)
See post 227 for details

Hi @corwin42
I'm using GetPageObject() method (AHPageContainer).
The method return a object (as object) that is a panel (I checked it with "object is panel" statement and I got a true).

How I can get the returned object as a Panel class in order to access to the props and methods of the Panel ?

Thanks!
 

Kevin

Well-Known Member
Licensed User
Longtime User
Should be possible with reflection:

B4X:
Sub SetOffscreenLimit(Pager As Object, Value As Int)
    Dim r As Reflector
  
    r.Target = Pager
    r.Target = r.RunMethod("getObject")
    r.RunMethod4("setOffscreenPageLimit", Array As Object(Value), Array As String("java.lang.int"))
End Sub

I haven't tested the above code but it should work.

I seem to get an error trying this code.

java.lang.NoSuchMethodException: getObject []

Any ideas what may be wrong?
 

Kevin

Well-Known Member
Licensed User
Longtime User
I seem to get an error trying this code.



Any ideas what may be wrong?

Nevermind! I got it working with JavaObject:

B4X:
Sub SetOffscreenLimit(oPager As Object, Value As Int)
   
    Dim jo As JavaObject = oPager
    jo.RunMethod("setOffscreenPageLimit", Array As Object(Value))

End Sub
 

iwan.deejay

Member
Licensed User
Longtime User
Is it just me or AHViewPager is incompatible with android-support-v4 revision 21?

Because when I tried to use rev. 21, it throws an exception.

Thanks
 

corwin42

Expert
Licensed User
Longtime User

iwan.deejay

Member
Licensed User
Longtime User
NoClassDefFoundException,
And it occurs at initialization. It says that the AHPageContainer caused the exception.
 

Kevin

Well-Known Member
Licensed User
Longtime User
For what it's worth, I just updated my "android-support-v4.jar" to revision 21 in order to test this because I recently have added AHViewPager to my project (great so far!). After copying the latest version to my extra libs folder I tried my app and it worked without any errors.

I can't really help with the error iwan is seeing but it would appear that it likely is not related to revision 21 of the support jar.
 

gelay

Member
Licensed User
Longtime User
Hi guys

I'm trying to set the tabs over the pager layer with Pager.SendToBack or Tabs.BringToFront, but after app start "Unfortunately, xy has stopped" follows.

here is my code:

B4X:
  'Now we have a container with our panels just add it to the pager object
       Pager.Initialize(Container, "Pager")
   
       'As we want to show the tabs page indicator, we initialize it
           Tabs.Initialize(Pager)
      
           Tabs.TextSize = 18
           Tabs.LineHeight = 5dip
        Tabs.LineColorCenter = Colors.Blue   
        Tabs.TextColor = Colors.Gray
        Tabs.TextColorCenter = Colors.White
   
    Activity.AddView(Tabs, 0dip, 0dip, 100%x, 100%y)
               
               
               
       'We add a line below the tab page indicator because it looks good
    Dim Col As ColorDrawable
   
        Col.Initialize(Colors.Blue, 0)
   
       Line.Initialize("")
       Line.Background = Col
   
                      
   
       'Now we can add the pager to the activity
       Activity.AddView(Pager, 0dip, 0dip, Activity.Width, Activity.Height )
       Activity.AddView(Line, 0dip, 35dip, Activity.Width, 2dip)
       'Pager.SendToBack
       Tabs.BringToFront

Debug:

java.lang.ArithmeticException: divide by zero
at de.amberhome.viewpager.ViewPagerTabs.higlightTab(ViewPagerTabs.java:301)
at de.amberhome.viewpager.ViewPagerTabs.offsetChildren(ViewPagerTabs.java:579)
at de.amberhome.viewpager.ViewPagerTabs.onPageScrolled(ViewPagerTabs.java:559)
at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1717)
at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1655)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:586)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1605)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at anywheresoftware.b4a.BALayout.onLayout(BALayout.java:41)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1673)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2035)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1792)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1047)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5896)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
to android.os.Handler.handleCallback (Handler.java:733)
to android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)


Any suggestions?

Thanks
Krisztian
 

Zdravko Filipovic

Member
Licensed User
Longtime User
I also have this error (I'm trying to start examples from page 1):
java.lang.ClassNotFoundException: de$amberhome$viewpager$AHPageContainer

android-support-v4.jar is version 21.0.1 (there is no older).

Basic4Android is v3.50.
 

corwin42

Expert
Licensed User
Longtime User
I also have this error (I'm trying to start examples from page 1):
java.lang.ClassNotFoundException: de$amberhome$viewpager$AHPageContainer

android-support-v4.jar is version 21.0.1 (there is no older).

Basic4Android is v3.50.

My guess: You didn't copy the AHViewPager.jar to your custom libs folder.
 

corwin42

Expert
Licensed User
Longtime User
AHViewPager.jar and AHViewPager.xml are in the libs folder. Also android-support-v4.jar is copied there.
The error you posted means that AHPageContainer class is not found. This class is in the AHViewPager.jar. So it seems that it is not added to the apk for some reason. AHViewPager has to be checked on the libraries tab and the jars should be placed in the Custom Libs folder.

I don't know if B4A 3.50 has some issues with the newest build tools. I suggest using always the newest version (currently 3.82)
 

corwin42

Expert
Licensed User
Longtime User

Martin Larsen

Active Member
Licensed User
Longtime User
Can the viewpager pages be created on the fly when the number of items is not known beforehand (or is very large)?

That is possible in classic Android. An example is Gmail where you can swype through potentially thousands of emails using the viewpager. And these are not created beforehand.
 
Top