我知道我可以给 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 等吗?或者有没有办法/hack我可以让我的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 类shared&resourcekey=0-tKADcOLf61mco5p5C-Ca5w" rel="nofollow noreferrer">这个链接.
1) First Download the CustomViewPager
Class from this link.
2) 如下所述使用该类:
在 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"/>
现在一次只会加载一个页面.
P.S:根据问题的要求,我已经发布了 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,它可能不会被 android studio 找到,因为 KeyEnentCompat 类在 API 级别 26.0.0 中已弃用,因此您需要将 KeyEventCompat 替换为事件以获取更多详细信息,您可以查看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 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
这篇关于如何让我的 ViewPager 一次只加载一个页面,即 setOffscreenPageLimit(0);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!