Android 无法正确交换actionbar选项卡上的片段

Android 无法正确交换actionbar选项卡上的片段,android,tabs,android-fragments,Android,Tabs,Android Fragments,我已经阅读了关于这个主题的所有问题/答案,但我无法让它们中的任何一个为我工作。aggghh 我有3个操作栏标签 tab1调用/使用动态显示“添加”按钮的片段 当按钮被点击时,我希望它删除自己的“按钮片段”,并用一个包含大量编辑文本的片段替换它 这正如预期的那样有效。但是,当我单击另一个选项卡时,例如tab2,它调用自己的片段,tab1中的EditText片段保留并覆盖tab2片段 我想我明白,我试图做的是根本错误的,一个片段不能调用另一个片段来替换自己?或者,不知何故,我没有正确地删除以前的片段

我已经阅读了关于这个主题的所有问题/答案,但我无法让它们中的任何一个为我工作。aggghh

我有3个操作栏标签

tab1调用/使用动态显示“添加”按钮的片段

当按钮被点击时,我希望它删除自己的“按钮片段”,并用一个包含大量编辑文本的片段替换它

这正如预期的那样有效。但是,当我单击另一个选项卡时,例如tab2,它调用自己的片段,tab1中的EditText片段保留并覆盖tab2片段

我想我明白,我试图做的是根本错误的,一个片段不能调用另一个片段来替换自己?或者,不知何故,我没有正确地删除以前的片段和一些与backbackback有关的东西。。????或者显示和隐藏xml

我对这个很陌生,完全糊涂了!请帮忙……提前谢谢:)

下面是我的代码,大部分来自android站点,使用TabCompat.zip示例

MainActivity.java

package com.example.android.tabcompat;
public class MainActivity extends TabCompatActivity {

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

TabHelper tabHelper = getTabHelper();

CompatTab tab1Tab = tabHelper.newTab("tab1")
.setText(R.string.tab_tab1)
.setTabListener(new InstantiatingTabListener(this,"A", Tab1Fragment.class));
tabHelper.addTab(tab1Tab);

        CompatTab tab2Tab = tabHelper.newTab("tab2")
            .setText(R.string.tab_tab2)
            .setTabListener(new InstantiatingTabListener(this, "B", Tab2Fragment.class));
        tabHelper.addTab(tab2Tab);

        CompatTab tab3Tab = tabHelper.newTab("tab3")
                .setText(R.string.tab_tab3)
                .setTabListener(new InstantiatingTabListener(this,"C", Tab3Fragment.class));
        tabHelper.addTab(tab3Tab);
    }

    /**
     * Implementation of {@link CompatTabListener} to handle tab change events. This implementation
     * instantiates the specified fragment class with no arguments when its tab is selected.
     */
    public static class InstantiatingTabListener implements CompatTabListener {

        private final TabCompatActivity mActivity;
        private final Class mClass;


        /**
         * Constructor used each time a new tab is created.
         * @param activity The host Activity, used to instantiate the fragment
         * @param cls      The class representing the fragment to instantiate
         */
        public InstantiatingTabListener(TabCompatActivity activity, String tag, Class<? extends Fragment> cls) {
            mActivity = activity;
            mClass = cls;

        }

        /* The following are each of the ActionBar.TabListener Callback */
        @Override
        public void onTabSelected(CompatTab tab, FragmentTransaction ft) {
            // Check if the fragment is already initialised
            Fragment fragment = tab.getFragment();
            if (fragment == null) {
                // If not, instantiate and add it to the activity
                fragment = Fragment.instantiate(mActivity, mClass.getName());
                tab.setFragment(fragment);
                ft.add(android.R.id.tabcontent, fragment, tab.getTag());
            } 
            else {
                // If it exists, simply attach it in order to show it
                ft.attach(fragment);
            }
        }


        public void onTabUnselected(CompatTab tab, FragmentTransaction ft) {
            Fragment fragment = tab.getFragment();
            if (fragment != null) {  
            ft.detach(fragment);
            }
        }

        @Override
        public void onTabReselected(CompatTab tab, FragmentTransaction ft) {
            // User selected the already selected tab. Do nothing.

    } }
      }
Tab1FormFragment.java此片段显示一种形式的edittext视图,它从布局文件form.xml中获取

package com.example.android.tabcompat;



   public class Tab1FormFragment extends Fragment {
    protected static final String ARG_POSITION = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      View myFragmentView = inflater.inflate(R.layout.form, container, false);

      return myFragmentView;
     }

     }
form.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/petForm"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
<ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_tab_photos" />

        <EditText
            android:id="@+id/editTextName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/field_name"
            android:inputType="text" />


        <EditText
            android:id="@+id/editTextAge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/field_age"
            android:inputType="text" />
        <EditText
            android:id="@+id/editTextBreed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/field_breed"
            android:inputType="text" />

        <Spinner
            android:id="@+id/spinnerSex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:entries="@array/sex_arrays"
            android:hint="@string/field_sex" />

        <Button
            android:id="@+id/savePet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save_record" />
    </LinearLayout>`

`

您可以尝试调用非操作栏选项卡(您所说的有一堆EditText),同时为片段实例提供一个标记,即在动态按钮的onClick方法中,在启动Tab1formFragment后添加此行:

getFragmentManager().beginTransaction().replace(android.R.id.tabcontent,formFragment,“FORM_FRAG”).addToBackStack(null.commit()

通过添加对formFragment实例的检查来更改操作栏选项卡事务:
if(getFragmentManager().findFragmentByTag(“FORM_FRAG”)!=null)
getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentByTag(“表单碎片”)).commit()


此外,您还可以在onTabSelected()方法中使用replace()片段事务,而不是attach()和add()。

好的,所以我们已经做了更多的阅读,并看到我的两个片段都需要是动态的。因此,我们将form.xml替换为动态创建的表单,而不是使用layoutparams。我还有一个重叠的问题我有一个建议。。您可以使用ViewFlipper将按钮片段和表单片段逻辑组合在同一个片段中。ViewFlipper的第一个子项是您的按钮,当用户按下按钮时,您可以使用ViewFlipper的方法“setDisplayedChild”来显示表单片段。这样,您就不必替换片段,而且仍然可以获得相同的预期效果?它在主活动的布局上吗?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/petForm"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
<ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_tab_photos" />

        <EditText
            android:id="@+id/editTextName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/field_name"
            android:inputType="text" />


        <EditText
            android:id="@+id/editTextAge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/field_age"
            android:inputType="text" />
        <EditText
            android:id="@+id/editTextBreed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/field_breed"
            android:inputType="text" />

        <Spinner
            android:id="@+id/spinnerSex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:entries="@array/sex_arrays"
            android:hint="@string/field_sex" />

        <Button
            android:id="@+id/savePet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save_record" />
    </LinearLayout>`