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,093
  • screenshot-1336034966953.png
    screenshot-1336034966953.png
    9.9 KB · Views: 7,347
  • AHViewPager2_21.zip
    39.8 KB · Views: 2,349
  • AHViewPager3_00.zip
    74.9 KB · Views: 2,267
Last edited:

Nostrildumbass

Member
Licensed User
Longtime User
Odd behavior I can't figure out:

If I scroll to tab 3 or 4 in my Activity that contains the AHViewPager and hit back (Activity paused) then press the button on the previous screen that starts the AHViewPager Activity, I get a crash with error:

B4X:
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x2

If I comment out the GoToPage(CurrentPage) line in Activity_Resume, the crash doesn't happen, however I'd like for the last visited tab to load again.

Update: Hmm, adding "DoEvents" to the top of my Activity_Resume seems to be a workound, however I can see the tabs changing.
 

nemiroG1

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 know this is quite old, but it is the best reference I have found for this. It errors out on the r.Target = r.RunMethod("getObject") line. Any suggestions? I'd like to increase the number of screens in memory to five. Thanks!
 

nemiroG1

Member
Licensed User
Longtime User
Not sure why I did not get this worked out earlier, but here is the code, and it does work. Only a slight modification from the original example. Thanks, corwin42!

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

corwin42

Expert
Licensed User
Longtime User
Not sure why I did not get this worked out earlier, but here is the code, and it does work. Only a slight modification from the original example. Thanks, corwin42!

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

Thank you for answering your question yourself. :)

Yes, this is correct. Because Pager1 is submitted "as Object" the compiler unwraps the Object automatically and so you don't need the getObject() call. Just for the technical background.

In short: Your answer is absolutely correct.
 

ArminKH

Well-Known Member
Tnx 4 sharing this useful lib
Works like a charm...
please let me to know if it is possible to have animation during changing pages by using pager.GotoPage for example
I read all 21 pages of this thread and my searches came up with noting for any relevant questions
Tnx again
 

corwin42

Expert
Licensed User
Longtime User
Tnx 4 sharing this useful lib
Works like a charm...
please let me to know if it is possible to have animation during changing pages by using pager.GotoPage for example
I read all 21 pages of this thread and my searches came up with noting for any relevant questions
Tnx again

Don't know if this is what you want:

if you use pager.GotoPage(pagenumber, True)

the ViewPager scrolls/animates to the new page. If you set the second parameter to False the ViewPager jumps to the new page.
 

ArminKH

Well-Known Member
Don't know if this is what you want:

if you use pager.GotoPage(pagenumber, True)

the ViewPager scrolls/animates to the new page. If you set the second parameter to False the ViewPager jumps to the new page.
No my question is about to using animation plus library or b4a new animation future instead your default scroll animation such as alpha or rotate animation when we jump from a page to another by using gotopage
Thank you for your quick response
 

ArminKH

Well-Known Member
And if this is not possible that can be very hopefull if you try to add this on future
I have not any problem now, this is just a wish because i like this lib and you save my time
And thank u again 4 your hard working since 2012 to now for supporting this lib :)
 

ArminKH

Well-Known Member
Is this possible with pager_pagechanged event or If we destroy pages and recreate panels?
I think if we use seperate eventname for each panel we have more control over each panel and when pages changed then we can play an animation for that panel
Is this correct way? Excuse me 4 my bad english
 

ArminKH

Well-Known Member
This is a small sample
I combine your lib and animation library
Click on bottom button and select a page and then that page will be appared with an animation
 

Attachments

  • AHFixedTabExampleWithAnimation.zip
    11.9 KB · Views: 188

jinxaw

Member
Licensed User
Longtime User
Hi,
first of all i have to say that i love this library. Unfortunately, i get this error while my app is starting:
B4X:
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x5
    at android.content.res.Resources.getResourceName(Resources.java:2026)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
    at android.app.BackStackRecord.run(BackStackRecord.java:833)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Everything worked fine until i added the google maps library and with it the resource files.
I believe, that it has to do with this error:
http://stackoverflow.com/questions/...foundexception-unable-to-find-resource-id-0xf

BUT i am not experienced enough to proove my theory. I would be very thankful for every hint to solve my problem.

Futher information:
I am adding the gmap view to a panel which is part of the pagerviewer.
 
Last edited:

corwin42

Expert
Licensed User
Longtime User
Hi,
Everything worked fine until i added the google maps library and with it the resource files.
I believe, that it has to do with this error:
http://stackoverflow.com/questions/...foundexception-unable-to-find-resource-id-0xf

I don't know if you really get the same error as in the Stackoverflow page. Your stack trace looks similar at the end but on the stackoverflow page the stack trace contains references to the viewpager. I can't see this in your stack trace.

You can try the solution mentioned in the stackoverflow post to set an Id to the pager with reflection.

(Code written from mind and not tested)
B4X:
Dim ref As Reflector
ref.Target = myAHViewPager
ref.RunMethod4("setId", Array As Int(428462864), Array As String("java.lang.int"))

If this does not help I think it may not be related to the AHViewPager library. Then you should open a new Thread in the Questions forum and the best would be to provide an example project that shows the error.
 

jinxaw

Member
Licensed User
Longtime User
I don't know if you really get the same error as in the Stackoverflow page. Your stack trace looks similar at the end but on the stackoverflow page the stack trace contains references to the viewpager. I can't see this in your stack trace.

You can try the solution mentioned in the stackoverflow post to set an Id to the pager with reflection.

(Code written from mind and not tested)
B4X:
Dim ref As Reflector
ref.Target = myAHViewPager
ref.RunMethod4("setId", Array As Int(428462864), Array As String("java.lang.int"))

If this does not help I think it may not be related to the AHViewPager library. Then you should open a new Thread in the Questions forum and the best would be to provide an example project that shows the error.

I would really like to try this. Unfortunately i get the "cannot cast type int to object" error.
 

corwin42

Expert
Licensed User
Longtime User
I would really like to try this. Unfortunately i get the "cannot cast type int to object" error.
Oops, try

B4X:
ref.RunMethod4("setId", Array As Object(428462864), Array As String("java.lang.int"))
 

ArminKH

Well-Known Member
If we use this lib how is possible to user other touch event?for example when i want to create a side menu or use slide switch lib the results is not good
Because when i touch screen for changing side menu state from hiden to show two touch event works together and my side menu opened and page(created by your lib) changed too,but i dont want to change page
Or when i use slide switch lib(a lib for change an state from off to on by using touch or click)when i want change my slide switch state from off to on again 2 touch event works together and again page changed
I report this on slide switch thread
https://www.b4x.com/android/forum/threads/slideswitch-v1-0-1.51749/#content
And you can see attached sample and my complete report on above link
My important question is:how is possible to manage other touch event inside using AhViewPager?because AhViewPager's touch is on top of all touch events eveeeeer
Thank u
 

corwin42

Expert
Licensed User
Longtime User
If we use this lib how is possible to user other touch event?for example when i want to create a side menu or use slide switch lib the results is not good
Because when i touch screen for changing side menu state from hiden to show two touch event works together and my side menu opened and page(created by your lib) changed too,but i dont want to change page
Or when i use slide switch lib(a lib for change an state from off to on by using touch or click)when i want change my slide switch state from off to on again 2 touch event works together and again page changed
I report this on slide switch thread
https://www.b4x.com/android/forum/threads/slideswitch-v1-0-1.51749/#content
And you can see attached sample and my complete report on above link
My important question is:how is possible to manage other touch event inside using AhViewPager?because AhViewPager's touch is on top of all touch events eveeeeer
Thank u

See my answer in the slideswitch library thread.
 

Jerez

Active Member
Licensed User
Longtime User
B4X:
Pager.GotoPage(0, True)

Hi, any way to change page transition speed? change too fast on real devices :)
 
Top