[英]Avoid page switching in a ViewPager until the content on the next page is ready
I am using a ViewPager so the user can browse through an infinite collection of images that need to be fetched from the Internet. 我正在使用ViewPager,因此用户可以浏览需要从Internet获取的无限图像集。
I would not like to change to the next/previous page, unless its image has already been loaded. 除非已经加载了图像,否则我不想更改为下一页/上一页。 So if the user swiped right/left, the current image would still be on display with a progressbar spinning on top, and the change to the next/previous page would not be performed until the image was ready.
因此,如果用户向右/向左滑动,当前图像仍将显示,其中进度条旋转在顶部,并且在图像准备好之前不会执行对下一页/上一页的更改。 At that very instant pages would swap and the progressbar would disappear.
在那个瞬间页面将交换,进度条将消失。
At the moment I was trying to implement this using the following layout for the children views of the ViewPager and a FragmentStatePageAdapter. 目前,我正在尝试使用以下布局为ViewPager和FragmentStatePageAdapter的子视图实现此功能。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/label_painting"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" android:visibility="gone" />
</FrameLayout>
In the Fragment I follow the steps listed below: 在片段中,我按照下面列出的步骤操作:
The problem with this approach I am following is that in case the image has not been fetched yet when the user swipes, the previous image disappears and he sees a spinning progressbar until the new image is available. 我所遵循的这种方法的问题在于,如果在用户滑动时尚未提取图像,则先前的图像消失并且他看到旋转的进度条直到新图像可用。 It would be much nicer to keep seeing an image.
继续看图像会好得多。
Has anyone been able to implement this with a ViewPager or knows what could I change in my code? 有没有人能够使用ViewPager实现这一点,或者知道我的代码中可以更改什么?
Thanks in advance. 提前致谢。
I did nearly the same. 我做的几乎一样。
You can enable/disable view paging in the adapter via: 您可以通过以下方式在适配器中启用/禁用视图分页:
//disable viewpager
public class CustomViewPager extends ViewPager {
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
}
If you Load your Image use an AsyncTask
如果加载图像,请使用
AsyncTask
private class DownloadImageTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
Use the onPreExecute()
Method to disable viewpager, and in onPostExecute()
set the image and re-Enable the ViewPager
again. 使用
onPreExecute()
方法禁用viewpager,并在onPostExecute()
设置图像并再次重新启用ViewPager
。 This works perfectly for me 这对我来说非常适合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.