我怎样才能显示一个选项卡中的新片段?选项卡、片段

2023-09-04 10:28:06 作者:你只是我的奴り

我创建三个选项卡包含一个片段每个现在我想用相同的选项卡中的一个新片段替换第一个选项卡的片段。我怎样才能做到这一点与保持不变的标签?

I am creating a three tabs that contains one fragment each now I want to replace the first Tab's Fragment with a new Fragment within the same Tab. How can I do that with the tab remaining the same?

我的code -

Tab_Widget.java

    public class Tab_Widget extends TabActivity {

            TabHost tabHost; 

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tab_host);

            tabHost = getTabHost();

            tabHost.addTab(tabHost.newTabSpec("Tab1")
                .setIndicator("Tab1")
                .setContent(new Intent().setClass(this, main_activity.class)));

            tabHost.addTab(tabHost.newTabSpec("Tab2")
                .setIndicator("Tab2")
                .setContent(new Intent().setClass(this, second_activity.class)));

            tabHost.addTab(tabHost.newTabSpec("Tab3")
                    .setIndicator("Tab3")
                    .setContent(new Intent().setClass(this, third_activity.class)));

            tabHost.setCurrentTab(0);
        }
     }

main_activity.java

public class main_activity extends FragmentActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainfragment);
    }
}

main_fragment.java

public class main_fragment extends Fragment
{
    LinearLayout mLayout;
    Button mButton;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        mLayout = (LinearLayout) inflater.inflate(R.layout.main_view, null);
        mButton = (Button) mLayout.findViewById(R.id.btn);
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Fragment mFragment = new third_fragment_view();
                FragmentTransaction ft = getFragmentManager().beginTransaction();
//              ft.replace(R.id.Maincontainer, mFragment);
                ft.replace(R.id.main_fragment, mFragment);
                ft.addToBackStack(null);
                ft.commit();
            }
        });
        return mLayout;
    }
}

我的XML

mainfragment.xml

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

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Maincontainer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
  android:name="com.fragment.main_fragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/main_fragment">
</fragment>
</LinearLayout>

通过,这个片段是获取调用毫无疑问的,但它的重叠previous片段。

By, this the Fragment is getting called no doubt, but its overlapping the previous Fragment.

推荐答案

您有一个结构,像这样:

You have a structure like so:

TabActivity
 -> Tab1 = FragmentActivity = MainFragment
 -> Tab2          
 -> Tab3          

MainFragment 里面你正在试图取代它的容器(本身)的内容。

From inside of MainFragment you are trying to replace the contents of its container (itself).

这将是最好的,如果在 MainFragment 称为定制TabActivity这又称为替换它的容器,或者像这样(可能会落在同一问题的犯规因为它基本上运行在同一个地方同一个code,但它应该给你一个什么我建议一个基本的想法):

It would be best if the MainFragment called a customised TabActivity which in turn called a replace on it's container, either like so (Which may likely fall foul of the same problem as it's basically running the same code from the same place, but it should give you a basic idea of what I'm suggesting):

    Fragment newFragment = new FragmentToChangeTo();
    CustomTabActivity tabActivity = (CustomTabActivity) getActivity();
    tabActivity.changeFragment(newFragment);

如果这不起作用尝试做类似的事情,但不是调用活动通过意图和放大器直接命令传递给它;含有什么要更改的容器信息操作(或包)来。

If this doesn't work try doing a similar thing but instead of calling the activity directly pass commands to it through Intents & Actions (or Bundles) containing information on what to change which container to.

我会preferred说/为什么/片段不与片段式片段和相关的复杂工作得很好:检查here看看为什么我建议切段出片段的控制。

I would have preferred to say /why/ Fragments don't work well with Fragment-in-fragment and related intricacies: check here to see why I suggest cutting fragments out of the control of fragments.