天天看點

Android開發模闆------ViewPager(二)FragmentPagerAdapter的簡介

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;

	@Override
	protected 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() {

			@Override
			public void onClick(View v) {
				viewpager.setCurrentItem(0);
			}
		});
		findViewById(R.id.btnLast).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				viewpager.setCurrentItem(NUM_ITEM - 1);
			}
		});
	}

	public static class MyFragmentPagerAdapter extends FragmentPagerAdapter {

		public MyFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public 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;
		}

		@Override
		public 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;
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			mNum = getArguments() != null ? getArguments().getInt("num") : 1;
		}

		@Override
		public 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;
		}

		@Override
		public 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", }));
		}

		@Override
		public 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>