Android的FragmentStatePagerAdapter [英] Android FragmentStatePagerAdapter

查看:85
本文介绍了Android的FragmentStatePagerAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个FragmentStatePagerAdapter <一个href="http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html">this例如。

I'm working with a FragmentStatePagerAdapter using this example.

该MyAdapter类实现如下:

The MyAdapter class is implemented as follows:

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

该ListFragment类包含以下方法来创建一个新的实例:

The ListFragment class includes the following method to create a new instance:

    /**
     * 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;
    }

当我在活动创建一个新的碎片状态寻呼机适配器,的getItem 被调用,从而调用的newInstance 在ListFragment类方法。这是伟大的,当我想创建一个新的片段。

When I create a new fragment state pager adapter in my activity, getItem is called, which in turn calls the newInstance method in the ListFragment class. This is great when I want to create a new fragment.

但它不是我清楚如何修改的getItem (如果连需要)来获得片段对象的时,它已经存在的和用户例如从第2页第1页。我想我的活动来获取pviously创建片段,现有的,$ P $,这样它可以运行一个内部类 AsyncMethod ,它驻留在片段类。

But it's not clear to me how to modify getItem (if even needed) to get the fragment object when it already exists and the user pages from, for example, page 2 to page 1. I'd like my Activity to retrieve that existing, previously created fragment so that it can run an inner class AsyncMethod, which resides in the fragment class.

推荐答案

我已经实现类似于你有什么事情。我延长了 FragmentPagerAdapter 类,像这样:

I have implemented something similar to what you have. I extended the FragmentPagerAdapter class like so:

public class ContactsFragmentPagerAdapter extends FragmentPagerAdapter {
    ActionBar mActionBar;
    private List<Fragment> mFragments;

    public ContactsFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        mFragments = fragments;
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    public void setActionBar(ActionBar bar) {
        mActionBar = bar;
    }
}

注意我添加了一个构造函数的参数传递列表片段对象研究。这样,这个类的的getItem()方法可以返回任何类,扩展片段或它的子类,而不是只是一个特定的类 ArrayListFragment 像你这样做。

Notice I have added an argument to the constructor to pass in the List of Fragment objects. This way the getItem() method of this class can return any class that extends Fragment or any of its subclasses and not just one specific class ArrayListFragment like you have done.

活动我的实例我的子类 FragmentPagerAdapter 我已经通过了<$ C $名单C>片段对象:

In the Activity where I instantiate my subclass of FragmentPagerAdapter I have passed in the list of Fragment objects:

类的实例化FragmentPagerAdapter

Class the instantiates the FragmentPagerAdapter

public final class ContactManager extends Activity {
    private ContactsFragmentPagerAdapter mAdapter;
    private ViewPager mPager;
    public ActionBar mActionBar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.contact_manager);

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, ContactsListFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, GroupsListFragment.class.getName()));
        mAdapter = new ContactsFragmentPagerAdapter(this.getFragmentManager(), fragments);

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int arg0) {}

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {}

            @Override
            public void onPageSelected(int arg0) {
                mActionBar.getTabAt(arg0).select();
            }
        });

    }

}

通过访问变量碎片,您可以访问previously创建片段这样您就可以运行该方法片段

By accessing the variable "fragments", you can access a previously created Fragment so that you can run methods of that Fragment.

这篇关于Android的FragmentStatePagerAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆