FragmentPagerAdapter
java.lang.Object ↳ android.support.v4.view.PagerAdapter ↳ android.support.v4.app.FragmentPagerAdapter
java.lang.Object | ||
↳ | android.support.v4.view.PagerAdapter | |
↳ | android.support.v4.app.FragmentPagerAdapter |
Summary
[Expand]
Inherited Constants
| |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
FragmentPagerAdapter( FragmentManager fm) |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
destroyItem(
ViewGroup container, int position,
Object object)
Remove a page for the given position.
| ||||||||||
void |
finishUpdate(
ViewGroup container)
Called when the a change in the shown pages has been completed.
| ||||||||||
abstract Fragment |
getItem(int position)
Return the Fragment associated with a specified position.
| ||||||||||
long |
getItemId(int position)
Return a unique identifier for the item at the given position.
| ||||||||||
Object |
instantiateItem(
ViewGroup container, int position)
Create the page for the given position.
| ||||||||||
boolean |
isViewFromObject(
View view,
Object object)
Determines whether a page View is associated with a specific key object as returned by
instantiateItem(ViewGroup, int) .
| ||||||||||
void |
restoreState(
Parcelable state,
ClassLoader loader)
Restore any instance state associated with this adapter and its pages that was previously saved by
saveState() .
| ||||||||||
Parcelable |
saveState()
Save any instance state associated with this adapter and its pages that should be restored if the current UI state needs to be reconstructed.
| ||||||||||
void |
setPrimaryItem(
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.
| ||||||||||
void |
startUpdate(
ViewGroup container)
Called when a change in the shown pages is going to start being made.
|
FragmentPagerAdapter是PagerAdapter中的其中一种实现。是专门为了Fragment而出现的一个快捷的适配器;它将每一个页面表示为一个 Fragment,并且每一个Fragment都将会保存到fragment manager当中。而且,当用户没可能再次回到页面的时候,fragment manager才会将这个Fragment销毁.出于使用FragmentPagerAdapter 时,Fragment对象会一直存留在内存中,所以当有大量的显示页时,就不适合用FragmentPagerAdapter 了,FragmentPagerAdapter 适用于只有少数的page情况,像选项卡。这个时候你可以考虑使用FragmentStatePagerAdapter ,当使用FragmentStatePagerAdapter 时,如果Fragment不显示,那么Fragment对象会被销毁,但在回调onDestroy()方法之前会回调onSaveInstanceState(Bundle outState)方法来保存Fragment的状态,下次Fragment显示时通过onCreate(Bundle savedInstanceState)把存储的状态值取出来,FragmentStatePagerAdapter 比较适合页面比较多的情况,像一个页面的ListView
当使用FragmentPagerAdapter的时候,ViewPager一定要使用正确的ID set。
FragmentPagerAdapter的子类只要实现 getItem(int) 和 getCount()方法。
注意:
FragmentPagerAdapter与FragmentStatePagerAdapter差异
平常使用的FragmentPagerAdapter和FragmentStatePagerAdapter来自android.support.v4.app包用来构建ViewPager。
FragmentPagerAdapter更多的用于少量界面的ViewPager,比如Tab。划过的fragment会保存在内存中,尽管已经划过。而FragmentStatePagerAdapter和ListView有点类似,会保存当前界面,以及下一个界面和上一个界面(如果有),最多保存3个,其他会被销毁掉。
要注意的是FragmentStatePagerAdapter可能不经意间会造成内存未正常回收,严重导致内存溢出,比如图片资源没有释放,资源引用问题。(之前碰到过EditTextt由于保存焦点导致Fragment未被释放,以至于内存溢出,设置editText.saveEanble(false)就可以解决此问题)。
在数据刷新是可能有很多朋友会遇到无法刷新;下面推荐一篇:
http://blog.sina.com.cn/s/blog_783ede03010173b4.html
附录官方给的一个小Demo:
Here is an example implementation of a pager containing fragments of lists:
public class FragmentPagerSupport extends FragmentActivity { static final int NUM_ITEMS = 10; MyAdapter mAdapter; ViewPager mPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_pager); mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager)findViewById(R.id.pager); mPager.setAdapter(mAdapter); // Watch for button clicks. Button button = (Button)findViewById(R.id.goto_first); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { mPager.setCurrentItem(0); } }); button = (Button)findViewById(R.id.goto_last); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { mPager.setCurrentItem(NUM_ITEMS-1); } }); } public static class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { return ArrayListFragment.newInstance(position); } } public static class ArrayListFragment extends ListFragment { int mNum; /** * Create a new instance of CountingFragment, providing "num" * as an argument. */ static ArrayListFragment newInstance(int num) { ArrayListFragment f = new ArrayListFragment(); // Supply num input as an argument. Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args); return f; } /** * When creating, retrieve this instance's number from its arguments. */ @Override public void 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 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, Cheeses.sCheeseStrings)); } @Override public void 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:
<LinearLayout xmlns: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> <LinearLayout android:orientation="horizontal" android:gravity="center" android:measureWithLargestChild="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"> <Button android:id="@+id/goto_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/first"> </Button> <Button android: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:
<LinearLayout xmlns: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"> <TextView android: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 --> <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:textAppearance="?android:attr/textAppearanceMedium" android:text="No items."/> </FrameLayout> </LinearLayout>