gpt4 book ai didi

android - 替换其容器中的 FragmentTabHost 时,选项卡小部件仍然存在

转载 作者:行者123 更新时间:2023-11-30 02:49:49 26 4
gpt4 key购买 nike

我有一个 MainActivity,我在其中将 ID 为 containerFrameLayout 的内容设置为 FragmentTabHost .在包含 ListView 的一个选项卡的内容中,我想用一个新的 Fragment 替换 container 的全部内容(即删除整个 FragmentTabHost),但选项卡小部件保留,仅替换选项卡内容。

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

enter image description here

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

NotificationFragment

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

}

最佳答案

NotificationListFragmentonCreateView() 中,替换

FragmentTransaction transaction = getFragmentManager().beginTransaction();

用这个:

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

或者这个:

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

试试这个。它应该有效。

关于android - 替换其容器中的 FragmentTabHost 时,选项卡小部件仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24387059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com