“"的作用是什么? isViewFromObject(View view,Object object)".在FragmentStatePagerAdapter中? [英] What is the role of " isViewFromObject (View view, Object object)" in FragmentStatePagerAdapter?
问题描述
我在View Pager中使用了FragmentStatePagerAdapter
.如果isViewFromObject (View view, Object object)
返回false
,则返回的片段将不显示在屏幕上.这是为什么?
开发人员文档说Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int). This method is required for a PagerAdapter to function properly.
,但是我不清楚这个定义.
I am using a FragmentStatePagerAdapter
with my View Pager. The Fragment returned is not displayed on the screen if isViewFromObject (View view, Object object)
returns false
. Why is that?
The developer doc says Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int). This method is required for a PagerAdapter to function properly.
But I am not clear with this definition.
推荐答案
方法instantiateItem(ViewGroup, int)
返回特定视图的Object
. PagerAdapter
实现将viewpager更改页面时将此Object
视为key
值.
The method instantiateItem(ViewGroup, int)
returns Object
for a particular view. PagerAdapter
implementation is considering this Object
as a key
value when viewpager changes a page.
因此,如果我们从instantiateItem(ViewGroup, int)
返回视图本身,则该页面的key
将成为视图本身.我们可以从isViewFromObject (View view, Object object)
中检查return view == object;
,它总是返回true
,并且页面将显示:
So, if we return the view itself from instantiateItem(ViewGroup, int)
, then our key
for that page becomes the view itself. We can check return view == object;
from isViewFromObject (View view, Object object)
which will always return true
and our pages will display :
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
来自 https://stackoverflow.com/a/16772250/1994950 的更多见解:
滑动时,ViewPager
从数组获取视图位置或将其实例化,然后使用适配器方法public boolean isViewFromObject(View view, Object object)
将此视图与ViewPager
的子级进行比较.在ViewPager
上向用户显示等于对象的视图.如果没有视图,则显示空白屏幕.
When you slide, the ViewPager
gets view position from an array or instantiates it and compare this view with children of ViewPager
with adapters method public boolean isViewFromObject(View view, Object object)
. The view which equals to object is displayed to the user on ViewPager
. If there is no view then the blank screen is displayed.
这里是ViewPager
方法,其中将视图与对象进行了比较:
Here is ViewPager
method where the view is compared to object:
ItemInfo infoForChild(View child) {
for (int i=0; i<mItems.size(); i++) {
ItemInfo ii = mItems.get(i);
if (mAdapter.isViewFromObject(child, ii.object)) {
return ii;
}
}
return null;
}
这篇关于“"的作用是什么? isViewFromObject(View view,Object object)".在FragmentStatePagerAdapter中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!