我如何使用标签导航传递时,片段之间的数据? [英] How can I pass data between fragments when using tab navigation?

查看:155
本文介绍了我如何使用标签导航传递时,片段之间的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的应用程序标签导航。每个标签都有preforms类似的任务形式,所以我用同样的片段,而只是隐藏/显示它的位。看来该ViewPager被实例化该片段的3个不同的副本,然后在它们之间切换,当用户更改标签。因为所有的碎片都是一样的,我想通过他们之间的字段的状态。这样,当用户更改不同的选项卡,它们填补了previous选项卡上的信息是新的标签present。

有没有preferred方法以这种方式传递数据?

的简单化code看起来像这样...

 公共类MainActivity扩展活动实现ActionBar.TabListener {    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        最后的动作条动作条= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        mSectionsPagerAdapter =新SectionsPagerAdapter(getFragmentManager(),这一点);        mViewPager =(ViewPager)findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);         mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
            @覆盖
            公共无效使用onPageSelected(INT位置){
                actionBar.setSelectedNavigationItem(位置);
            }
        });         的for(int i = 0; I< mSectionsPagerAdapter.getCount();我++){
            actionBar.addTab(
                actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(I))
                    .setTabListener(本));
        }
    }    @覆盖
    公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
        mViewPager.setCurrentItem(tab.getPosition());
    }    公共类SectionsPagerAdapter扩展FragmentPagerAdapter {        私人上下文的背景下;        公共SectionsPagerAdapter(FragmentManager FM){
            超(FM);
        }        公共SectionsPagerAdapter(FragmentManager FM,上下文的背景下){
            超(FM);
            this.context =背景;
        }        @覆盖
        公众诠释的getCount(){
            //显示3总页​​数。
            返回3;
        }        @覆盖
        公共CharSequence的getPageTitle(INT位置){
            区域设置L = Locale.getDefault();
            开关(位置){
                情况下0:
                    返回getResources()的getString(R.string.tab_one_title)。
                情况1:
                    返回getResources()的getString(R.string.tab_two_title)。
                案例2:
                    返回getResources()的getString(R.string.tab_three_title)。
            }
            返回null;
        }
    }    公共类MainFragment扩展片段{        私有静态最后弦乐ARG_SECTION_TYPE =部分类型;        公共MainFragment(){}        公共MainFragment(INT sectionNumber){
            捆绑ARGS =新包();
            args.putInt(ARG_SECTION_TYPE,sectionNumber);
            setArguments(参数);
        }        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);            //设置视图
            开关(getArguments()。调用getInt(ARG_SECTION_TYPE)){
                //根据页码隐藏或显示领域。
            }            返回rootView;
        }
    }
}

如果用户修改的第1页,我想这些信息也显示在 2页 3页。什么是preferred方法来做到这一点?


解决方案

  1. 数据推法:LocalBroadcastManager

每个片段注册一个BroadcastReceiver,当数据更改其广播到所有注册的接收器。这是当在接收的对象被更新以决定数据的发送者。

有关实施LocalBroadcastManager的例子​​:如何使用LocalBroadcastManager

<醇开始=2>
  • 数据 - 拉法:中央存储库

  • 数据发送到中介对象(中央储存库)来存储数据。然后各片段可以轮询对象时,它需要与最新的数据进行更新。

    在这种情况下,活动将引用对象,每个片段将被调用的库((MyInterface的)getActivity).mDataObject.data ....

    I'm using tab navigation in my app. Each tab has a form that preforms similar tasks, so I'm using the same fragment but simply hiding/showing bits of it. It appears that the ViewPager is instantiating 3 different copies of the fragment, and then switching between them when the user changes tabs. Since all the fragments are the same, I'd like to pass the state of the fields between them. This way when a user changes to a different tab, the information they filled in on the previous tab is present on the new tab.

    Is there a preferred method to passing data in this way?

    The dumbed down code looks like this...

    public class MainActivity extends Activity implements ActionBar.TabListener {
    
        SectionsPagerAdapter mSectionsPagerAdapter;
        ViewPager mViewPager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final ActionBar actionBar = getActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
            mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), this);
    
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);
    
             mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });
    
             for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
                actionBar.addTab(
                    actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
            }
        }
    
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            mViewPager.setCurrentItem(tab.getPosition());
        }
    
        public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
            private Context context;
    
            public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            public SectionsPagerAdapter(FragmentManager fm, Context context) {
                super(fm);
                this.context = context;
            }
    
            @Override
            public int getCount() {
                // Show 3 total pages.
                return 3;
            }
    
            @Override
            public CharSequence getPageTitle(int position) {
                Locale l = Locale.getDefault();
                switch (position) {
                    case 0:
                        return getResources().getString(R.string.tab_one_title);
                    case 1:
                        return getResources().getString(R.string.tab_two_title);
                    case 2:
                        return getResources().getString(R.string.tab_three_title);
                }
                return null;
            }
        }
    
        public class MainFragment extends Fragment {
    
            private static final String ARG_SECTION_TYPE = "section type";
    
            public MainFragment(){}
    
            public MainFragment(int sectionNumber) {
                Bundle args = new Bundle();
                args.putInt(ARG_SECTION_TYPE, sectionNumber);
                setArguments(args);
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    
                //setup the view
                switch(getArguments().getInt(ARG_SECTION_TYPE)) {
                    //hide or show fields based on page number.
                }
    
                return rootView;
            }
        }
    }
    

    If the user modifies a field on page 1, i'd like the information to also show up on page 2, and page 3. What's the preferred method to accomplish this?

    解决方案

    1. Data-Push Method: LocalBroadcastManager

    Each fragment registers a broadcastReceiver, and when data changes broadcast it to all registered receivers. It's the sender of the data that decides when the receiving objects are updated.

    For an example of implementing a LocalBroadcastManager: how to use LocalBroadcastManager?

    1. Data-Pull Method: Central repository.

    Send the data to an intermediary object (the central repository) to store the data. Then each fragment can poll the object whenever it needs to be updated with the latest data.

    In this case, the activity would reference the object, and each fragment would call to the repository by ((MyInterface) getActivity).mDataObject.data....

    这篇关于我如何使用标签导航传递时,片段之间的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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