Android开发模板------ViewPager(二)FragmentPagerAdapter的简介

来源:互联网 发布:嵌入式linux打开图片 编辑:程序博客网 时间:2024/04/30 14:55

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

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

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

FragmentPagerAdapter的子类只要实现 getItem(int)  getCount()方法。

public class MainActivity extends ActionBarActivity {private ViewPager viewpager;private LinearLayout layoutDot;public static int NUM_ITEM = 4;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_main);initView();}private void initView() {viewpager = (ViewPager) findViewById(R.id.viewpager);layoutDot = (LinearLayout) findViewById(R.id.groupDot);MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());viewpager.setAdapter(adapter);findViewById(R.id.btnFirst).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {viewpager.setCurrentItem(0);}});findViewById(R.id.btnLast).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {viewpager.setCurrentItem(NUM_ITEM - 1);}});}public static class MyFragmentPagerAdapter extends FragmentPagerAdapter {public MyFragmentPagerAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int arg0) {return ArrayListFragment.newInstance(arg0);//<span style="white-space:pre"></span>或者如下//<span style="white-space:pre"></span>Fragment fragment;//<span style="white-space:pre"></span>if (arg0 == 0) {//<span style="white-space:pre"></span>fragment = new MainFragment();//<span style="white-space:pre"></span>}else if(arg0 == 1){//<span style="white-space:pre"></span>fragment = new MyFragment();//<span style="white-space:pre"></span>}else {//<span style="white-space:pre"></span>fragment = null;//<span style="white-space:pre"></span>}//<span style="white-space:pre"></span>return fragment;}@Overridepublic int getCount() {return NUM_ITEM;}}public static class ArrayListFragment extends ListFragment {int mNum;static ArrayListFragment newInstance(int num) {ArrayListFragment f = new ArrayListFragment();Bundle args = new Bundle();args.putInt("num", num);f.setArguments(args);return f;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mNum = getArguments() != null ? getArguments().getInt("num") : 1;}@Overridepublic View 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;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, new String[] {"hello", "hello", "hello", "hello", "hello","hello", }));}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {Log.i("FragmentList", "Item clicked: " + id);}}}



fragment_main的布局为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button         android:id="@+id/btnFirst"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="BtnFirst"/>    <Button         android:id="@+id/btnLast"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/btnFirst"        android:text="BtnLast"/>    <android.support.v4.view.ViewPager         android:id="@+id/viewpager"         android:layout_below="@id/btnLast"        android:layout_width="match_parent"          android:layout_height="match_parent" />    <LinearLayout        android:id="@+id/groupDot"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"          android:layout_centerHorizontal="true"          android:gravity="center"          android:layout_marginBottom="40dip"        android:orientation="horizontal">    </LinearLayout></RelativeLayout>



fragment_pager_list的布局为:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:drawable/gallery_thumb"    android:orientation="vertical" >    <TextView        android:id="@+id/text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical|center_horizontal"        android:text="@string/hello_world"        android:textAppearance="?android:attr/textAppearanceMedium" />    <!--   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        -->        <ListView            android: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 -->        <TextView            android:id="@android:id/empty"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="No items."            android:textAppearance="?android:attr/textAppearanceMedium" />    </FrameLayout></LinearLayout>




0 0
原创粉丝点击