5

我有两个片段(Fragment1 和 Fragment2),使用自定义 TabListener 类创建了 ActionBar 导航。每个选项卡的内容都是我的片段之一。但现在我有一个问题 - 我无法访问/获取片段的实例。

public void onCreate( Bundle savedInstanceState )
    {   
        super.onCreate( savedInstanceState );
        getSupportActionBar().setNavigationMode( getSupportActionBar().NAVIGATION_MODE_TABS );

        Tab tab = getSupportActionBar().newTab()
            .setText( R.string.frag1 )
            .setTabListener( 
                new MyTabListener( this, 
                    Fragment1.class.getName() ) );
        getSupportActionBar().addTab( tab );

        tab = getSupportActionBar().newTab()
            .setText( R.string.frag2 )
            .setTabListener( 
                new MyTabListener( this, 
                    Fragment2.class.getName() ) );
        getSupportActionBar().addTab( tab );

        Fragment1 f1 = (Fragment1) getSupportFragmentManager().findFragmentByTag("FRAG1");
        if(f1 != null) {
            Toast.makeText(this, "It's not empty", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(this, "Photo Intent Goes Here", Toast.LENGTH_SHORT).show();

        if(data != null) {
            Toast.makeText(this, "It's not empty", Toast.LENGTH_SHORT).show();

             if (resultCode == RESULT_OK) {  


                }  

        } else {
            Toast.makeText(this, "It's empty", Toast.LENGTH_SHORT).show();
        }
    }

    private class MyTabListener  implements TabListener
    {
        private SherlockFragment mFragment;
        private final Activity mActivity;
        private final String mFragName;

        public MyTabListener( Activity activity, 
            String fragName )
        {
            mActivity = activity;
            mFragName = fragName;
        }


        @Override
        public void onTabSelected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            mFragment = (SherlockFragment) SherlockFragment.instantiate( mActivity, 
                    mFragName );

            if( mFragment instanceof Fragment1 ) {
                Fragment1 f1temp = (Fragment1) mFragment;

                //Toast.makeText(mActivity, "Frag1", Toast.LENGTH_SHORT).show();

                  ft.add( android.R.id.content, mFragment,"FRAG1");
            }

            else {
                  ft.add( android.R.id.content, mFragment,"ELSE");
            }


            //Toast.makeText(mActivity, mFragName, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onTabUnselected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            ft.remove( mFragment );
            mFragment = null;

        }

        public SherlockFragment getCurrentFragment() {
            return mFragment;
        }

        @Override
        public void onTabReselected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

    }

我不能使用 getSupportFragmentManager().findFragmentById( id ),因为我也没有 id 我也尝试过getSupportFragmentManager().findFragmentByTag( tag ),但它也不起作用,因为我只调用add()方法FragmentTransaction类而不是replace()

有什么方法可以获取当前选项卡片段实例,以便我可以调用片段的方法?

4

2 回答 2

5

我找到了解决方案。我试图在onActivityResult()方法中获取片段实例,并且确实有效。

 public void onActivityResult(int requestCode, int resultCode, Intent data) {

Toast.makeText(this, "Photo Intent Goes Here", Toast.LENGTH_SHORT).show();

        if(data != null) {
            Toast.makeText(this, "It's not empty", Toast.LENGTH_SHORT).show();

             if (resultCode == RESULT_OK) {  
                   Fragment1 f1 = (Fragment1) getSupportFragmentManager().findFragmentByTag("FRAG1");
                  f1.onCameraResult(requestCode,resultCode,data); // Your method   of the fragment

                }  

        } else {
            Toast.makeText(this, "It's empty", Toast.LENGTH_SHORT).show();
        }
    }
于 2013-01-16T16:52:13.837 回答
0

我从FragmentPagerAdapter实例中获取片段:

FragmentPagerAdapter adapter = (FragmentPagerAdapter) viewPager.getAdapter();
Fragment fragment = adapter.getItem(tabLayout.getSelectedTabPosition());
于 2016-02-26T12:24:50.130 回答