I understand the lowest number I can give setOffscreenPageLimit(int)
is 1. but I need to load one page at a time because memory problems.
Am i going to have to use the old style tabhost etc? or is there a way/hack I can make my viewPager load one page at a time?
My Adapter extends BaseAdapter with the ViewHolder patern.
1 Answer. Show activity on this post. You cannot do that, the viewpager requires at least one fragment to the left and one to the right. I suggest you move the onResume() logic to a separate method and call it when the fragment becomes visible.
Try pager. setOffscreenPageLimit(1); This will retain one fragment at a time. this will retain in "memory" one however it will create the fragment every time the use swype.
getChildAt(1); will return the current page. But, if you then change back to page 2 (from page 3) your list of children will be in this order page 2, page 3, page 1 which means that ViewPager. getChildAt(1); does not return the current page.
I was having the same problem and I found the solution for it:
Steps:
1) First Download the CustomViewPager
Class from this link.
2) Use that class as mentioned below:
In Java:
CustomViewPager mViewPager; mViewPager = (CustomViewPager) findViewById(R.id.swipePager); mViewPager.setOffscreenPageLimit(0);
In XML:
<com.yourpackagename.CustomViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipePager" android:layout_width="match_parent" android:layout_height="match_parent"/>
Now only one page will be loaded at once.
P.S: As per the question's requirement, I have posted the solution for Viewpager
. I haven't tried the same with TabLayout
yet. If I will find any solution for that I will update the answer.
In this file, KeyEventCompat is used it may not found by the android studio because KeyEnentCompat class was deprecated in API level 26.0.0 so you need to replace KeyEventCompat to event for more details you can view https://developer.android.com/sdk/support_api_diff/26.0.0-alpha1/changes/android.support.v4.view.KeyEventCompat
As far as I know, that is not possible when using the ViewPager. At least not, when you want your pages to be swipe-able.
The explaination therefore is very simple:
When you swipe between two pages, there is a Point when both pages need to be visible, since you cannot swipe between two things when one of those does not even exist at that point.
See this question for more: ViewPager.setOffscreenPageLimit(0) doesn't work as expected
CommonsWare provided a good explaination in the comments of his answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With