Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can make my ViewPager load only one page at a time ie setOffscreenPageLimit(0);

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.

like image 468
code511788465541441 Avatar asked Sep 30 '13 14:09

code511788465541441


People also ask

How do you make a ViewPager only resume selected fragment?

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.

How do I load a fragment in ViewPager?

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.

How do I get ViewPager current position?

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.


2 Answers

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

like image 165
Ronak Thakkar Avatar answered Sep 21 '22 07:09

Ronak Thakkar


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.

like image 44
Philipp Jahoda Avatar answered Sep 22 '22 07:09

Philipp Jahoda