无限的 ViewPager [英] Infinite ViewPager

查看:15
本文介绍了无限的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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