查看问题从FirstFragment

时间:2015-09-03 05:23:19

标签: android android-fragments

我正在使用Fragments创建一个简单的演示项目,我在按钮上点击SecondFragment来调用FirstFragment

我没有任何问题地调用SecondFragment,但我getting view of both the Fragments SecondFragment和FirstFragment

那我在做错了什么?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new FirstFragment()).commit();
        }
    }

    public static class FirstFragment extends Fragment {

        Button buttonCallSecondFragment;

        public FirstFragment() {
        }

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

            buttonCallSecondFragment = (Button) rootView.findViewById(R.id.button1);
            buttonCallSecondFragment.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    FragmentManager fm = getFragmentManager();
                    SecondFragment fragment = new SecondFragment();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.add(R.id.container, fragment);
                    ft.commit();
                }
            });

            return rootView;
        }
    }


    public static class SecondFragment extends Fragment {

        public SecondFragment() {
        }

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

7 个答案:

答案 0 :(得分:2)

您需要删除第一个片段,您可以使用replace或先致电remove然后add

来删除

为了能够按后退按钮将事务添加到后台堆栈,您可以通过调用片段管理器上的addToBackStack(tag)来实现。标签可能为空。

答案 1 :(得分:1)

您正在Android应用中添加已显示片段的片段。

 FragmentTransaction ft = fm.beginTransaction();
                    ft.add(R.id.container, fragment);
                    ft.commit();

当活动中已经加载了一个片段时,不要添加片段,而是替换片段。

所以要实现:

请在OnCreate()中添加您的代码,并将以下代码添加到您的点击监听器:

 FragmentManager fm = getFragmentManager();
                    SecondFragment fragment = new SecondFragment();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.container, fragment);
                    ft.addToBackStack(null);
                    ft.commit();

谢谢。!!

答案 2 :(得分:1)

如果你想替换片段,你应该调用replace代替add:

buttonCallSecondFragment = (Button) rootView.findViewById(R.id.button1);
                buttonCallSecondFragment.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        FragmentManager fm = getFragmentManager();
                        SecondFragment fragment = new SecondFragment();
                        FragmentTransaction ft = fm.beginTransaction();
                        ft.replace(R.id.container, fragment);
                        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                        ft.addToBackStack(null);
                        ft.commit();
                    }
                });

答案 3 :(得分:0)

您正在添加片段,以便它将位于另一个片段之上,为scecond片段的根视图提供背景颜色

答案 4 :(得分:0)

您也可以将其加载为嵌套片段

public void addFragB() {
        FragmentManager childFragMan = getChildFragmentManager();
        FragmentTransaction childFragTrans =    childFragMan.beginTransaction();
        SecondFragment 2ndfrag = new SecondFragment();
        childFragTrans.add(R.id.fragA_LinearLayout, 2ndfrag);
        //childFragTrans.addToBackStack("");
        childFragTrans.commit();
    }

答案 5 :(得分:0)

不,你不能像这样在片段之间进行通信。您需要通过容器活动在片段之间进行通信。

我建议你在活动中制作一个导航方法,并在那里切换/调用碎片。

以下是一小段代码:

在您的容器活动中:

Fragment fragment;
    public void navigateTo(Fragment newFragment,boolean addToBackStack) {

        this.fragment = newFragment;
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.fragment_container, newFragment);
        if(addToBackStack)
            ft.addToBackStack(fragment.getClass().getSimpleName());
        ft.commit();
    }

如果您要从Fragment1转到Fragment2,那么在Fragment1中执行以下操作:

((YourContainerActivity) getActivity()).navigateTo(new Fragment2(),false); //if you want to add to back stack make 2nd argument true.

答案 6 :(得分:0)

private void selectItem(int position) {
    selectedPosition = position;
    // update the main content by replacing fragments
    fragment = null;
    fragmentStack = new Stack<Fragment>();
     switch (position) {

         case 0:

             fragment = new  Fragment1();

             break;

         case 1:

             fragment = new  Fragment2();


             break;

    }
     //redirect the screen
     if (fragment != null) {
        fragmentManager = getSupportFragmentManager();
        redirectScreen(fragment);
        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(title[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
     }
}