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,350
  • AHViewPager3_00.zip
    74.9 KB · Views: 2,268
Last edited:

Inman

Well-Known Member
Licensed User
Longtime User
I have been using your viewpager in pretty much all of my projects since you made it available. But for the first time I am working on a non-English project. I want to use a custom font to display tab titles with foreign characters. Will it be possible for you to add an option to change the typeface of tab titles?
 

yves67

Active Member
Licensed User
Longtime User
how to display a layout on top on a page

Hello,
I am using AHViewPager to display three pages, work fine, but on one page I have a button to display a layout to send a SMS and ask the user to type the text.
question :
1-how can I remove or disable the page after the button has been clicked ?
2-to display the layout I use Activity.LoadLayout statement (correct ? )
3-when the user click on the send button, how to remove the layout and redisplay the page ?

Thanks for any help
Regards

Yves67

ok, j'ai trouvé, avec l'aide de google.... et des panels....
 
Last edited:

corwin42

Expert
Licensed User
Longtime User
Wouldn't it be better to put the "Send SMS" code into a new activity?

Using Activity.LoadLayout somewhere in the activity code is a bad idea. You just add the new layout to the existing one in the activity and don't replace the old one.
 

Inman

Well-Known Member
Licensed User
Longtime User
I have been using your viewpager in pretty much all of my projects since you made it available. But for the first time I am working on a non-English project. I want to use a custom font to display tab titles with foreign characters. Will it be possible for you to add an option to change the typeface of tab titles?

So I take it that this is not possible as of now?
 

ceballot

Member
Licensed User
Longtime User
Solution to outofmemoryerror

if someone has a memory problem using images this code works fine:

B4X:
Sub Pager_PageDestroyed (Position As Int, Page As Object)
   'Log("Page destroyed " & Position)
   
   Dim pan As Panel
   Dim img As ImageView
'   
   pan = Page
   img = pan.GetView(1)

   Dim Obj1 As Reflector
   Obj1.Target = img.Bitmap 
   Obj1.RunMethod("recycle")
   
End Sub
 

alienhunter

Active Member
Licensed User
Longtime User
Hi ,

thanks for sharing this code work very good , only one thing i don't understand is when I load a layout all Edit text/spinners are empty ...:sign0161:
Those i created with the designer

But the code runs perfect in the back, reading form those text edits /spinner

why ?:sign0104:

how do i fill those text edits after the container init

B4X:
container.Initialize

Dim Pan(4) As Panel   

      Pan(0).Initialize("")
      Pan(1).Initialize("")
      Pan(2).Initialize("")
        Pan(3).Initialize("")
      
      
      

             Pan(0).LoadLayout("layout")
          container.AddPageAt(Pan(0), "Information", 0)
             Pan(1).LoadLayout("hol")
             container.AddPageAt(Pan(1), "Page2", 1)
            Pan(2).LoadLayout("int")
         container.AddPageAt(Pan(2), "Page3", 2)
            Pan(3).LoadLayout("sd")
             container.AddPageAt(Pan(3), "Page4", 3)
            
   
   
   'Now we have a container with our panels just add it to the pager object
   pager.Initialize(container, "Pager")



thanks AH
 

Attachments

  • page1.jpg
    page1.jpg
    57.3 KB · Views: 275

CidTek

Active Member
Licensed User
Longtime User
Is there a way to limit the sliding to one direction and intercept a wrong direction?

I have 2 panels and only want to to slide from 2 to 1. I want to neutralize any attempt to slide from 1 to 2 as that action is handled in a click event on panel 1.
 

yttrium

Active Member
Licensed User
Longtime User
Is there a way to limit the sliding to one direction and intercept a wrong direction?

I have 2 panels and only want to to slide from 2 to 1. I want to neutralize any attempt to slide from 1 to 2 as that action is handled in a click event on panel 1.

Perhaps in the PageChanged event handler, detect if the new page is 1.

Then, if so, set
B4X:
pager.PagingEnabled = False

Under your button click that sends it to page 2, then re-enable PagingEnabled.
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I have a strange problem here.

I have a project with 4 pages. Works great on the emulators. (Android 2.3) Now I put it on a generic 7in tablet running Android 4.01 and I get this error below when swiping to the 4th page. I can directly hit the 4th tab and it switches just fine but if I do the swipe it blows chunks.



B4X:
java.lang.RuntimeException: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=186.76654, y[0]=258.46155, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=651148, downTime=650954, deviceId=3, source=0x1002 } recycled twice!
   at android.view.MotionEvent.recycle(MotionEvent.java:1659)
   at android.view.ViewRootImpl.finishMotionEvent(ViewRootImpl.java:2904)
   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2895)
   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
   at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4424)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **

Any ideas?

thanks!
 
Last edited:

JakeBullet70

Well-Known Member
Licensed User
Longtime User
:sign0060:

Found it, It was a DoEvents. Funny how it seemed to only happen on Android 4.x

Thanks so much for the help!!!!
 

corwin42

Expert
Licensed User
Longtime User
There was some changes in internal event handling in Android 4.0.

If I remember correct there is another event raising method for library developers which won't have this problem but it is slower than the other "old" method so I decided to leave it as it is. So it is not possible to do a DoEvent in an event sub. If you need to do longer running calculations with DoEvent then call them with a timer. This should work.
 

corwin42

Expert
Licensed User
Longtime User
Which event causes this issue? Most UI events should be raised with BA.raiseEventFromUI. Only events such as Touch events (which fire rapidly) will be affected by the different way that this method works.

AHViewPager uses the normal BA.raiseEvent() method.

Perhaps you can update the Creating Libraries Thread and explain the four different raiseEventxxx methods and exactly when to use which one?
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hi,
what is this error, compiling the example project?

B4X:
Parsing code.                           0.01
Compiling code.                         0.02
Compiling layouts code.                 0.00
Generating R file.                      0.16
Compiling generated Java code.          Error
B4A line: 67
container.Initialize
javac 1.6.0_30
src\de\amberhome\viewpagerexample\main.java:268: cannot access android.support.v4.view.PagerAdapter
class file for android.support.v4.view.PagerAdapter not found
mostCurrent._container.Initialize(mostCurrent.activityBA);
                      ^
1 error
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I think this might be your problem.

*****
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!
*****

See post #1
 
Top