hanno letto ogni domanda/risposta su questo argomento, ma non riesco a nessuno di loro di lavorare per me.aggghhImpossibile scambiare correttamente i frammenti in schede ActionBar
ho 3 schede ActionBar
chiamate TAB1/usa un frammento che visualizza dinamicamente un pulsante "aggiungi"
quando il pulsante viene premuto lo voglio per rimuovere se 'il pulsante frammento' e sostituirlo con un frammento che ha un sacco di EditTexts
questo funziona come previsto. Tuttavia quando clicco su un'altra scheda, ad esempio tab2 che chiama il proprio frammento, il frammento EditTexts da tab1 rimane e si sovrappone al frammento tab2
Penso di capire che ciò che sto cercando di fare è fondamentalmente sbagliato che un frammento non possa chiamare un altro frammento sostituire se stesso? o che in qualche modo non sto rimuovendo correttamente i frammenti precedenti e qualcosa a che fare con il backstack .. ???? o mostrare e nascondere il xml ?????
Sono molto nuovo e completamente confuso! Si prega di aiuto ... grazie in anticipo :)
Ecco il mio codice più preso dal sito di Android http://developer.android.com/training/backward-compatible-ui/index.html utilizzando il campione 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 questo è dove viene caricato il pulsante "aggiungi"
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 questo frammento visualizzare una forma di punti di vista EditText cui prende dal file di layout 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>`
ok, quindi ho letto un po 'di più, http://stackoverflow.com/questions/12114150/action-bar-tabs-having-two-fragments-one-being-dynamic-in-one-tab e vedi che entrambi i miei frammenti devono essere dinamici. quindi ho sostituito form.xml con un modulo creato dinamicamente usando invece layoutparams. ho ancora il problema di sovrapposizione ... sospiro – Wenz
Ho un suggerimento .. Probabilmente potresti combinare il tuo frammento di pulsante e la logica del frammento di modulo nello stesso frammento usando un ViewFlipper.Il primo figlio di ViewFlipper sarà il tuo pulsante, quando un utente preme il pulsante è possibile utilizzare il metodo ViewFlipper "setDisplayedChild" per mostrare il frammento del modulo. In questo modo eviti di dover sostituire il frammento e ottieni comunque lo stesso effetto desiderato. –
Dove vive R.id.tabcontent? È sul layout per MainActivity? – japino