如何创建轮播ViewPager? [英] How to create carousel ViewPager?

查看:79
本文介绍了如何创建轮播ViewPager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的就是在Android中使用水平轮播.

All I want to do is a horizontal carousel in Android.

如果我有3个屏幕A B和C,那么我希望ViewPager允许我像 A<-> B B<-> C C<-> A.

If I have 3 screens A B and C then I want my ViewPager to allow me to move like A <-> B, B <-> C, C <-> A.

GTalk for Android的对话可以这样切换. 三星的主屏幕和应用程序屏幕可以像这样切换.

GTalk for Android's conversation can be switched like this. Samsung's homescreen and application screen can be switched like this.

A B和C是片段,我正在使用扩展FragmentPagerAdapter的适配器.所有片段都将包含一个webview.

A B and C are fragments and I'm using an adapter that extends FragmentPagerAdapter. All the fragments will contain a webview.

我已经在此处查看了

I have looked here here and here but none of them seem to be doing what I want.

有人可以引导我朝正确的方向发展吗?

Can anyone guide me in the right direction?

推荐答案

(交叉发布我的答案来自相同的答案StackOverflow问题)

(Cross-posting my answer from an identical StackOverflow question)

一种可能性是像这样设置屏幕:

One possibility is setting up the screens like this:

C'A B C A'

C' A B C A'

C'看起来就像C,但是当您滚动到那里时,它将切换到真实的C. A'看起来就像A,但是当您滚动到该位置时,它将切换到真实的A.

C' looks just like C, but when you scroll to there, it switches you to the real C. A' looks just like A, but when you scroll to there, it switches you to the real A.

我可以通过实现

I would do this by implementing onPageScrollStateChanged like so:

@Override
public void onPageScrollStateChanged (int state) {
    if (state == ViewPager.SCROLL_STATE_IDLE) {
        int curr = viewPager.getCurrentItem();
        int lastReal = viewPager.getAdapter().getCount() - 2;
        if (curr == 0) {
            viewPager.setCurrentItem(lastReal, false);
        } else if (curr > lastReal) {
            viewPager.setCurrentItem(1, false);
        }
    }
}

请注意,这将调用 setCurrentItem ,并传递false导致跳转立即发生,而不是平滑滚动.

Note that this calls the alternate form of setCurrentItem and passes false to cause the jump to happen instantly rather than as a smooth scroll.

我对此有两个主要缺点.首先,在到达任一端时,用户必须让滚动稳定下来,然后才能进一步移动.其次,这意味着在您的第一页和最后一页中拥有所有视图的第二份副本.根据屏幕的资源占用情况,这可能会排除这种技术.

There are two main drawbacks I see to this. Firstly, upon reaching either end the user has to let the scrolling settle before they can go further. Secondly, it means having a second copy of all of the views in your first and last page. Depending on how resource-heavy your screens are, that may rule out this technique as a possible solution.

还请注意,由于视图分页器直到滚动结束后才允许点击进入底层控件,因此最好不为A'和C'片段设置clicklistener等.

Note also that since the view pager doesn't let clicks go through to underlying controls until after the scrolling has settled, it's probably fine to not set up clicklisteners and the like for the A' and C' fragments.

现在,我自己执行了此操作,还有另一个非常大的缺点.从A'切换到A或从C'切换到C时,屏幕闪烁片刻,至少在我当前的测试设备上.

Having now implemented this myself, there's another pretty major drawback. When it switches from A' to A or C' to C, the screen flickers for a moment, at least on my current test device.

这篇关于如何创建轮播ViewPager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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