如何让我的 ViewPager 一次只加载一页,即 setOffscreenPageLimit(0); [英] How can make my ViewPager load only one page at a time ie setOffscreenPageLimit(0);

查看:55
本文介绍了如何让我的 ViewPager 一次只加载一页,即 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) 首先从 这个链接.

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 替换为 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 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);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆