Fragment与Viewpager及FragmentStatePagerAdapter

前两篇我们讲过PagerAdapter和FragmentPagerAdapter,这里将介绍第三个适配器即FragmentStatePagerAdapter,它和ListView有点类似,会保存当前界面,以及下一个界面和上一个界面(如果有),最多会保存3个,其他会被销毁掉。需要注意的是FragmentStatePagerAdapter可能会不经意间造成内存未正常回收,严重导致内存溢出,比如图片资源没有释放,资源引用问题。

MainActivity.java代码:

public class MainActivity extends FragmentActivity {
	private ViewPager viewPager;
	static final int NUM_ITEMS = 10;// 定义一共有10页
	private MyAdapter adapter;
	private Button go_first;
	private Button go_last;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		viewPager = (ViewPager) this.findViewById(R.id.pager);

		adapter = new MyAdapter(getSupportFragmentManager());

		go_first = (Button) this.findViewById(R.id.goto_first);
		go_last = (Button) this.findViewById(R.id.goto_last);

		go_first.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				viewPager.setCurrentItem(0);
			}
		});

		go_last.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				viewPager.setCurrentItem(NUM_ITEMS - 1);
			}
		});

		viewPager.setAdapter(adapter);
	}

	// 填充适配器的数据:每一页都是Fragment
	public static class MyAdapter extends FragmentStatePagerAdapter {

		public MyAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return ArrayListFragment.getIntances(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return NUM_ITEMS;
		}

	}

	public static class ArrayListFragment extends ListFragment {

		int num;

		static ArrayListFragment getIntances(int num) {
			ArrayListFragment listFragment = new ArrayListFragment();
			Bundle bundle = new Bundle();
			bundle.putInt("num", num);
			listFragment.setArguments(bundle);
			return listFragment;
		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onActivityCreated(savedInstanceState);
			setListAdapter(new ArrayAdapter<String>(getActivity(),
					android.R.layout.simple_list_item_1, getData()));
		}

		public List<String> getData() {
			List<String> list = new ArrayList<String>();
			for (int i = 0; i < 20; i++) {
				list.add("jack" + i);
			}
			return list;
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			System.out.println("------>>" + num);
			num = (getArguments() != null ? getArguments().getInt("num") : 1);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {

			View view = inflater.inflate(R.layout.fragment_pager_list, null);
			TextView textView = (TextView) view.findViewById(R.id.text);
			textView.setText("Fragment #" + num);
			// TODO Auto-generated method stub
			return view;
		}

		@Override
		public void onPause() {
			// TODO Auto-generated method stub
			super.onPause();
		}

		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			Log.i("FragmentList", "Item clicked: " + id);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


activity_main.xml:

<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="4dip"
    tools:context=".MainActivity" >

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="center"
        android:measureWithLargestChild="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/goto_first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳到首页" >
        </Button>

        <Button
            android:id="@+id/goto_last"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳到尾页" >
        </Button>
    </LinearLayout>

</LinearLayout>


fragment_pager_list.xml:

<?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" />

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值