目录

FragmentPagerAdapter

目录

FragmentPagerAdapter的官方解释为:

Implementation of [PagerAdapter](file:///Users/zaizai/Library/Application%20Support/Dash/DocSets/Android/Android.docset/Contents/Resources/Documents/developer.android.com/reference/android/support/v4/view/PagerAdapter.html) that represents each page as a [Fragment](file:///Users/zaizai/Library/Application%20Support/Dash/DocSets/Android/Android.docset/Contents/Resources/Documents/developer.android.com/reference/android/app/Fragment.html) that is persistently kept in the fragment manager as long as the user can return to the page.

实现PagerAdapter去展示每一个以Fragment表示的页面,只要用户可以返回pager,这些pager将一直保持处于fragment管理中。

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state.

当考虑使用少量的页面最好是静态的fragment时,此时使用FragmentPagerAdapter是最合适的,例如一个卡开集合。用户访问的每一个fragment都将被保存在内存中,要注意的是其视图层次在不可见时可能被销毁。因为fragment实例化可以保持任意数量的状态值,这可能导致使用大量的存储器。

When using FragmentPagerAdapter the host ViewPager must have a valid ID set.

Subclasses only need to implement [getItem(int)](file:///Users/zaizai/Library/Application%20Support/Dash/DocSets/Android/Android.docset/Contents/Resources/Documents/developer.android.com/reference/android/support/v13/app/FragmentPagerAdapter.html#getItem(int)) and [getCount()](file:///Users/zaizai/Library/Application%20Support/Dash/DocSets/Android/Android.docset/Contents/Resources/Documents/developer.android.com/reference/android/support/v4/view/PagerAdapter.html#getCount()) to have a working adapter.

Here is an example implementation of a pager containing fragments of lists:

当使用fragmentPagerAdapter时,控制者必须有一个有效的id集合,子类只需要实现getItem(int)和getCount()就可以实现一个可用的适配器,一下是一个案例

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
public class FragmentPagerSupport extends Activity {
    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(getFragmentManager());

        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);
        }
    }
}