public abstract class

FragmentPagerAdapter

extends PagerAdapter

java.lang.Object
   ↳ android.support.v4.view.PagerAdapter
     ↳ android.support.v4.app.FragmentPagerAdapter

  

Class Overview


它是PagerAdapter的一种实现,每一个页面都是一个Fragment,并且每一个页面都会保存到fragment manager中,当用户没有可能回到该页面时fragment manager才会将这个fragment销毁。

这种页面十分适用于有一些静态的fragment,例如一组tabs,用户访问的每一个页面都会保存在内存中,尽管当view不可见时可能会被销毁。这就会导致应用程序会占用太多的资源,所以,通常当页面数据量过大时使用FragmentStatePagerAdapter来代替FragmentPagerAdapter

当使用FragmentPageAdapter时ViewPager必须有一个ID。

子类只需要实现适配器的getItem(int)和getCount()方法

下面是官方给出的一个例子:

publicclassFragmentPagerSupportextendsFragmentActivity{
    staticfinalint NUM_ITEMS =10;     MyAdapter mAdapter;     ViewPager mPager;     @Override
    protectedvoid onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pager);         mAdapter =newMyAdapter(getSupportFragmentManager());         mPager =(ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);         // Watch for button clicks.
        Button button =(Button)findViewById(R.id.goto_first);
        button.setOnClickListener(newOnClickListener(){
            publicvoid onClick(View v){
                mPager.setCurrentItem(0);
            }
        });
        button =(Button)findViewById(R.id.goto_last);
        button.setOnClickListener(newOnClickListener(){
            publicvoid onClick(View v){
                mPager.setCurrentItem(NUM_ITEMS-1);
            }
        });
    }     publicstaticclassMyAdapterextendsFragmentPagerAdapter{
        publicMyAdapter(FragmentManager fm){
            super(fm);
        }         @Override
        publicint getCount(){
            return NUM_ITEMS;
        }         @Override
        publicFragment getItem(int position){
            returnArrayListFragment.newInstance(position);
        }
    }     publicstaticclassArrayListFragmentextendsListFragment{
        int mNum;         /**
         * Create a new instance of CountingFragment, providing "num"
         * as an argument.
         */
        staticArrayListFragment newInstance(int num){
            ArrayListFragment f =newArrayListFragment();             // Supply num input as an argument.
            Bundle args =newBundle();
            args.putInt("num", num);
            f.setArguments(args);             return f;
        }         /**
         * When creating, retrieve this instance's number from its arguments.
         */
        @Override
        publicvoid onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            mNum = getArguments()!=null? getArguments().getInt("num"):1;
        }         /**
         * The Fragment's UI is just a simple text view showing its
         * instance number.
         */
        @Override
        publicView onCreateView(LayoutInflater inflater,ViewGroup container,
                Bundle savedInstanceState){
            View v = inflater.inflate(R.layout.fragment_pager_list, container,false);
            View tv = v.findViewById(R.id.text);
            ((TextView)tv).setText("Fragment #"+ mNum);
            return v;
        }         @Override
        publicvoid onActivityCreated(Bundle savedInstanceState){
            super.onActivityCreated(savedInstanceState);
            setListAdapter(newArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1,Cheeses.sCheeseStrings));
        }         @Override
        publicvoid onListItemClick(ListView l,View v,int position,long id){
            Log.i("FragmentList","Item clicked: "+ id);
        }
    }
}

The R.layout.fragment_pager resource of the top-level fragment is:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"android:padding="4dip"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"android:layout_height="match_parent">     <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1">
    </android.support.v4.view.ViewPager>     <LinearLayoutandroid:orientation="horizontal"
            android:gravity="center"android:measureWithLargestChild="true"
            android:layout_width="match_parent"android:layout_height="wrap_content"
            android:layout_weight="0">
        <Buttonandroid:id="@+id/goto_first"
            android:layout_width="wrap_content"android:layout_height="wrap_content"
            android:text="@string/first">
        </Button>
        <Buttonandroid:id="@+id/goto_last"
            android:layout_width="wrap_content"android:layout_height="wrap_content"
            android:text="@string/last">
        </Button>
    </LinearLayout>
</LinearLayout>

The R.layout.fragment_pager_list resource containing each individual fragment's layout is:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:drawable/gallery_thumb">     <TextViewandroid:id="@+id/text"
        android:layout_width="match_parent"android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/hello_world"/>     <!-- The frame layout is here since we will be showing either
    the empty view or the list view.  -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1">
        <!-- Here is the list. Since we are using a ListActivity, we
             have to call it "@android:id/list" so ListActivity will
             find it -->
        <ListViewandroid:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"/>         <!-- Here is the view to show if the list is emtpy -->
        <TextViewandroid:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="No items."/>     </FrameLayout> </LinearLayout>

Summary


[Expand]

Inherited Constants
From class android.support.v4.view.PagerAdapter

 
Public Constructors
  FragmentPagerAdapter(FragmentManager fm) 构造方法,需要传入一个FragmentManager
Public Methods
void destroyItem(ViewGroup container, int position, Object object)

根据给定的position移除一个page页
void finishUpdate(ViewGroup container)

当页面数据加载完成时调用该方法
abstract Fragment getItem(int position)

返回指定位置的相关fragment
long getItemId(int position)

返回给定位置的item的标示符
Object instantiateItem(ViewGroup container, int position)

在给定的位置处创建一个page
boolean isViewFromObject(View view, Object object)

Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int).
void restoreState(Parcelable state, ClassLoader loader)

恢复所有的通过saveState()方法保存的与adapter关联的页面实例状态
Parcelable saveState()

保存所有与adapter相关的页面实例。直到调用restoreState方法是恢复
void setPrimaryItem(ViewGroup container, int position, Object object)

Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page.
void startUpdate(ViewGroup container)

当页面将要被显示时调用

Android FragmentPagerAdapter翻译的更多相关文章

  1. Monkey Android API 翻译

      此篇笔记,记录了API中,对monkey用法的说明,基于Android Studio 2.2.3. Monkey是一个运行在Android模拟器或者Android设备上的程序,通过使用monkey ...

  2. android FragmentPagerAdapter getItem方法没有执行

    转自 http://blog.csdn.net/getchance/article/details/40263505 在一个 Android 应用中,我使用 FragmentPagerAdapter  ...

  3. 【Android Api 翻译3】android api 完整翻译之Application Fundamentals (学习android必须知道的)

    Android应用程序是用Java编程语言编写的.Android SDK工具把应用程序的代码.数据和资源文件一起编译到一个Android程序包中(这个程序包是以.apk为后缀的归档文件),一个Andr ...

  4. 【Android Api 翻译2】Android Testing(1) 浅尝Android测试的奥秘

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 仅供学习和交流使用,翻译不好勿喷,请只摘除不合适的地方 Testing The Android fram ...

  5. Android PageAdapter翻译

    介绍:ViewPager和PagerAdapter结合使用 public abstract class PagerAdapter extends Object java.lang.Object     ...

  6. Embedded Android 协同翻译

    假设你有一定的Android的基础和英语基础. 有愿意贡献开源社区的心. 假设你对下面文件夹感兴趣, 欢迎增加我们协同翻译<Embedded Android> 此次协同翻译.将使用gith ...

  7. [转][Android]FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别

    原文链接:http://blog.csdn.net/zhaokaiqiang1992 FragmentPagerAdapter是android-support-v4支持包里面出现的一个新的适配器,继承 ...

  8. 【Android Api 翻译4】android api 完整翻译之Contacts Provider (学习安卓必知的api,中英文对照)

    Contacts Provider 电话簿(注:联系人,联络人.通信录)提供者 ------------------------------- QUICKVIEW 快速概览 * Android's r ...

  9. 【Android Api 翻译1】Android Texting(2)Testing Fundamentals 测试基础篇

    Testing Fundamentals The Android testing framework, an integral part of the development environment, ...

随机推荐

  1. 微信小程序request同步请求

    今天在搞微信小程序的时候顺手用了async,await死活不起作用,后来查了一下子,竟然不支持,那没办法就换了一种实现wx.request同步请求的方案 祭出promise来搞一搞,下面直接贴代码,简 ...

  2. Salesforce自定义权限简介

    自定义权限(Custom Permission) Salesforce默认提供了多种方式设定用户的权限,比如简档.权限集等.在这些设定中,已经包括了系统中的对象.应用.字段.页面布局等组件,管理员或开 ...

  3. 【转】巧用DOS tree命令+批处理 实现 指定文件 批量复制!

    转自:http://www.cnblogs.com/looky/archive/2010/01/24/1655292.html 今天一朋友叫我帮忙解决指定文件批量复制的问题,于是找了一大堆批处理命令, ...

  4. Android NDK编译之undefined reference to 'JNI_CreateJavaVM'

    利用Android NDK编译动态库,在C文件中调用了两个JNI函数:JNI_GetDefaultJavaVMInitArgs和JNI_CreateJavaVM.编译的时候始终报以下错误: XXX: ...

  5. il8n国际化

    il8n国际化 支持多国语言的web应用,根据客户端系统的语言类型返回对应的界面 方案 为每种语言提供一套相应的资源文件,并以规范化命名的方式保存在特定的目录中,由系统自动根据客户端语言选择适合的资源 ...

  6. (后端)java回调机制

    转自强哥: 所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数.例如Win32下的窗口过程函数就是一个典型的回调函数. ...

  7. 转 fiddler常见的应用场景

    fiddler常见的应用场景   在移动互联网时代,作为软件测试工程师,fiddler绝对是值得掌握并添加进技术栈里的工具之一. 那么,fiddler在日常的测试工作中,一般都有哪些常见的应用场景呢? ...

  8. YUM仓库服务与PXE网络装机

    1.yum:基于RPM包构建软件更新机制自动解决依赖关系,软件包由软件包库提供 提供方式:ftp服务:ftp://IP地址/仓库目录 Http服务:http ://  IP地址/仓库目录 本地目录:f ...

  9. Python-数学篇之计算方法的目录:

    目录: 1.出本专题的初衷: 2.参考计算方法的书籍: 3.具体算法的实现: (一)出本专题的初衷: 在我们机械专业的大二上学期课程中,能与计算机沾上边的科目就数<计算方法>了.简化为&q ...

  10. spring4整合xfire1.2.6的问题解决

    历史原因,需要用xfire发布webservice,项目用了spring4,整合几个坑,记录下(其他的配置忽略,相关资料比较多): 1. xfire定义bean的时候,用了 singleton 属性, ...