如何让我的 ViewPager 一次只加载一个页面,即 setOffscreenPageLimit(0);

2023-10-04移动开发问题
4

本文介绍了如何让我的 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 等吗?或者有没有办法/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);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?
How can i use MYSQL database connection in iphone application useing cocos2d?(如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?)...
2024-08-12 移动开发问题
5

在 cocos2d 中平滑拖动一个 Sprite - iPhone
Smoothly drag a Sprite in cocos2d - iPhone(在 cocos2d 中平滑拖动一个 Sprite - iPhone)...
2024-08-12 移动开发问题
10

Cocos2D 复杂动画
Cocos2D complex animations(Cocos2D 复杂动画)...
2024-08-12 移动开发问题
6