1

我有一个我将带有 id的 aMainActivity的内容设置为 a 。在包含 a 的一个选项卡的内容中,我想用一个新的替换整个内容(即删除整个),但选项卡小部件仍然存在,并且只有选项卡内容被替换。FrameLayoutcontainerFragmentTabHostListViewcontainerFragmentFragmentTabHost

为什么FragmentTabHost没有完全替换?我怎样才能删除它?

在此处输入图像描述

MainActivity.java

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();
    }
}

主要的.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" >

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

NotificationTabsFragment

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;
    }
}

NotificationListFragment.java

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;
    }
}

通知片段

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;
    }

}
4

1 回答 1

1

onCreateView()你的NotificationListFragment,替换

FragmentTransaction transaction = getFragmentManager().beginTransaction();

用这个:

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

或这个:

FragmentTransaction transaction = getParentFragment().getFragmentManager().beginTransaction();

尝试这个。它应该工作。

于 2014-06-24T12:56:41.957 回答