如何测试如果一个片段视图是对用户可见? [英] How to test if a fragment view is visible to the user?

查看:181
本文介绍了如何测试如果一个片段视图是对用户可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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)

推荐答案

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

看一看的<一个href="http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html">FragmentPagerAdapter javadoc的在线,你会看到有一个方法<一href="http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html#setPrimaryItem%28android.view.ViewGroup,%20int,%20java.lang.Object%29">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.

因此​​,它可能是一个好主意,还附上一个<一个href="http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html#onPageScrollStateChanged%28int%29">ViewPager.OnPageChangeListener只有做必须做一次viewpagers滚动状态变为<一href="http://developer.android.com/reference/android/support/v4/view/ViewPager.html#SCROLL_STATE_IDLE">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.

这篇关于如何测试如果一个片段视图是对用户可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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