如何使我的ViewPager一次仅加载一页,即setOffscreenPageLimit(0); [英] How can make my ViewPager load only one page at a time ie setOffscreenPageLimit(0);
问题描述
我知道我可以给setOffscreenPageLimit(int)
的最小数字是1,但是由于内存问题,我需要一次加载一页.
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.
我是否必须使用旧样式的tabhost等?还是有办法让我的viewPager一次加载一页?
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?
我的适配器使用ViewHolder模式扩展BaseAdapter.
My Adapter extends BaseAdapter with the ViewHolder patern.
推荐答案
我遇到了同样的问题,并且找到了解决方案:
步骤:
1)首先从下载CustomViewPager
类此链接.
1) First Download the CustomViewPager
Class from this link.
2)使用该类,如下所述:
2) Use that class as mentioned below:
在Java中:
CustomViewPager mViewPager;
mViewPager = (CustomViewPager) findViewById(R.id.swipePager);
mViewPager.setOffscreenPageLimit(0);
在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"/>
现在一次只能加载一页.
PS :根据问题的要求,我已经发布了Viewpager
的解决方案.我还没有尝试使用TabLayout
进行同样的操作.如果我能找到任何解决方案,我将更新答案.
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.
在此文件中使用了KeyEventCompat,但由于在API级别26.0.0中已弃用KeyEnentCompat类,因此android studio可能找不到它,因此您需要将KeyEventCompat替换为event,以查看更多详细信息 https://developer. android.com/sdk/support_api_diff/26.0.0-alpha1/changes/android.support.v4.view.KeyEventCompat
In this file KeyEventCompat is used it may not found by 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
这篇关于如何使我的ViewPager一次仅加载一页,即setOffscreenPageLimit(0);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!