如何测试如果一个片段视图是对用户可见?视图、片段、测试、用户

由网友(许我一笔墨、绘你倾城颜)分享简介:我有一个ViewPager,每个页面都是一个片段视图。我想测试如果一个片段是在可见光区域。该Fragment.isVisible唯一的测试I have a ViewPager, each page is a Fragment view. I want to test if a fragment is in a vis...

我有一个ViewPager,每个页面都是一个片段视图。我想测试如果一个片段是在可见光区域。该Fragment.isVisible唯一的测试

I have a ViewPager, each page is a Fragment view. I want to test if a fragment is in a visible region. the Fragment.isVisible only test

的片段被连接到一个活性 的片段设置为可见 片段已被添加到一个视图

在ViewPager将创建3(默认)片段三人都符合上述标准,但只有一个实际上是对用户可见的(人的眼睛)

The ViewPager will create 3 (by default) fragment and all three of them meet the above criteria, but only one is actually visible to the user (the human eyes)

推荐答案

你说得有更好的方法来做到这一点!

看一看的FragmentPagerAdapter javadoc的在线,你会看到有一个方法setPrimaryItem(ViewGroup集装箱,INT位置,Object对象):无效做的正是你需要的。

Have a look at the FragmentPagerAdapter javadoc online and you'll see there is a method setPrimaryItem(ViewGroup container, int position, Object object):void doing exactly what you need.

从的javadoc

公共无效setPrimaryItem(ViewGroup中的容器,INT位置,Object对象)

public void setPrimaryItem (ViewGroup container, int position, Object object)

调用,通知其中项目目前认为到适配器   是主,即一个节目给用户作为当前   页。

Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page.

参数容器含查看从该页面会   除去。位置上的页面位置,现在是主要的。   目的是通过instantiateItem(查看返回的同一个对象,   INT)。

Parameters container The containing View from which the page will be removed. position The page position that is now the primary. object The same object that was returned by instantiateItem(View, int).

注意上滚动状态

现在如果实现这一点,并开始调试以达到完全时,这个叫你很快就会发现,这是触发几次preparing片段,并在用户刷卡沿的感觉。

Now if you implement this and start debugging to get a feel of when exactly this is called you'll quickly notice this is triggered several times on preparing the fragment and while the user is swiping along.

因此​​,它可能是一个好主意,还附上一个ViewPager.OnPageChangeListener只有做必须做一次viewpagers滚动状态变为SCOLL_STATE_IDLE试。

So it might be a good idea to also attach a ViewPager.OnPageChangeListener and only do what has to be done once the viewpagers scroll state becomes SCOLL_STATE_IDLE again.

阅读全文

相关推荐

最新文章