替换其容器中的FragmentTabHost时,Tab小部件仍然存在

时间:2014-06-24 12:47:04

标签: android android-fragments fragment-tab-host fragmenttransaction fragmentmanager

我有一个MainActivity,其中我将FrameLayout的内容设为container FragmentTabHost ListView。在包含container的一个标签的内容中,我想用新的Fragment替换FragmentTabHost的整个内容(即删除整个FragmentTabHost),但是标签小部件仍然只有标签内容被替换。

为什么public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); Fragment notificationTabsFragment = new NotificationTabsFragment(); fragmentTransaction.replace(R.id.container, notificationTabsFragment, "notificationTabsFragment"); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } } 没有被完全取代?我该如何删除它?

enter image description here

MainActivity.java

<?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" >

    <fragment
        android:id="@+id/header"
        android:name="com.xxx.fragment.HeaderFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

main.xml中

public class NotificationTabsFragment extends Fragment {

    private FragmentTabHost mTabHost;

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

        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.container);       

        mTabHost.addTab(
                mTabHost.newTabSpec("all").setIndicator(getString(R.string.all), null),
                NotificationListFragment.class, null);

        /* adding more tab */ 

        return mTabHost;
    }
}

NotificationTabsFragment

public class NotificationListFragment extends Fragment {

    private ListView listview;
    private NotificationAdapter adapter;

    private List<Notification> notifList;

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

        notifList = MyApplication.dbHelper.getNotificationList();

        View view = inflater.inflate(R.layout.notification_list, container, false);

        listview = (ListView) view.findViewById(R.id.listview);
        listview.setEmptyView(view.findViewById(R.id.empty));

        adapter = new NotificationAdapter(getActivity(), notifList);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position,
                    long arg3) 
            {
                Bundle args = new Bundle();
                args.putLong(Notification.KEY_OCC_ID, notifList.get(position).occ_id);

                Fragment fragment = new NotificationFragment();
                fragment.setArguments(args);
                FragmentTransaction transaction = getFragmentManager().beginTransaction();

                transaction.replace(R.id.container, fragment); // replacing the whole container, but the tab widgets remain!
                transaction.addToBackStack(null);

                transaction.commit();
            }
        });

        return view;
    }
}

NotificationListFragment.java

public class NotificationFragment extends Fragment {

    private TextView text;

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

        View view = inflater.inflate(R.layout.notification_detail, container, false);
        text = (TextView) view.findViewById(R.id.dude);

        text.setText("NotificationFragment content");

        return view;
    }

}

NotificationFragment

{{1}}

1 个答案:

答案 0 :(得分:1)

onCreateView()的{​​{1}}中,替换

NotificationListFragment

用这两个:

FragmentTransaction transaction = getFragmentManager().beginTransaction();

或者这个:

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

试试这个。它应该工作。