Android ViewPager 离屏页面限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11650152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ViewPager offscreen page limit
提问by user634545
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?
有没有办法绕过 的正常行为ViewPager
及其屏幕外页面限制?MyViewPager
包含四个fragments
,每个包含一个gridview
图像。我遇到的问题是,在 实例化时ViewPager
,fragments
创建了两个,这导致同时从 catch 下载/获取大约 20 个图像(每个片段大约 10 个)。是否可以禁用屏幕外页面限制?
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.
我的目标是仅在fragment
选择a 时下载图像,或者仅在用户悬停图像时下载图像。实现此目的的一种方法是使用onPageSelected
侦听器并设置一个标志,该标志告诉GridViewAdapter
是否允许下载图像。
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.
我能想到的第二个方法是设置HoverListener
上ImageView
,只有下载的图像onHover
,但听者只在4.0及更高版本。
Is there a better way to achieve this?
有没有更好的方法来实现这一目标?
回答by CommonsWare
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.
另外,请注意“悬停”意味着某种鼠标或类似的指针,大多数 Android 设备上都没有使用。
回答by DroidBender
Simply set the offscreen limit to one.
只需将屏幕外限制设置为 1。
ViewPager mViewpager = (ViewPager)findView....
mViewPager.setOffscreenPageLimit(1);
回答by Hasina
My goal is to only download images when a fragment is selected
我的目标是仅在选择片段时下载图像
You can use setUserVisibleHint(boolean isVisibleToUser)
callback of the Fragment in your ViewPager.
您可以setUserVisibleHint(boolean isVisibleToUser)
在 ViewPager 中使用Fragment 的回调。
回答by Dawid Hy?y
You can also do this in xml using Android Data Binding:
您还可以使用 Android 数据绑定在 xml 中执行此操作:
<android.support.v4.view.ViewPager
[...]
app:offscreenPageLimit="@{1}"
[...]
</android.support.v4.view.ViewPager>
回答by FindOut_Quran
Handle viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
处理 viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
Inside it, remove extra views after position + 1
and before position - 1
using viewPager.removeViewAt(int)
(i.e. limit the pager to have only 3 pages loaded: current, previous and next.
在它里面,position + 1
在position - 1
使用前后删除额外的视图viewPager.removeViewAt(int)
(即限制传呼机只加载 3 个页面:当前、上一个和下一个。
Then you should handle destroyItem
in the viewpager adapter, recycle()
ing the bitmaps in the destroyed view.
然后你应该destroyItem
在 viewpager 适配器中recycle()
处理,在被破坏的视图中处理位图。
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
LinearLayout layout = (LinearLayout) object;
((ViewPager) container).removeView(layout);
ImageView imgDisplay = (ImageView) layout.findViewById(R.id.quranPage);
Drawable drawable = imgDisplay.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (bitmap != null) //when reading page fails, this will be null
bitmap.recycle();
}
}
回答by McLeroy Ibe
To avoid downloading the images without the user actually scrolling to the fragment you can override "setUserVisibilityHint (boolean visibleToUser)" and load your images only if "visibleToUser" becomes true
为了避免在用户没有实际滚动到片段的情况下下载图像,您可以覆盖“setUserVisibilityHint (boolean visibleToUser)”并仅在“visibleToUser”变为真时加载您的图像
回答by vikas singh
mViewPager.setOffscreenPageLimit(1);
wont give what you want,it will load neighbor fragment if its not load already(it means it will load 2 fragment).
mViewPager.setOffscreenPageLimit(1);
不会给你想要的东西,如果它没有加载,它会加载邻居片段(这意味着它将加载 2 个片段)。
Lets say if you want to load all your fragment(4 fragment) at once, then only use setOffscreenPageLimit(3)
, else avoid using setOffscreenPageLimit
.
假设您想一次加载所有片段(4 个片段),则只使用setOffscreenPageLimit(3)
,否则避免使用setOffscreenPageLimit
.
I think you cannot change the default behavior of viewpager loading neighbor fragment.
我认为您无法更改 viewpager 加载邻居片段的默认行为。