Android Question Odd Anchors Behaviour with ViewPager

Haris Hafeez

Active Member
Licensed User
Longtime User
Hello All,

I've been having an odd issue with anchors in a layout. In my understanding, the anchors are relative to the containing view or activity. This is not happening in my project and I have reproduced this in my test project, attached here.

One thing I do want to mention is that my app uses AHViewPager and this is to blame I think.

Can someone please take a look and let me know what might be causing this when used in AHViewPager? Is there a workaround?

Layout screenshot:
upload_2017-1-13_19-10-58.png


Screenshot of the layout on runtime in the app:
upload_2017-1-13_19-13-11.png



Kind Regards,
Haris.
 

Attachments

  • upload_2017-1-13_19-11-53.png
    upload_2017-1-13_19-11-53.png
    36.6 KB · Views: 308
  • AnchorsPanel.zip
    88.3 KB · Views: 317

Haris Hafeez

Active Member
Licensed User
Longtime User
Yes and the result was the same. I've only been working on Android 6.1 emulator and 7.1 device.

One thing I will check is if the view pager sets its width as multiple of the screen width based on the number of pages. Still a guess at this point as I'm away from my work place at the moment.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
The problem is that you load the layout too early.

In your code, when you add the panel to the pagecontainer the pagecontainer isn't even attached to the ViewPager so the Panel has no dimensions.

The correct time to load a layout for the page is in the ViewPager_PageCreated event. Be sure to remove all Views from the page panel again in the ViewPager_PageDestroyed event or if you want to cache the pages make sure that you load them only once in PageCreated.

B4X:
Sub VP_PageCreated (Position As Int, Page As Object)
    Dim p As Panel = Page
    p.LoadLayout("ContentLayout")
End Sub

Sub VP_PageDestroyed (Position As Int, Page As Object)
    Dim p As Panel = Page
    p.RemoveAllViews
End Sub
 
Last edited:
Upvote 0
Top