public abstract class

FragmentStatePagerAdapter

extends PagerAdapter

java.lang.Object
   ↳ android.support.v4.view.PagerAdapter
     ↳ android.support.v4.app.FragmentStatePagerAdapter

Class Overview


Implementation of PagerAdapter that uses a Fragment to manage each page. This class also handles saving and restoring of fragment's state.

This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.

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

Subclasses only need to implement getItem(int) and getCount() to have a working adapter.

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

public class FragmentStatePagerSupport 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 FragmentStatePagerAdapter {
        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>

Summary


[Expand]

Inherited Constants
From class android.support.v4.view.PagerAdapter

 
Public Constructors
  FragmentStatePagerAdapter(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.
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.
[Expand]

Inherited Methods
 From class android.support.v4.view.PagerAdapter

 
 From class java.lang.Object

 

Public Constructors


public FragmentStatePagerAdapter (FragmentManager fm)

 
 

Public Methods


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

 

Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).

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

public void finishUpdate (ViewGroup container)

 

Called when the a change in the shown pages has been completed. At this point you must ensure that all of the pages have actually been added or removed from the container as appropriate.

Parameters
container The containing View which is displaying this adapter's page views.

public abstract Fragment getItem (int position)

 

Return the Fragment associated with a specified position.

public Object instantiateItem (ViewGroup container, int position)

 

Create the page for the given position. The adapter is responsible for adding the view to the container given here, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).

Parameters
container The containing View in which the page will be shown.
position The page position to be instantiated.
Returns
  • Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.

public boolean isViewFromObject (View view, Object object)

 

Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int). This method is required for a PagerAdapter to function properly.

Parameters
view Page View to check for association with object
object Object to check for association with view
Returns
  • true if view is associated with the key object object

public void restoreState (Parcelable state, ClassLoader loader)

 

Restore any instance state associated with this adapter and its pages that was previously saved by saveState().

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

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

Returns
  • Saved state for this adapter

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

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

public void startUpdate (ViewGroup container)

 

Called when a change in the shown pages is going to start being made.

Parameters
container The containing View which is displaying this adapter's page views.
Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.
Android 4.4 r1 — 28 Aug 2014 0:14

About Android  |  Legal  |  Support

FragmentStatePagerAdapter的更多相关文章

  1. ViewPager适配器FragmentStatePagerAdapter 与FragmentPagerAdapter

    使用FragmentPagerAdapter存在删除dataSet顺序错乱的问题 改用FragmentStatePagerAdapter

  2. 转载---ViewPager,PagerAdapter,FragmentPagerAdapter和FragmentStatePagerAdapter的分析对比

    转载:http://blog.csdn.net/dreamzml/article/details/9951577 ViewPager ViewPager 如其名所述,是负责翻页的一个 View.准确说 ...

  3. FragmentStatePageradapter 与 FragmentPageradapter的区别

    FragmentPageradapter : 会将fragment储存在内存中 每次加载页面读取内存中的fragment FragmentStatePageradapter: 不会将fragment储 ...

  4. [转][Android]FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别

    原文链接:http://blog.csdn.net/zhaokaiqiang1992 FragmentPagerAdapter是android-support-v4支持包里面出现的一个新的适配器,继承 ...

  5. 【Android界面实现】FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别

    转载请注明出处: http://blog.csdn.net/zhaokaiqiang1992 FragmentPagerAdapter是android-support-v4支持包里面出现的一个新的适配 ...

  6. FragmentPagerAdapter与FragmentStatePagerAdapter区别

    在一个 Android 应用中,我使用 FragmentPagerAdapter 来处理多 Fragment 页面的横向滑动.不过我碰到了一个问题,即当 Fragment 对应的数据集发生改变时,我希 ...

  7. 109、FragmentPagerAdapter与FragmentStatePagerAdapter区别

    x 在一个 Android 应用中,我使用 FragmentPagerAdapter 来处理多 Fragment 页面的横向滑动.不过我碰到了一个问题,即当 Fragment 对应的数据集发生改变时, ...

  8. 38、FragmentStatePagerAdapter分页

    [ ViewPager ] ViewPager 如其名所述,是负责翻页的一个 View.准确说是一个 ViewGroup,包含多个 View 页,在手指横向滑动屏幕时,其负责对 View 进行切换.为 ...

  9. FragmentStatePagerAdapter.notifyDataSetChanged不刷新页面的解决的方法

    公司做医疗产品的,显示操作用的是android.所以我就用上下两个部分大致是固定的,仅仅有中间会有6个页面的切换,当中会有两个用户的切换.即普通用户和管理员用户,图片能够大致展示一下 其他页面是同样的 ...

  10. FragmentPagerAdapter与FragmentStatePagerAdapter差异

    平常使用的FragmentPagerAdapter和FragmentStatePagerAdapter来自android.support.v4.app包用来构建ViewPager. FragmentP ...

随机推荐

  1. Burp+Xray的联动使用

    Burp+Xray的联动使用 步骤如下, 1)首先,我们启动Xray的url监听功能,我们设置监听地址为127.0.0.1,端口为7777.监听的报告输出到xray文件夹根目录下的proxy_test ...

  2. C++ Tips of the Week-01 string_view

    1: string_view ref https://abseil.io/tips/1 函数参数需要是一个字符串的时候,通常的实现有以下几种: // C Convention void TakesCh ...

  3. .NET周报 【6月第4期 2023-06-25】

    国内文章 如何在long-running task中调用async方法 https://www.cnblogs.com/eventhorizon/p/17497359.html long-runnin ...

  4. 白嫖一个WebAPI限流解决方案

    什么是API限流: API 限流是限制用户在一定时间内 API 请求数量的过程.应用程序编程接口 (API) 充当用户和软件应用程序之间的网关.例如,当用户单击社交媒体上的发布按钮时,点击该按钮会触发 ...

  5. 华为云河图KooMap 共筑数字孪生底座 共建产业标杆

    摘译:7月7日至9日,华为开发者大会2023(Cloud)将在东莞溪村盛大举行,由华为云河图KooMap带来的关于数字孪生主题的技术分论坛.圆桌闭门会和精彩成果展示 7月7日至9日,华为开发者大会20 ...

  6. 知识图谱之《海贼王-ONEPICE》领域图谱项目实战(含码源):数据采集、知识存储、知识抽取、知识计算、知识应用、图谱可视化、问答系统(KBQA)等

    知识图谱之<海贼王-ONEPICE>领域图谱项目实战(含码源):数据采集.知识存储.知识抽取.知识计算.知识应用.图谱可视化.问答系统(KBQA)等 实体关系可视化页面可视化页面尝鲜 1. ...

  7. go NewTicker 得使用

    转载请注明出处: 在 Go 语言中,time.NewTicker 函数用于创建一个周期性触发的定时器.它会返回一个 time.Ticker 类型的值,该值包含一个通道 C,定时器会每隔一段时间向通道 ...

  8. 我用numpy实现了VIT,手写vision transformer, 可在树莓派上运行,在hugging face上训练模型保存参数成numpy格式,纯numpy实现

    先复制一点知乎上的内容 按照上面的流程图,一个ViT block可以分为以下几个步骤 (1) patch embedding:例如输入图片大小为224x224,将图片分为固定大小的patch,patc ...

  9. 【活动回顾】WebRTC服务端工程实践和优化探索

    11月7日,即构和上海GDG技术社区联合举办了实时音视频技术云上技术分享专场,来自即构科技和Bilibili的资深技术专家进行了深度分享.大会吸引了众多开发人员交流.观看,并在活动过程中与分享嘉宾进行 ...

  10. Hexo博客Next6.0版本主题配置(站内搜索、新建404界面、静态资源压缩、底部信息隐藏、各版块透明度修改、字数统计、推荐阅读、博文置顶、阅读进度、在线评论、运行时间)

    新建404界面 在站点根目录下,输入hexo new page 404,在默认Hexo站点下/source/404/index.md 打开新建的404界面,编辑属于自己的404界面,可以显示腾讯公益4 ...