简单说明:

  ViewPager是android扩展包v4包中的类,直接继承了ViewGroup类,和LinearLayout等布局一样,都是一个容器,需要在里面添加我们想要显示的内容。

一、在xml中添加ViewPager  

<RelativeLayout xmlns:andro>
    android:id="@id/bannerContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false">

    <android.support.v4.view.ViewPager
        android:id="@id/bannerViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false" />

注:除了通过xml代码来定义一个ViewPager外,同其他控件一样也可以动态添加。

  

二、定义ViewPageAdapter

  1、可以在构造函数中传递ViewPager需要加载显示的数据。

  2、必须重载的函数:

    a)  Determines whether a page View is associated with a specific key object return by  instantiateItem(ViewGroup ,int)

 public abstract boolean isViewFromObject(View view, Object object);

     b)   获取Pager的总页数

 /**
     * Return the number of views available.
     */
    public abstract int getCount();

   c)干函数中需要将  页面需要加载的内容添加到container中,同时将需要加载的view返回。该函数是PagerAdapter的核心,具体可以参照下面代码

/**
     * 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 from
     * {@link #finishUpdate(ViewGroup)}.
     *
     * @param container The containing View in which the page will be shown.
     * @param position The page position to be instantiated.
     * @return Returns an Object representing the new page.  This does not
     * need to be a View, but can be some other container of the page.
     */
    public Object instantiateItem(ViewGroup container, int position) {
        return instantiateItem((View) container, position);
    }

  d)该函数负责对ViewGroup中已经添加的部分资源进行回收

    (遇到过当没有重载该函数时,滑动到图片页数第三张时,activity自动结束返回到上一个Activity。具体原因有待分析)

 /**
     * 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 from {@link #finishUpdate(ViewGroup)}.
     *
     * @param container The containing View from which the page will be removed.
     * @param position The page position to be removed.
     * @param object The same object that was returned by
     * {@link #instantiateItem(View, int)}.
     */
    public void destroyItem(ViewGroup container, int position, Object object) {
        destroyItem((View) container, position, object);
    }

   e) destoryItem() 和  instantiateItem()  执行顺序问题:下图为 移动一个viewpage时 这两个函数打印的 position

                            ViewPage最全解析 

  ViewPager启动时,第一个和第二个页面已经加载完成。

  当再次滑动时,先加载第三个页面,然后再销毁第一个页面。(如果这时返回第1个页面,则先加载第一个页面再销毁第三个页面)

  总之,除了第一个后最后一个页面外,ViewPager总会保持左右两侧的页面已经加载完成。

3、 PagerAdapter 还有 FragementPagerAdapter 用于加载Fragment。 (用普通的PagerAdapter加载Fragment的区别)

  FragementStatePagerAdapter : 当页数较多时还可以使用该Adapter。 

 

PagerAdapter的实现代码: 

/**
     *  PagerAdapter的实现
     * */
    public class DripPageGuideAdapter extends PagerAdapter {

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViews.get(position));
            return mViews.get(position);
        }

        @Override
        public int getCount() {
            return totalSize;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }
    }
View Code

相关文章:

  • 2021-08-13
  • 2021-07-20
  • 2021-05-21
  • 1970-01-01
  • 2021-10-07
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-24
  • 2021-11-26
  • 2021-11-04
  • 2021-12-12
  • 2021-04-07
  • 2021-09-11
  • 2021-11-05
相关资源
相似解决方案