替换ViewPager的节片段

时间:2013-12-04 10:24:48

标签: android android-fragments android-tabhost android-tabs

我使用带有FragmentPagerAdapter的ViewPager进行主导航。现在在一个片段/选项卡中,我想要一个可以转换为新片段的按钮。我正在努力使用替换方法的第一个参数。我需要把哪个ID放在那里?我已经尝试过ViewGroup的视图ID,但这不起作用。

    public static class SectionFragment extends Fragment {
    public static final String ARG_SECTION_NUMBER = "section_number";

    public SectionFragment() {
    }

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

        int sectionNr = getArguments().getInt(ARG_SECTION_NUMBER);
        String url = "http://www.google.com";
        if (sectionNr == 2) {
            url = "http://www.yahoo.com";
        } else if (sectionNr == 3) {
            final View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
            final ViewGroup groupContainer = container;
            Button btn = (Button)rootView.findViewById(R.id.lowerLevelBtn);
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.addToBackStack(null);

                    DetailFragment fr = new DetailFragment();

                    ft.replace(<<<Which id do I need to place here?>>>, fr, "detailFragment");

                    ft.commit();
                }
            });

            return rootView;
        }

        WebView wv = new WebView(getActivity());
        wv.setWebViewClient(new WebViewClient());
        wv.loadUrl(url);

        return wv;
    }
}

1 个答案:

答案 0 :(得分:0)

private static final int CONTENT_VIEW_ID = 666;

FrameLayout frame = new FrameLayout(this);
    frame.setId(CONTENT_VIEW_ID);
    setContentView(frame, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    mainFragment = MainFragment.newInstance();
    fragmentTransaction.add(CONTENT_VIEW_ID, mainFragment).commit();