Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager offscreen page limit

Is there a way to bypass the normal behavior of ViewPager and its offscreen page limit? My ViewPager contains four fragments, each containing a gridview of images. The problem I have is that on instansiation of the ViewPager, two fragments are created, which results in that about 20 images (about 10 per fragment) is downloaded/fetched from catch simultaneously. Is it possible to disable the offscreen page limit?

My goal is to only download images when a fragment is selected, or only when the user is hovering the image. One way to achieve this is to use the onPageSelected listener and set a flag, which tells the GridViewAdapter if it's allowed to download the image or not.

A second way that I can think of is to set a HoverListener on the ImageView, and only download the image on onHover, but that listener is only available in 4.0 and later.

Is there a better way to achieve this?

like image 562
user634545 Avatar asked Jul 25 '12 12:07

user634545


People also ask

What is ViewPager offscreen page limit?

No. It is already set to the minimum possible value: one page to each side of the viewed page. This is necessary to have the animation effects work -- you see parts of two fragments (original and new) at the same time.

Is ViewPager deprecated?

This function is deprecated. Set a drawable that will be used to fill the margin between pages. Set a drawable that will be used to fill the margin between pages. reverseDrawingOrder: Boolean, transformer: ViewPager.

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.

What is PagerAdapter?

PagerAdapter supports data set changes. Data set changes must occur on the main thread and must end with a call to notifyDataSetChanged similar to AdapterView adapters derived from android. widget. BaseAdapter . A data set change may involve pages being added, removed, or changing position.


2 Answers

Is it possible to disable the offscreen page limit?

No. It is already set to the minimum possible value: one page to each side of the viewed page. This is necessary to have the animation effects work -- you see parts of two fragments (original and new) at the same time.

My goal is to only download images when a fragment is selected, or only when the user is hovering the image.

Then load your grid with placeholder images, and do not load the real images until the page is changed.

Also, note that "hover" implies some sort of mouse or similar sort of pointer, which is not used on most Android devices.

like image 200
CommonsWare Avatar answered Sep 19 '22 02:09

CommonsWare


Simply set the offscreen limit to one.

ViewPager mViewpager = (ViewPager)findView.... mViewPager.setOffscreenPageLimit(1); 
like image 39
DroidBender Avatar answered Sep 18 '22 02:09

DroidBender