2012-10-19 42 views
5

讀了有關這個話題的每一個問題/答案,但我不能讓任何人來爲me.aggghh無法正確碎片掉在動作條標籤

工作,我有3個動作條標籤

TAB1電話/使用了動態顯示的「添加」按鈕

當按鈕被點擊我想它本身刪除「按鈕片段」,並使用具有一堆EditTexts

該作品的片段替換片段如預期。然而,當我點擊另一個選項卡,例如tab2調用它自己的片段,tab1中的EditTexts片段仍然存在,並覆蓋tab2片段

我想明白我試圖做的根本錯誤是一個片段不能調用另一個片段取代自己?或者不知何故,我不能正確刪除以前的碎片和與後臺堆棧有關的東西。或顯示和隱藏的XML ?????

我對此很新,完全困惑!請幫助...謝謝提前:)

這裏是我的代碼大部分從Android網站http://developer.android.com/training/backward-compatible-ui/index.html使用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. 

    } } 
     } 

Tab1Fragment .java這是「添加按鈕」加載的地方

package com.example.android.tabcompat; 

     public class Tab1Fragment extends Fragment{ 

     protected int position; 
     //display the add tab1 button 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
      final Button btn = new Button (getActivity()); 
      btn.setGravity(Gravity.TOP); 
      btn.setText(R.string.btn_tab1); 
      btn.setId(1); 
      btn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
    //when button is clicked replace the button and display the tab1 form fragment 


    FragmentManager fragmentManager = getFragmentManager(); 
    Tab1FormFragment formFragment = new Tab1FormFragment(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.replace(android.R.id.tabcontent, formFragment); 
    transaction.addToBackStack(null); 
    transaction.commit(); 
       } 

      }); 
      return btn; 

     } 
    } 

Tab1FormFragment.java這個片段顯示它從佈局文件form.xml得到的EditText視圖的形式

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>` 
+0

好的,所以做了更多的閱讀,http://stackoverflow.com/questions/12114150/action-bar-tabs-having-two-fragments-one-being-dynamic-in-one-tab,看看我的碎片都需要動態。所以用一個動態創建的表單代替了使用layoutparams的form.xml。我仍然有重疊的問題壽....嘆息 – Wenz

+1

我有一個建議..你可能可以結合你的按鈕片段和表單片段邏輯通過使用ViewFlipper相同的片段。ViewFlipper的第一個孩子將成爲你的按鈕,當用戶按下按鈕時,你可以使用ViewFlipper的方法「setDisplayedChild」來顯示你的表單片段。這樣你可以避免更換片段,並且仍然可以獲得相同的效果。 –

+0

R.id.tabcontent在哪裏住?它在MainActivity的佈局上嗎? – japino

回答

0

你可以嘗試調用非動作-bar選項卡(你說的有一堆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("FORM_FRAG")).commit()

此外,您可以使用您的onTabSelected替換()片段的交易()方法,而不是附加()並添加()。