如何在 viewpager 中的片段之间传递字符串

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

本文介绍了如何在 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 中的片段之间传递字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 viewpager 中的片段之间传递字符串

基础教程推荐