2015-09-16 1 views
0

Vous comprendrez mon problème si vous le faites pas à pas comme indiqué ci-dessous (désolé, mais je ne peux pas vous faire comprendre d'une autre manière). Cela pourrait être déroutant. Mais, je suis sûr que si vous le lisez une fois, vous comprendrez.Custom LinearLayout avec EditText inside Fragment

  1. Créez un nouveau projet android, puis créez deux fragments. Vous allez maintenant avoir

    a. MainActivity.java.

    b. FirstFragment (FirstFragment.java) avec son fichier xml (first_fragment.xml).

    c. SecondFragment (SecondFragment.java) avec son fichier xml (second_fragment.xml).

Maintenant, créez une classe LinearLayout personnalisée (FormRow.java) comme ci-dessous:

public class FormRowNew extends LinearLayout { 
    public FormRowNew(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initView(); 
    } 

    private void initView() { 
     View.inflate(getContext(), R.layout.form_row, this); 
    } 
} 

fichier XML (form_row) dans c'est comme:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  
     xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="eeeee"/> 
</LinearLayout> 

Utilisez cette classe linéaire personnalisée DEUX FOIS dans FirstFragment comme ci-dessous:

<gallery.com.yyyyyyyyy.FormRow 
    android:id="@+id/first" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/txvFirst" 
    > 
</gallery.com.yyyyyyyyy.FormRow> 

<gallery.com.yyyyyyyyy.FormRow 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/first" 
    > 
</gallery.com.yyyyyyyyy.FormRow> 

N'OUBLIEZ PAS: Je l'ai utilisé deux fois. Vous l'utilisez aussi au moins deux fois.

Maintenant, chargez FirstFragment dans MainActivity.java à un id (rlRootLayout) comme ci-dessous.

FirstFragment firstFragment = new FirstFragment(); 
getSupportFragmentManager().beginTransaction().addToBackStack(null)                  
.replace(R.id.rlRootLayout, firstFragment).commit(); 

De même, la charge deuxième fragment à même ID (rlRootLayout) de FirstFragment comme ci-dessous.

secondFragment blankFragment = new secondFragment(); 
getActivity().getSupportFragmentManager().beginTransaction() 
.addToBackS tack(null).replace(R.id.rlRootLayout, blankFragment) 
.commit(); 

Ici j'ajoute SecondFragment à MainActivity de FirstFragment à ID (rlRootLayout).

Problème: - Lancez l'application et aller à la page comme mentionné:

MainActivity -> 
FirstFragment -> 
Change something in BOTTOM FormRow(like add some text in EditText) -> 
SecondFragment -> 
FirstFragment. 

Maintenant, voir les classes personnalisées (FormRow) à l'intérieur FirstFragment. Tout ce que nous avions ajouté dans la classe inférieure de FormRow, ajouté automatiquement dans Top FormRow. Je ne pouvais pas trouver la raison pour laquelle cela se produisait. Même si je ne fais rien avec FormRow haut, ça change selon le bas quand je reviens de SecondFragment. S'il vous plaît aider.

Répondre

1

Enregistrer le texte dans editText lors de l'ajout du second fragment. Ensuite, réglez le texte sauvegardé tout en revenant à la première à partir de la seconde @Override public void onViewStateRestored(@Nullable Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); //set edit text } est appelé.

+0

thnx d'Edittexts .. got it. –

+0

celui-ci m'aide @Arjun –

0

Ce comportement est dû à l'ID du EditText. Lorsque le premier fragment gonfle la mise en page tout en revenant du second fragment, il affecte les valeurs pour éditer le texte en utilisant son identifiant. Donc, dans ce cas, les deux EditText a le même id donc il attribue la même valeur pour les deux

+0

Merci pour votre aide. –