Android FragmentPagerAdapter翻译

本文详细介绍了FragmentPagerAdapter在Android开发中的使用方法,包括其基本概念、适用场景、实现方式以及实例代码解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
voiddestroyItem(ViewGroup container, int position, Object object)
根据给定的position移除一个page页
voidfinishUpdate(ViewGroup container)
当页面数据加载完成时调用该方法
abstract FragmentgetItem(int position)
返回指定位置的相关fragment
longgetItemId(int position)
返回给定位置的item的标示符
ObjectinstantiateItem(ViewGroup container, int position)
在给定的位置处创建一个page
booleanisViewFromObject(View view, Object object)
Determines whether a page View is associated with a specific key object as returned by  instantiateItem(ViewGroup, int).
voidrestoreState(Parcelable state, ClassLoader loader)
恢复所有的通过saveState()方法保存的与adapter关联的页面实例状态
ParcelablesaveState()
保存所有与adapter相关的页面实例。直到调用restoreState方法是恢复
voidsetPrimaryItem(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.
voidstartUpdate(ViewGroup container)
当页面将要被显示时调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值