How can make my ViewPager load only one page at a time ie setOffscreenPageLimit(0);
android, android-viewpager
Solution
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 be found by the Android studio because KeyEnentCompat class was deprecated in API level 26.0.0 so you need to replace KeyEventCompat with 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
Problem
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.