FragmentPagerAdapter API

FragmentPagerAdapter是PagerAdapter中的其中一种实现。它将每一个页面表示为一个 Fragment,并且每一个Fragment都将会保存到fragment manager当中。而且,当用户没可能再次回到页面的时候,fragment manager才会将这个Fragment销毁。

这种pager十分适用于有一些静态fragment,例如一组tabs,的时候使用。每个页面对应的Fragment当用户可以访问的时候会一直存在内存中,但是,当这个页面不可见的时候,view hierarchy将会被销毁。这样子会导致应用程序占有太多资源。当页面数量比较大的时候,建议使用 FragmentStatePagerAdapter

当使用FragmentPagerAdapter的时候,ViewPager一定要使用正确的ID set

FragmentPagerAdapter的子类只要实现 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);

 

       // Watchfor 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 ofCountingFragment, 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 thisinstance'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 simpletext 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 individualfragment'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"/>

 

   <!-- Theframe 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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值