zoukankan      html  css  js  c++  java
  • 【Android 界面效果45】ViewPager源码分析

    ViewPager概述:

    Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.
    Note this class is currently under early design and development. The API will likely change in later updates of the compatibility library, requiring changes to the source code of apps when they are compiled against the newer version.
    ViewPager is most often used in conjunction with android.app.Fragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These are android.support.v4.app.FragmentPagerAdapter and android.support.v4.app.FragmentStatePagerAdapter; each of these classes have simple code showing how to build a full user interface with them.
    Here is a more complicated example of ViewPager, using it in conjuction with android.app.ActionBar tabs. You can find other examples of using ViewPager in the API 4+ Support Demos and API 13+ Support Demos sample code.
     {@sample development/samples/Support13Demos/src/com/example/android/supportv13/app/ActionBarTabsPager.java complete}

    布局管理器允许用户通过左右滑动的方式浏览数据页面。开发者需要提供产生这些页面的数据适配器---实现PagerAdapter。

    值得注意的是,ViewPager这个类目前仍在前期设计和开发阶段,它在今后的兼容包中有可能更新,如果ViewPager在编译时与更新版本冲突就需要修改它的源码。

    ViewPager通常与Fragment结合使用,这样可以方便有效的管理各个页面的生命周期。为了让Fragment和ViewPager结合使用,已经有了概含最常见的用例的标准适配器:FragmentPagerAdapter和FragmentStatePagerAdapter.这两个类都是PagerAdapter的子类,都有一些简单的代码来展示如何用他们来创建一个完整的用户接口。

    ViewPager有一个更复杂的例子,使用它与ActionBar的tab结合使用。可以在v4和v13支持库的示例代码中找到其他和ViewPager有关的例子。(路径为:development/samples/Support13Demos/src/com/example/android/supportv13/app/ActionBarTabsPager.java)

    Support13Demos源码下载:http://download.csdn.net/detail/dongdong230/8440129

    Support13Demos源码下载:http://download.csdn.net/detail/dongdong230/8440129

    ViewPager方法精选:

    1.Viewpager继承ViewGroup,需重写其抽象方法OnLayout

    protected void onLayout (boolean changed, int l, int t, int r, int b)

    调用场景:在view给其孩子设置尺寸和位置时被调用。子view,包括孩子在内,必须重写onLayout(boolean, int, int, int, int)方法,并且调用各自的layout(int, int, int, int)方法

    几个参数的含义:

    changed view有新的尺寸或位置
    l 相对于父view的Left位置
    t 相对于父view的Top位置
    r 相对于父view的Right位置
    b 相对于父view的Bottom位置

    这里分别对decor view 和 非decor view进行位置移动,依据是srollx及offset。

    2.ViewPager的内部类:LayoutParams

    Layout parameters that should be supplied for views added to a ViewPager。

    这是为viewpager页面子view准备的布局参数类。

    其中的几个重要字段:

    isDecor true:该view是pager自身的而非适配器适配的
    gravity 仅为decor view使用,布局子view的位置,常量值在android.view.Gravity中
    widthFactor 页面宽度的乘数因子?
    needsMeasure true:该view在正layout时被添加,且在布局这个view前需测量
    position 非decorview的view所适配的位置

    3.ViewPager重要接口OnPageChangeListener

    三个未实现的方法:onPageScrolled、onPageSelected、onPageScrollStateChanged

    最常用接口,无需多说。

    另有内部类SimpleOnPageChangeListener:实现了OnPageChangeListener方法,但没有写实现代码,如果不想重写OnPageChangeListener所有方法,使用这个内部类即可。

    4.滑动动画接口:PageTransformer:页面滑动时所展现的动画效果

    transformPage:给指定页面添加滑动动画

    5.其他方法

    1. addFocusables:当前显示的页面方可获取焦点
    2. addTouchables:当前显示的页面方可获取触摸事件
    3. addView:根据指定布局参数添加子view
    4. arrowScroll:滑动的方向?
    5. beginFakeDrag:开始控制滑动事件,处于fake drag时,viewpager会忽略掉touch事件
    6. canScrollHorizontally:判断view是否可在指定方向(derection)上水平滑动.derection:负数代表向左,正数代表向右
    7. computeScroll:由父类调用,计算其子类所应滑动的距离
    8. dispatchKeyEvent:分发key事件
    9. dispatchPopulateAccessibilityEvent:分发辅助事件给view,然后将其子类的text content添加到这个事件中。
    10. draw:绘制view及其子view到canvas上。
    11. endFakeDrag:结束控制滑动事件
    12. executeKeyEvent:执行key事件
  • 相关阅读:
    python 小练习 5
    python 小练习4
    python 小练习3
    python 小练习2
    遇到后缀名为whl的库的安装方法
    hdu1394Minimum Inversion Number
    Triangle
    codeforces B. Pasha and String
    F
    C
  • 原文地址:https://www.cnblogs.com/dongdong230/p/4288474.html
Copyright © 2011-2022 走看看