<bdo id='eIh1P'></bdo><ul id='eIh1P'></ul>
<i id='eIh1P'><tr id='eIh1P'><dt id='eIh1P'><q id='eIh1P'><span id='eIh1P'><b id='eIh1P'><form id='eIh1P'><ins id='eIh1P'></ins><ul id='eIh1P'></ul><sub id='eIh1P'></sub></form><legend id='eIh1P'></legend><bdo id='eIh1P'><pre id='eIh1P'><center id='eIh1P'></center></pre></bdo></b><th id='eIh1P'></th></span></q></dt></tr></i><div id='eIh1P'><tfoot id='eIh1P'></tfoot><dl id='eIh1P'><fieldset id='eIh1P'></fieldset></dl></div>

    <legend id='eIh1P'><style id='eIh1P'><dir id='eIh1P'><q id='eIh1P'></q></dir></style></legend>
  1. <small id='eIh1P'></small><noframes id='eIh1P'>

    1. <tfoot id='eIh1P'></tfoot>
    2. 如何在 viewpager 中的片段之间传递字符串

      How to pass a string between fragments in a viewpager(如何在 viewpager 中的片段之间传递字符串)

        <bdo id='VgoKv'></bdo><ul id='VgoKv'></ul>

          <legend id='VgoKv'><style id='VgoKv'><dir id='VgoKv'><q id='VgoKv'></q></dir></style></legend>
          • <small id='VgoKv'></small><noframes id='VgoKv'>

              <tbody id='VgoKv'></tbody>

              <tfoot id='VgoKv'></tfoot>
              <i id='VgoKv'><tr id='VgoKv'><dt id='VgoKv'><q id='VgoKv'><span id='VgoKv'><b id='VgoKv'><form id='VgoKv'><ins id='VgoKv'></ins><ul id='VgoKv'></ul><sub id='VgoKv'></sub></form><legend id='VgoKv'></legend><bdo id='VgoKv'><pre id='VgoKv'><center id='VgoKv'></center></pre></bdo></b><th id='VgoKv'></th></span></q></dt></tr></i><div id='VgoKv'><tfoot id='VgoKv'></tfoot><dl id='VgoKv'><fieldset id='VgoKv'></fieldset></dl></div>
              1. 本文介绍了如何在 viewpager 中的片段之间传递字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我试图在 viewpager 中的两个片段之间传递一个字符串,但我没有找到正确的方法.到目前为止,这是我的代码:

                I'm trying to pass a string between two fragments in a viewpager but I didn't found the right way to do it. Here is my code so far :

                public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
                
                    final int PAGE_COUNT = 6;
                
                    /** Constructor of the class */
                    public MyFragmentPagerAdapter(FragmentManager fm) {
                        super(fm);
                    }
                
                    /** This method will be invoked when a page is requested to create */
                    @Override
                    public Fragment getItem(int arg0) {
                
                
                        switch (arg0) {
                        case 0:
                            return new MyFragment();
                        case 1:
                            return new part2();
                        case 2:
                            return new Part3();
                        case 3:
                            return new Part4();
                        case 4:
                            return new Part5();
                        case 5:
                            return new Part6();
                        default:
                            return null;
                        }
                    }
                
                    /** Returns the number of pages */
                    @Override
                    public int getCount() {     
                        return PAGE_COUNT;
                    }
                
                    @Override
                    public CharSequence getPageTitle(int position) {        
                        return "Page #" + ( position + 1 );
                    }
                
                
                
                
                
                }
                
                public class MainActivity extends FragmentActivity {
                
                    @Override
                    public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                
                        /** Getting a reference to the ViewPager defined the layout file */        
                        ViewPager pager = (ViewPager) findViewById(R.id.pager);
                
                        /** Getting fragment manager */
                        FragmentManager fm = getSupportFragmentManager();
                
                        /** Instantiating FragmentPagerAdapter */
                        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
                
                        /** Setting the pagerAdapter to the pager object */
                        pager.setAdapter(pagerAdapter);
                
                    }
                
                    @Override
                    public boolean onCreateOptionsMenu(Menu menu) {
                        getMenuInflater().inflate(R.menu.activity_main, menu);
                        return true;
                    }
                }
                

                我没有在 viewpager 中使用标签.我应该在片段中写什么?

                I'm not using tabs with the viewpager. What should I write in the fragments?

                更新:我尝试实现答案中建议的接口:

                UPDATE : I tried to implement an interface like suggested in the answers :

                public interface OnTogglePressListener {
                    public void onTogglePressed(String msg);
                }
                

                在第一个片段上:

                @Override
                public void onAttach(Activity activity) {
                        // TODO Auto-generated method stub
                        try {
                            buttonListener = (OnTogglePressListener) getActivity();
                        } catch (ClassCastException e) {
                            throw new ClassCastException(activity.toString() + " must implement onTogglePressed");
                        }
                        super.onAttach(activity);
                    }
                
                @Override
                public void onViewCreated(View view, Bundle savedInstanceState) {
                        // TODO Auto-generated method stub
                        buttonListener.onTogglePressed("Off");
                ... }
                

                关于第二个片段:

                public String getEtat(String etat)
                    {   state =etat;
                        return etat;
                
                    }
                

                关于主要活动:

                public void onTogglePressed(String etat)
                    {
                        Part5 p = (Part5)getSupportFragmentManager().findFragmentById(R.layout.frag_2);
                        p.getEtat(etat);
                
                    }
                

                但我每次都收到 NullPointerException.

                But I got a NullPointerException everytime.

                推荐答案

                所以我找到了解决方案,实现是正确的,我唯一应该在第二个片段的方法中做我想要的处理,就是这样!

                So i found the solution, the implementation is correct, the only thing that I should do the treatement I want in the method on the second fragment and that's it !

                这篇关于如何在 viewpager 中的片段之间传递字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
                cocos2d-android: how to display score(cocos2d-android:如何显示分数)
                Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
                SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
                Android file copy(安卓文件拷贝)
                Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)
              2. <tfoot id='NeSFL'></tfoot>

                      <bdo id='NeSFL'></bdo><ul id='NeSFL'></ul>
                          <tbody id='NeSFL'></tbody>

                        <small id='NeSFL'></small><noframes id='NeSFL'>

                      1. <legend id='NeSFL'><style id='NeSFL'><dir id='NeSFL'><q id='NeSFL'></q></dir></style></legend>
                        <i id='NeSFL'><tr id='NeSFL'><dt id='NeSFL'><q id='NeSFL'><span id='NeSFL'><b id='NeSFL'><form id='NeSFL'><ins id='NeSFL'></ins><ul id='NeSFL'></ul><sub id='NeSFL'></sub></form><legend id='NeSFL'></legend><bdo id='NeSFL'><pre id='NeSFL'><center id='NeSFL'></center></pre></bdo></b><th id='NeSFL'></th></span></q></dt></tr></i><div id='NeSFL'><tfoot id='NeSFL'></tfoot><dl id='NeSFL'><fieldset id='NeSFL'></fieldset></dl></div>