zoukankan      html  css  js  c++  java
  • ViewPager的简单使用说明

    前提:工程中使用ViewPager,需要导入google提供的jar包(android-support-v4.jar)

    要学习ViewPager的使用,建议直接看官方文档 Creating Swipe Views with Tabs

    接下来主要对使用进行下总结,例子是官网上的。

    ViewPager可以理解成一个布局(layout)部件,如在xml中加载

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    每一个子View作为一个独立的页面,加载在ViewPager上。不过要使子View能作为独立页面插入到ViewPager上,需要实现一个PagerAdapter。

    在PagerAdapter类或其子类中设计tab的标题(getPageTitle方法中实现)、tab的数量(在getCount方法中实现)、tab页面的显示内容(即fragment对象,在getItem方法中实现,该方法返回一个fragment对象)。

    jar包中已经实现了两种:

    1.FragmentPagerAdapter

    用于tab较少、较固定的滑动。效果如图,3个tab:

    下面是FragmentPagerAdapter的子类的实现代码

     1     public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
     2 
     3         public AppSectionsPagerAdapter(FragmentManager fm) {
     4             super(fm);
     5         }
     6 
     7         @Override
     8         public Fragment getItem(int i) {
     9             switch (i) {
    10                 case 0:
    11                     // The first section of the app is the most interesting -- it offers
    12                     // a launchpad into the other demonstrations in this example application.
    13                     return new LaunchpadSectionFragment();
    14 
    15                 default:
    16                     // The other sections of the app are dummy placeholders.
    17                     Fragment fragment = new DummySectionFragment();
    18                     Bundle args = new Bundle();
    19                     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
    20                     fragment.setArguments(args);
    21                     return fragment;
    22             }
    23         }
    24 
    25         @Override
    26         public int getCount() {
    27             return 3;
    28         }
    29 
    30         @Override
    31         public CharSequence getPageTitle(int position) {
    32             return "Section " + (position + 1);
    33         }
    34     }
    FragmentPagerAdapter的子类实现

    在代码中,设置了tab的标题、tab的数量及每个tab显示的内容。具体fragment的实现就不贴出了,参考官网。

    2.FragmentStatePagerAdapter

    用于tab数量不确定,并且当用户切换到其他界面时,销毁之前的界面,达到减少内存使用。效果如图,tab是动态可移动的:

    下面是FragmentStatePagerAdapter的子类的实现代码

     1     public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
     2 
     3         public DemoCollectionPagerAdapter(FragmentManager fm) {
     4             super(fm);
     5         }
     6 
     7         @Override
     8         public Fragment getItem(int i) {
     9             Fragment fragment = new DemoObjectFragment();
    10             Bundle args = new Bundle();
    11             args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is just an integer :-P
    12             fragment.setArguments(args);
    13             return fragment;
    14         }
    15 
    16         @Override
    17         public int getCount() {
    18             // For this contrived example, we have a 100-object collection.
    19             return 100;
    20         }
    21 
    22         @Override
    23         public CharSequence getPageTitle(int position) {
    24             return "OBJECT " + (position + 1);
    25         }
    26     }
    FragmentStatePagerAdapter的子类实现

    在代码中,同样设置了tab的标题、tab的数量及每个tab显示的内容。

    总结步骤

    1.设计布局文件,加载ViewPager,必须包含包名:

    android.support.v4.view.ViewPager

    2.为ViewPager对象设计PagerAdapter对象。该对象需要实现tab的标题内容、tab的数量及每个页面显示的fragment。

    3.实现所需的fragment。所实现的fragment类一般是在PagerAdapter类或其子类的getItem方法中使用。在getItem方法中会创建fragment类的对象,返回给ViewPager来显示。getItem方法有一个参数(int position),开发时,可根据该值来确定哪个位置对应创建哪个fragment对象。

    这样就简单地实现了ViewPager的使用了。

  • 相关阅读:
    [转发]深入理解git,从研究git目录开始
    iOS系统网络抓包方法
    charles抓包工具
    iOS多线程中performSelector: 和dispatch_time的不同
    IOS Core Animation Advanced Techniques的学习笔记(五)
    IOS Core Animation Advanced Techniques的学习笔记(四)
    IOS Core Animation Advanced Techniques的学习笔记(三)
    IOS Core Animation Advanced Techniques的学习笔记(二)
    IOS Core Animation Advanced Techniques的学习笔记(一)
    VirtualBox复制CentOS后提示Device eth0 does not seem to be present的解决方法
  • 原文地址:https://www.cnblogs.com/chenbin7/p/3473987.html
Copyright © 2011-2022 走看看