无限的 ViewPager

Infinite ViewPager(无限的 ViewPager)
本文介绍了无限的 ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是可能的,但我在弄清楚如何实现一个可以无限分页的 ViewPager 时遇到了一些麻烦.

This seems possible, but I'm having a little trouble figuring out how to implement a ViewPager that can page indefinitely.

我的用例是一次显示一个月的日历,但可以通过分页足够的次数来切换到任何月份.

My use case is for a calendar that shows a month at a time, but would be able to switch to any month by paging enough times.

目前我只是扩展PagerAdapter,并添加了3个自定义MonthViews,如下所示:

Currently I'm just extending PagerAdapter, and 3 custom MonthViews are added like so:

    @Override
    public Object instantiateItem(View collection, int position) {
        final MonthView mv = new MonthView(HistoryMonthActivity.this);
        mv.setLayoutParams(new ViewSwitcher.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT));
        mv.setSelectedTime(mTime);

        ((DirectionalViewPager) collection).addView(mv, 0);

        return mv;
    }

我想一次只在内存中保留 3 个 MonthView,以便在每个 MonthView 上显示我的数据库中的数据时不会有很大的延迟当用户点击时.因此,每次显示新页面时,我都需要删除一个 MonthView 并添加一个 MonthView.如果有人对相同的功能有更好的想法,我很想听听!

I would like to keep only 3 MonthViews in memory at a time, so that there isn't a huge delay in displaying data from my database on each MonthView when the user is tabbing. So every time a new page is shown, I will need to remove one MonthView and add one MonthView. If anyone has a better idea for the same functionality , I'd love to hear it!

我正在考虑尝试使用 FragmentStatePagerAdapter.

I'm thinking of trying to use a FragmentStatePagerAdapter.

推荐答案

我能够在我的 MonthViewPagerAdapter 中使用一点魔法来实现这个OnPageChangeListener,以及编辑ViewPager本身的源代码.如果有人对我如何实现它感兴趣,查看源代码或针对特定问题发表评论.

I was able to implement this using a little magic in my MonthViews, my PagerAdapter's OnPageChangeListener, as well as with editing the source code of ViewPager itself. If anyone is interested in how I achieved it, check out the source code or comment with a specific question.

这篇关于无限的 ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)