Most visited

Recently visited

FragmentPagerAdapter

public abstract class FragmentPagerAdapter
extends PagerAdapter

java.lang.Object
    android.support.v4.view.PagerAdapter
      android.support.v13.app.FragmentPagerAdapter


实现 PagerAdapter ,表示每个页面为一个 Fragment ,只要用户可以返回页面,该页面将永久保存在片段管理器中。

这种版本的寻呼机最适合用于有少量通常需要寻呼的静态片段,比如一组选项卡。 用户访问的每个页面的片段将保存在内存中,尽管其视图层次结构在不可见时可能会被破坏。 这可能导致使用大量的内存,因为片段实例可以保持任意数量的状态。 对于更大的页面集,请考虑FragmentStatePagerAdapter

当使用FragmentPagerAdapter时,主机ViewPager必须有一个有效的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);
        }
    }
}

顶级片段的 R.layout.fragment_pager资源是:

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

包含每个单独片段的布局的 R.layout.fragment_pager_list资源是:

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

摘要(Summary)

Inherited constants

From class android.support.v4.view.PagerAdapter

Public constructors

FragmentPagerAdapter(FragmentManager fm)

公共方法(Public methods)

void destroyItem(ViewGroup container, int position, Object object)

删除给定位置的页面。

void finishUpdate(ViewGroup container)

在所示页面中的更改完成时调用。

abstract Fragment getItem(int position)

返回与指定位置关联的碎片。

long getItemId(int position)

返回给定位置上项目的唯一标识符。

Object instantiateItem(ViewGroup container, int position)

创建给定位置的页面。

boolean isViewFromObject(View view, Object object)

确定页面视图是否与 instantiateItem(ViewGroup, int)返回的特定键对象相关联。

void restoreState(Parcelable state, ClassLoader loader)

还原与此适配器及其先前由 saveState()保存的页面关联的任何实例状态。

Parcelable saveState()

如果需要重建当前UI状态,请保存与此适配器及其应恢复的页面关联的任何实例状态。

void setPrimaryItem(ViewGroup container, int position, Object object)

被调用以通知适配器当前哪个项目被认为是“主要”,这是向用户显示的当前页面。

void startUpdate(ViewGroup container)

当所显示页面的变化即将开始时调用。

继承方法(Inherited methods)

From class android.support.v4.view.PagerAdapter
From class java.lang.Object

Public constructors

FragmentPagerAdapter

FragmentPagerAdapter (FragmentManager fm)

参数(Parameters)
fm FragmentManager

公共方法(Public methods)

destroyItem

void destroyItem (ViewGroup container, 
                int position, 
                Object object)

删除给定位置的页面。 适配器负责从容器中移除视图,但它只能确保在从finishUpdate(ViewGroup)返回时完成finishUpdate(ViewGroup)

参数(Parameters)
container ViewGroup: The containing View from which the page will be removed.
position int: The page position to be removed.
object Object: The same object that was returned by instantiateItem(View, int).

finishUpdate

void finishUpdate (ViewGroup container)

在所示页面中的更改完成时调用。 此时,您必须确保所有页面实际上已根据需要添加或从容器中移除。

参数(Parameters)
container ViewGroup: The containing View which is displaying this adapter's page views.

getItem

Fragment getItem (int position)

返回与指定位置关联的碎片。

参数(Parameters)
position int
返回(Returns)
Fragment

getItemId

long getItemId (int position)

返回给定位置上项目的唯一标识符。

默认实现返回给定的位置。 如果项目的位置可以更改,子类应该覆盖此方法。

参数(Parameters)
position int: Position within this adapter
返回(Returns)
long Unique identifier for the item at position

instantiateItem

Object instantiateItem (ViewGroup container, 
                int position)

创建给定位置的页面。 适配器负责将视图添加到此处给出的容器,但它只能确保在从finishUpdate(ViewGroup)返回时完成此操作。

参数(Parameters)
container ViewGroup: The containing View in which the page will be shown.
position int: The page position to be instantiated.
返回(Returns)
Object Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.

isViewFromObject

boolean isViewFromObject (View view, 
                Object object)

确定页面视图是否与instantiateItem(ViewGroup, int)返回的特定键对象相关联。 这个方法对于PagerAdapter正常运行是必需的。

参数(Parameters)
view View: Page View to check for association with object
object Object: Object to check for association with view
返回(Returns)
boolean true if view is associated with the key object object

restoreState

void restoreState (Parcelable state, 
                ClassLoader loader)

还原与此适配器及其先前由 saveState()保存的页面关联的任何实例状态。

参数(Parameters)
state Parcelable: State previously saved by a call to saveState()
loader ClassLoader: A ClassLoader that should be used to instantiate any restored objects

saveState

Parcelable saveState ()

如果需要重建当前UI状态,请保存与此适配器及其应恢复的页面关联的任何实例状态。

返回(Returns)
Parcelable Saved state for this adapter

setPrimaryItem

void setPrimaryItem (ViewGroup container, 
                int position, 
                Object object)

被调用以通知适配器当前哪个项目被认为是“主要”,这是向用户显示的当前页面。

参数(Parameters)
container ViewGroup: The containing View from which the page will be removed.
position int: The page position that is now the primary.
object Object: The same object that was returned by instantiateItem(View, int).

startUpdate

void startUpdate (ViewGroup container)

当所显示页面的变化即将开始时调用。

参数(Parameters)
container ViewGroup: The containing View which is displaying this adapter's page views.

Hooray!