ViewPager 四大函数详解「进阶篇」

上一篇文章写的 ViewPager 实现页面切换「入门篇」 简单介绍了 ViewPager 的用法,并简单的做了一个界面滑动的 Demo。上一篇文章中留了一个问题就是在 PagerAdapter 适配器中的四个函数分别起到什么作用。今天我到官网详细看了一下,看完之后自己做个整理、总结。

适配器中的四大函数:

官方文档解释:

viewpager 不直接处理每一个视图而是将各个视图与一个键联系起来。这个键用来跟踪且唯一代表一个页面,不仅如此,该键还独立于这个页面所在 adapter 的位置。当 pageradapter 将要改变的时候他会调用 startUpdate 函数,接下来会调用一次或多次的 instantiateItem 或者 destroyItem。最后在更新的后期会调用 finishUpdate。当 finishUpdate 返回时, instantiateItem 返回的对象应该添加到父容器ViewGroup中,destroyItem 返回的对象应该被ViewGroup删除。方法 isViewFromObject(View, Object) 代表了当前的页面是否与给定的键相关联。
对于非常简单的 pageradapter 或许你可以选择用 page 本身作为键,在创建并且添加到 viewgroup 后 instantiateItem 方法里返回该page本身即可 destroyItem 将会将该 page 从 viewgroup 里面移除。isViewFromObject 方法里面直接可以返回 view == object。

1、getCount()函数:

官网文档:

   public abstract int getCount (){

       Return the number of views available.//返回可用视图的数量。

}


那么我们就按照上一篇的文章继续讲解:「说白了就是返回可滑动视图的个数」

@Override
public int getCount() {
    return viewList.size();
}

2、destoryItem()函数:

官网文档:

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

Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure this is done by the time it returns fromfinishUpdate(ViewGroup).

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

官网资料解释:为给定的位置删除一个页面。适配器负责将视图从其容器中移除,尽管它只必须确保这是在它从完成更新(ViewGroup)返回时完成的


那么我们就按照上一篇的文章继续讲解:「说白了就是移除给定位置的视图」

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView(viewList.get(position));
}
container 通过 removeView() 方法中移除视图。


3、instantiateItem(ViewGroup container,int position)函数:

官网文档:

public Object instantiateItem (ViewGroup container, int position)

Create the page for the given position. The adapter is responsible for adding the view to the container given here, although it only must ensure this is done by the time it returns fromfinishUpdate(ViewGroup).

Parameters
container The containing View in which the page will be shown.
position The page position to be instantiated.
Returns
  • Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.

官网资料解释:为给定的位置创建页面。适配器负责将视图添加到此处给出的容器中,尽管它只必须确保这是在它从结束更新(ViewGroup)返回时完成的。


那么我们就按照上一篇的文章继续讲解:「说白了就是根据指定位创建视图然后添加到 container 中」

@Override
public Object instantiateItem(ViewGroup container, int position) {
    container.addView(viewList.get(position));
    return viewList.get(position);
}
返回值为新添加的指定位置的视图。


4、isViewFromObject(View view,Object object)

public abstract boolean isViewFromObject (View view, Object object)

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.

Parameters
view Page View to check for association with object
object Object to check for association with view
Returns
  • true if view is associated with the key object object

官网资料翻译:确定一个页面视图是否与实例 instanTiateItem(ViewGroup、int)返回的特定键对象相关联。这种方法对于一个PagerAdapter来说是必需的。


那么我们就按照上一篇的文章继续讲解:「说白了就是比较一个页面视图是否与新产生的视图相同」

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}
返回值:如果对应的是同一个View,返回True,否则返回False。


猜你喜欢

转载自blog.csdn.net/qq_36903042/article/details/80488160