Fragment to Fragment与FragmentStatePagerAdapter的通信

时间:2013-11-21 05:04:46

标签: android android-fragments android-viewpager fragmentstatepageradapter

我正在尝试通过FragmentA中的Button更新FragmentB中TextView的文本。但每当我点击Button时,我的TextView都没有任何反应。可能是什么问题呢?这是我的代码:

Communicator.java:

public interface Communicator {
    public void respond(String data);
}

MainActivity.java:

public class MainActivity extends FragmentActivity implements Communicator{

    ViewPager viewPager = null;
    FragmentManager fragmentManager = getSupportFragmentManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.pager);   
        viewPager.setAdapter(new MyAdapter(fragmentManager));

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        FragmentB fragB = new FragmentB();

        fragmentTransaction.add(R.id.pager, fragB, "frag_tag");
        fragmentTransaction.commit();

    }

    public class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter (FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;

            if (i == 0)
            {
                fragment = new FragmentA();
            }
            if (i == 1)
            {
                fragment = new FragmentB();
            }
                    if (i == 2)
            {
                fragment = new FragmentC();
            }
            if (i == 3)
            {
                fragment = new FragmentD();
            }
                    if (i == 4)
            {
                fragment = new FragmentE();
            }
            if (i == 5)
            {
            fragment = new FragmentF();
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 6;
        }
    }

    @Override
    public void respond(String data) {
        // TODO Auto-generated method stub

         FragmentB f2 =  (FragmentB) fragmentManager.findFragmentByTag("frag_tag");
         f2.changeText(data);
    }

}

FragmentA:

public class FragmentA extends Fragment implements OnClickListener {

    int counter = 0;
    Button button1;
    Communicator comm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragmenta, container, false);              
    }

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

        comm = (Communicator) getActivity();
        button1 = (Button) getActivity().findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        counter++;
        comm.respond("The button was clicked" + counter + "times");
    }

}

FragmentB:

public class FragmentB extends Fragment {

    TextView text1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragmentb, container, false);              
    }

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

        text1 = (TextView) getActivity().findViewById(R.id.textView1);
    }

    public void changeText(String data) {
        // TODO Auto-generated method stub
        text1.setText(data);
    }

}

1 个答案:

答案 0 :(得分:0)

主机活动可以通过使用findFragmentById()捕获Fragment实例来将消息传递给片段,然后直接调用片段的公共方法。

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article

        ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);
        } else {
            // Otherwise, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }
}

有关详细信息,请点击此链接http://developer.android.com/training/basics/fragments/communicating.html#Deliver