Android: FragmentTabHost: Navigating Fragment Over

2020-07-25 02:24发布

问题:

I have got some sample FragmentTabHost project. And I made some modifications which are required for my project. my tab bar xml (bottom_tabs.xml)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

oncreate() - FragmentActivity

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        Bundle b = new Bundle();
        b.putString("key", "Simple");
        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                Fragment1.class, b);
        //
        b = new Bundle();
        b.putString("key", "Contacts");
        mTabHost.addTab(mTabHost.newTabSpec("contacts")
                .setIndicator("Contacts"), Fragment2.class, b);
        b = new Bundle();
        b.putString("key", "Custom");
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                Fragment3.class, b);

Fragment3 class

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

        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_3,
                null);  
        Button b = (Button) root.findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                 Fragment f;
                    f = (Fragment) new Fragment4();
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.replace(R.id.realtabcontent, f);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);
                   ft.commit();
            }
        });
        return root;
    }

The Problems: **

  • 1) If I press goto1 button in Fragment3 , It redirects to Fragment4. No Issues

  • 2) Then I press the tabs, the layout of the Fragment4 is overlapped in all the screens

  • 3) If I press goto 2nd tab button, How to redirect to Fragment2 and same time 2nd tab should be highlighted.

**

Please provide me the best way to do this.

回答1:

To change tabs your should use the FragmentTabHost.setCurrentTab() method instead of using FragmentTransaction yourself (it is handled internally). The reason your Fragment4 is appearing overlapped is because it is never removed when you change tabs.

This is what is happening:

  1. You navigate to tab 3.
  2. You press the goto fragment4 button which causes your code to replace fragment3 with fragment4
  3. You press another tab indicator. This causes the FragmentTabHost to remove the current fragment (which it still thinks is Fragment3) and add the new Fragment. The Fragment4, which you added manually, is still there. To avoid this you could add a listener for tab change events and explicitly remove Fragment4 if it is there.. Or even better you could add Fragment4 as a child fragment of Fragment3. That way when you leave Tab3 it will go away, and it will appear again when you return to Tab3. It just depends on what you are trying to accomplish.