FragmentPagerAdapter

  发布日期:   2017-06-01
  最新修改:   2020-05-22
  阅读次数:   71 次

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()就可以实现一个可用的适配器,一下是一个案例

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

   转载规则

《FragmentPagerAdapter字》GajAngels 采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可。