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.
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;
}
Some more insights from post https://stackoverflow.com/a/16772250/1994950 :
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.
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;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…