ViewPager getScrollX返回0时恢复片段/再现 [英] ViewPager getScrollX return 0 when fragment resumes/recreates

查看:449
本文介绍了ViewPager getScrollX返回0时恢复片段/再现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Velocity查看传呼机在那里我可以通过像画廊视图页面滚动。我在做滚动计算遇到的问题。我使用的FragmentStatePagerAdapter ViewPager。对于一个正常的第一次运行的东西都很酷与getScrollBy返回实际滚动位置以1元为0,即如果我在第3页,getScrollX返回2400(800像素每页)等。

I am trying to create a Velocity View Pager where in I can scroll through pages like a gallery view. I am experiencing problem while doing scrolling calculations. I am using FragmentStatePagerAdapter for ViewPager. For a normal first time run things are cool with getScrollBy returning the actual scroll position taking 1st element at 0. i.e. if I am at 3rd page, getScrollX returns 2400(800px per page) and so on.

当我从backstack简历后举办viewpager片段出现问题。这一次,我的第3页变为0,我的第一页变成-2400位置。有些时候是如何的android reinflates适配器它认为为0当前位置我一直在使用setCurrentItem无处不在我可以尝试各种各样的。一些我的感受computeScroll不起作用。

Problem arises when I get the fragment that hosts viewpager after a resume from backstack. This time my 3rd page becomes 0 and my first page becomes -2400 position. Some how when android reinflates the adapter it considers current position as 0. I have tried all sorts using setCurrentItem everywhere I could. Some how I feel computeScroll does not work.

这将是巨大的,如果任何一个可以建议我怎么能做出getScrollX返回的位置开始考虑为0,而不是新膨胀的当前位置。

It would be great if any one can suggest how can I make the getScrollX return the position considering start as 0 and not the newly inflated current position.

否则我就没有选项来创建一个新的片段,每次我重新开始。

Else I would have no option to create a new fragment everytime I resume.

推荐答案

您知道在任何时候的当前位置,对吧?我想你ViewPager的宽度乘以当前的位置,使用 ViewPager.getWidth()。自从2400像素,可以使用的宽度,以像素为单位从的getWidth()来确定位置。

You know the current position at all times, right? I'd multiply the current position by the width of your ViewPager, using ViewPager.getWidth(). Since the 2400 is in pixels, you can use the width, in pixels from getWidth() to determine the position.

需要注意的一点是要确保你使用的getWidth()价值的观点进行膨胀,测量后,否则会返回的宽度为零。

One thing to note is to make sure you use the getWidth() value after the view is inflated and measured, otherwise it will return zero for width.

这篇关于ViewPager getScrollX返回0时恢复片段/再现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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