11
votes

Onglet Barre d'action Android avec ScrollView Fabriqué en double vue après le changement d'orientation

J'ai un code très simple où j'utilise une barre d'action avec des fragments d'onglets. Cela fonctionne bien après la charge, mais après le changement d'orientation, il devient fou. Le vieux fragment aussi visible (pourquoi?).

Désolé pour les textes hongrois sur l'image, mais j'espère que cela n'a pas d'importance. après l'orientation change p>

i joignez le code, peut-être qu'il aide à résoudre ce problème.

Activité principale: P>

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/newword"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/newwordtext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/wordhint" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/description"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/descriptionwordtext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/descriptionhint"
            android:inputType="textMultiLine"
            android:minLines="4" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/origin"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/origintext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/originhint" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/source"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/sourcetext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/sourcehint" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/name"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/nametext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName" />

        <Button
            android:id="@+id/sendbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@string/send" />
    </LinearLayout>

</ScrollView>


0 commentaires

5 Réponses :


2
votes

Essayez d'utiliser ft.replace (R.Id.Content, MFRAGMENT) à la place de FT.ATTACH (MFRAGEMENT); dans la fonction OntabSelected


2 commentaires

J'ai essayé avec ceci: ft.replace (android.r.id.content, mfragment); Mais cela n'aide pas (fait le même problème) et après avoir sélectionné à nouveau l'onglet, il supprimait le contenu complet de l'onglet.


Heh ... ce code a fonctionné pour moi: si (fragmentanager.findfragmentybyid (r.id.frame_for_fragment) == null) {fragmenttransaction fragmenttransaction = fragmentmanager .begintransaction (); Fragment = Nouveau nombre de fragments (); fragmenttransaction.add (r.id.frame_for_fragment, fragment); fragmenttransaction.commit (); }



0
votes
public void onTabSelected(Tab tab, FragmentTransaction ft)
    {
        // Check if the fragment is already initialized
        if (mFragment == null)
        {
            if(ft.findFragmentById(android.R.id.content) == null){
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
            }
        } else
        {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }

    }
Something like that, because your problem is that your adding the same fragment twice, we just have to find where...

0 commentaires

5
votes

J'ai trouvé une réponse utilisable dans d'autres Question .

Je dois modifier mon tablisener (je l'ai déplacé dans ma classe d'activité principale en classe intérieure): xxx

donc avant d'ajouter fragment (encore), je vérifie son existence ( et obtenir sa référence) et si cela existe, je l'attache que.


2 commentaires

La seule chose que j'ajouterais, c'est que cette ligne "mfragment = mactivity.getfragmentManager (). FINDFRAGMENTBYTAG (MTAG);" Devrait également être ajouté à la méthode ontabunselected avant le MFRAGMENT! = NULL VÉRIEUR. Sinon, a fonctionné super!


mfragment = mactivity.getfragmentmanager (). FINDFRAGMENTBYTAG (MTAG); Résolu mon problème aussi, la classe n'a pas besoin d'être intérieure et privée.



1
votes

J'ai trouvé une solution très simple pour éviter les fragments de duplication: xxx

La clé de la solution est dans la condition: xxx

Cette condition n'est valable que si les classes des fragments sont différentes. Si vous avez des instances différentes de la même classe, vous devez mettre un attribut supplémentaire dans vos fragments pour reconnaître sa fonction (ou l'association avec et pour faire la condition! Currentfragment.getclass (). Equals (mfragment.getclass ()). : Par exemple, vous pouvez utiliser la fonction de balise de fragmenttransaction.

bye, Alex.


0 commentaires

0
votes

J'ai résolu ceci en recherchant simplement le fragment dans le constructeur de l'écoute des onglets.

public class TabListener<T extends Fragment> implements ActionBar.TabListener 
{
    private Fragment fragment;
    private final SherlockFragmentActivity activity;
    private final String tag;
    private final Class<T> clazz;

    public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clazz) 
    {
        this.activity = activity;
        this.tag = tag;
        this.clazz = clazz;

        FragmentManager manager = ((SherlockFragmentActivity) activity).getSupportFragmentManager();
        fragment = manager.findFragmentByTag(tag);
    }
...
}


0 commentaires