从Host活动访问ViewPager片段将片段返回为NULL

时间:2014-08-04 10:39:10

标签: android android-viewpager

我尝试从主机活动访问ViewPager片段,但MyFragment1 myFragment1 = (MyFragment1)getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.new_pager+":0");行返回NULL片段,如何从视图寻呼机正确获取片段? 的 MainActivity:

    public class MainActivity extends FragmentActivity implements MyFragment1.OnMyFragment1Listener {
   @Override
   public void onCreate(){
      newPagerAdapter = new NewPagerAdapter(getSupportFragmentManager());

        // find ViewPager
        mViewpager = (ViewPager) findViewById(R.id.new_pager);
        mViewpager.setAdapter(newPagerAdapter);
   }

    @Override
    public void onSubmit() {
            MyFragment1 myFragment1 = (MyFragment1)getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.new_pager+":0");

    // Here myFragment1 returns always NULL
    View rootView = myFragment1.getView();

    }

MyFragment1:

public class MyFragment1 extends Fragment {

    private OnMyFragment1Listener mCallback;

    /**
     * Callback interface for login
     */
    public interface OnMyFragment1Listener {
        public void onSubmit(); 
    }

    /**
     * Called when this fragment on attached to the Host Activity
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface.If not, it throws an exception

        try {
            mCallback = (OnMyFragment1Listener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + "should implement onLoginListener interface");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallback = null;
    }

    public MyFragment1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_myFragment1, container, false);
        return rootView;
    }
}

更新

private class NewInspectionPagerAdapter extends FragmentStatePagerAdapter implements PagerSlidingTabStrip.IconTabProvider {
        SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

        private final int[] ICONS = {android.R.drawable.btn_star, android.R.drawable.btn_star,
                android.R.drawable.btn_star, android.R.drawable.btn_star};
        private Fragment resultFragment;

        public NewInspectionPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    resultFragment = new NewInspectionFragment1();
                    break;
                case 1:
                    resultFragment = new NewInspectionFragment2();
                    break;
                case 2:
                    resultFragment = new NewInspectionFragment3();
                    break;
                case 3:
                    resultFragment = new NewInspectionFragment4();
                    break;
            }

            return resultFragment;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return "OBJECT " + (position + 1);
        }


        @Override
        public int getPageIconResId(int i) {
            return ICONS[i];
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(container, position);
            registeredFragments.put(position, fragment);
            return fragment;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            registeredFragments.remove(position);
            super.destroyItem(container, position, object);
        }

        public Fragment getRegisteredFragment(int position) {
            return registeredFragments.get(position);
        }
    }

的onSubmit:

@Override
    public void onSubmit() {
       NewInspectionPagerAdapter adapter = (NewInspectionPagerAdapter)mViewpager.getAdapter();
       NewInspectionFragment1 newInspectionFragment1 = (NewInspectionFragment1)      adapter.getRegisteredFragment(0);
     // Still newInspectionFragment1 returns as NULL
    }

1 个答案:

答案 0 :(得分:0)

您可以按照HERE

所述实例化片段,将片段存储在 fieldvariable

只需将片段添加到instantiateItem

中的变量即可
@Override
public Object instantiateItem(ViewGroup container, int position) {
    Fragment fragment = (Fragment) super.instantiateItem(container, position);
    registeredFragments.put(position, fragment);
    return fragment;
}

并在destroyItem

中删除它们
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    registeredFragments.remove(position);
    super.destroyItem(container, position, object);
}