我在 View Pager 中使用 FragmentStatePagerAdapter
.如果 isViewFromObject (View view, Object object)
返回 false
,则返回的 Fragment 不会显示在屏幕上.这是为什么?
开发者文档说 Determines 页面视图是否与由 instantiateItem(ViewGroup, int) 返回的特定键对象相关联.PagerAdapter 需要此方法才能正常运行.
但我不清楚这个定义.
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
将成为视图本身.我们可以检查 return view == object;
from isViewFromObject (View view, Object 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 帖子的更多见解:
Some more insights from post https://stackoverflow.com/a/16772250/1994950 :
当您滑动时,ViewPager
从数组中获取视图位置或实例化它,并使用适配器方法 public boolean isViewFromObject(视图视图,对象对象)
.在ViewPager
上向用户显示等于object 的视图.如果没有视图,则显示空白屏幕.
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(视图视图,对象对象)"在 FragmentStatePagerAdapter 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!