从其父级访问内部片段方法 class

Acces inner fragment method from its parent class

是否可以访问内部 class 方法?

public class MenuFragment extends Fragment {

    RelativeLayout fullLayout;

    ImageView imgSettings;

    public static ImageView imgTopZigzag, imgBottomZigzag;

    MyPagerAdapter myPagerAdapter;

    ProgressBar levelProgressBar;
    TextView txtLevel, txtXp;

    public static int screenWidth, screenHeight;

    public static ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.menu_fragment, null);

        mViewPager = (ViewPager) rootView.findViewById(R.id.view_pager);

        myPagerAdapter = new MyPagerAdapter(getChildFragmentManager());

        mViewPager.setAdapter(myPagerAdapter);

        fullLayout = (RelativeLayout) rootView.findViewById(R.id.rl_menu);

        imgSettings = (ImageView) rootView.findViewById(R.id.img_settings);
        imgSettings.bringToFront();
        imgSettings.getLayoutParams().height = 100;
        imgSettings.getLayoutParams().width = imgSettings.getLayoutParams().height;

        imgTopZigzag = (ImageView) rootView.findViewById(R.id.img_top_zigzag);
        imgBottomZigzag = (ImageView) rootView.findViewById(R.id.img_bottom_zigzag);

        levelProgressBar = (ProgressBar) rootView.findViewById(R.id.progressLevel);
        txtLevel = (TextView) rootView.findViewById(R.id.txt_level);
        txtXp = (TextView) rootView.findViewById(R.id.txt_xp);
        txtLevel.setTypeface(((MainActivity) getActivity()).typeface);
        txtXp.setTypeface(((MainActivity) getActivity()).typeface);

        Display display = getActivity().getWindowManager().getDefaultDisplay();
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();

        final Animation showMenu = new AlphaAnimation(0, 1);
        showMenu.setDuration(600);
        showMenu.setFillAfter(true);

        final TranslateAnimation centerUpBack = new TranslateAnimation(0, 0, -screenHeight/3, -screenHeight/3 + screenHeight/10);
        centerUpBack.setDuration(400);
        centerUpBack.setFillAfter(true);

        TranslateAnimation centerUp = new TranslateAnimation(0, 0, 0, -screenHeight/3);
        centerUp.setDuration(1000);
        centerUp.setFillAfter(true);
        centerUp.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation arg0) {
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                imgTopZigzag.startAnimation(centerUpBack);
                mViewPager.setVisibility(View.VISIBLE);
                mViewPager.startAnimation(showMenu);

            }
        });

        final TranslateAnimation centerDownBack = new TranslateAnimation(0, 0, screenHeight/3, screenHeight/3 - screenHeight/10);
        centerDownBack.setDuration(400);
        centerDownBack.setFillAfter(true);

        final TranslateAnimation centerDown = new TranslateAnimation(0, 0, 0, screenHeight/3);
        centerDown.setDuration(1000);
        centerDown.setFillAfter(true);
        centerDown.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation arg0) {
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                imgBottomZigzag.startAnimation(centerDownBack);
            }
        });

        imgTopZigzag.startAnimation(centerUp);
        imgBottomZigzag.startAnimation(centerDown);

        updateLevelBar();

        mViewPager.getLayoutParams().width = screenWidth;
        mViewPager.getLayoutParams().height = mViewPager.getLayoutParams().width*17/25;

        return rootView;
    }

    public void updateLevelBar() {
        levelProgressBar.setMax(((MainActivity) getActivity()).level * 150);
        levelProgressBar.setProgress(((MainActivity) getActivity()).xp);
        txtLevel.setText("Level " + ((MainActivity) getActivity()).level);
        txtXp.setText(((MainActivity) getActivity()).xp + " / " + ((MainActivity) getActivity()).level * 150 + " xp");
    }

    public static class FragmentMenu extends Fragment {
        LinearLayout innerMenuLayout;

        TextView classicHighscore, zenHighscore;

        ImageButton btnPlayLast, btnLeaderboard;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.menu_tab, container, false);

            innerMenuLayout = (LinearLayout) rootView.findViewById(R.id.linearLayout);
            innerMenuLayout.getLayoutParams().width = screenWidth*17/25;

            btnPlayLast = (ImageButton) rootView.findViewById(R.id.btn_play_last);
            btnLeaderboard = (ImageButton) rootView.findViewById(R.id.btn_leaderboard);

            classicHighscore = (TextView) rootView.findViewById(R.id.txt_classic_highscore);
            zenHighscore = (TextView) rootView.findViewById(R.id.txt_zen_highscore);
            classicHighscore.setTypeface(((MainActivity) getActivity()).typeface);
            zenHighscore.setTypeface(((MainActivity) getActivity()).typeface);

            final TranslateAnimation openTopLeaderboard = new TranslateAnimation(0, 0, - screenHeight/3 + screenHeight/10, -screenHeight/2 - 50);
            openTopLeaderboard.setDuration(800);
            openTopLeaderboard.setFillAfter(true);

            final TranslateAnimation openBottomLeaderboard = new TranslateAnimation(0, 0, screenHeight/3 - screenHeight/10, screenHeight/2 + 50);
            openBottomLeaderboard.setDuration(800);
            openBottomLeaderboard.setFillAfter(true);

            final Animation leaderboard = new AlphaAnimation(1, 0);
            leaderboard.setDuration(800);
            leaderboard.setFillAfter(true);
            leaderboard.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    imgTopZigzag.startAnimation(openTopLeaderboard);
                    imgBottomZigzag.startAnimation(openBottomLeaderboard);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    ((MainActivity) getActivity()).startLeaderboard();
                    MenuFragment.FragmentMenuPlay fragment = (MenuFragment.FragmentMenuPlay) getChildFragmentManager().findFragmentById(R.id.fragmentView);
                    fragment.buttonsSetClickable();
                }
            });

            btnLeaderboard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.startAnimation(leaderboard);
                    MenuFragment.FragmentMenuPlay fragment = (MenuFragment.FragmentMenuPlay) getChildFragmentManager().findFragmentById(R.id.fragmentView);
                    fragment.buttonsSetUnclickable();
                }
            });

            updateHighscores();

            return rootView;
        }

        public void updateHighscores() {
            SharedPreferences prefs = getContext().getSharedPreferences("com.thematus.twinz", Context.MODE_PRIVATE);
            int classic_highscore = prefs.getInt("classic_highscore", 0);
            int zen_highscore = prefs.getInt("zen_highscore", 0);
            classicHighscore.setText(classic_highscore + "");
            zenHighscore.setText(zen_highscore + "");
        }
    }

    public static class FragmentMenuPlay extends Fragment {
        LinearLayout innerMenuLayout;

        ImageButton btnClassicMode, btnZenMode, btnLocalMulti;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.menu_play_tab, container, false);

            innerMenuLayout = (LinearLayout) rootView.findViewById(R.id.linearLayout);
            innerMenuLayout.getLayoutParams().width = screenWidth*17/25;

            btnClassicMode = (ImageButton) rootView.findViewById(R.id.btn_classic_mode);
            btnZenMode = (ImageButton) rootView.findViewById(R.id.btn_zen_mode);
            btnLocalMulti = (ImageButton) rootView.findViewById(R.id.btn_local_multi);

            final TranslateAnimation closeMenuTop = new TranslateAnimation(0, 0, - screenHeight/3 + screenHeight/10, 0);
            closeMenuTop.setDuration(300);
            closeMenuTop.setFillAfter(true);

            final TranslateAnimation closeMenuBottom = new TranslateAnimation(0, 0, screenHeight/3 - screenHeight/10, 0);
            closeMenuBottom.setDuration(300);
            closeMenuBottom.setFillAfter(true);

            final Animation hideMenuClassic = new ScaleAnimation(1f, 0.2f, 1f, 0.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
            hideMenuClassic.setDuration(300);
            hideMenuClassic.setFillAfter(true);
            hideMenuClassic.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    imgTopZigzag.startAnimation(closeMenuTop);
                    imgBottomZigzag.startAnimation(closeMenuBottom);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    ((MainActivity) getActivity()).startClassicMode();
                    buttonsSetClickable();
                }
            });

            btnClassicMode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.startAnimation(hideMenuClassic);
                    buttonsSetUnclickable();

                }
            });

            final Animation hideMenuZen = new ScaleAnimation(1f, 0.2f, 1f, 0.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
            hideMenuZen.setDuration(300);
            hideMenuZen.setFillAfter(true);
            hideMenuZen.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    imgTopZigzag.startAnimation(closeMenuTop);
                    imgBottomZigzag.startAnimation(closeMenuBottom);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    ((MainActivity) getActivity()).startZenMode();
                    buttonsSetClickable();
                }
            });

            btnZenMode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.startAnimation(hideMenuZen);
                    buttonsSetUnclickable();
                }
            });

            final Animation hideMenuLocalMulti = new ScaleAnimation(1f, 0.2f, 1f, 0.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
            hideMenuLocalMulti.setDuration(300);
            hideMenuLocalMulti.setFillAfter(true);
            hideMenuLocalMulti.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    imgTopZigzag.startAnimation(closeMenuTop);
                    imgBottomZigzag.startAnimation(closeMenuBottom);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    ((MainActivity) getActivity()).startLocalMulti();
                    buttonsSetClickable();
                }
            });

            btnLocalMulti.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.startAnimation(hideMenuLocalMulti);
                    buttonsSetUnclickable();
                }
            });

            return rootView;
        }

        public void buttonsSetClickable() {
            btnClassicMode.setClickable(true);
            btnZenMode.setClickable(true);
            btnLocalMulti.setClickable(true);
        }

        public void buttonsSetUnclickable() {
            btnClassicMode.setClickable(false);
            btnZenMode.setClickable(false);
            btnLocalMulti.setClickable(false);
        }
    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fragmentManager){
            super(fragmentManager);

        }
        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            switch(position){
                case 0:
                    FragmentMenu fm = new FragmentMenu();
                    return fm;
                case 1:
                    FragmentMenuPlay fmp = new FragmentMenuPlay();
                    return fmp;
            }
            return null;
        }

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

有什么办法吗?我试着用

来调用它
MenuFragment.FragmentMenu fragment = (MenuFragment.FragmentMenu) getChildFragmentManager()
                                     .findFragmentById(R.id.viewPager);
                                      fragment.method();

但是这个 returns 空指针异常。

问题也可能是我正在使用带有内部 classes(片段)的视图寻呼机。

感谢您的回复!

我通过在适配器 public 中制作片段来修复它,所以我在顶部定义了它,这样我就能够制作一个片段无效,它从每个内部 class 运行无效, 比如:

    public void doSomething() {
     definedFragment.void();
    }

而在第二个片段的内部,只是:

Fragment fragment = (Fragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.fragmentView);
fragment.doSomething();