2011-12-08 9 views
7

Come implementare mostrare e nascondere il frammento all'interno del frammento in Android? Ho aggiunto due frammenti all'interno dell'attività. Un frammento contenente un menu e un frammento contiene un sottomenu. Ho molto pulsante nel frammento di menu come casa, idea, ecc. Se clicco sul pulsante idea. Devo mostrare il sottomenu. Se faccio nuovamente clic sul pulsante idea, devo nascondere il sottomenu. Qualcuno può fornire un esempio o come accedere a un frammento di vista in un altro frammento?Come implementare mostrare e nascondere il frammento all'interno del frammento in Android

questo è il mio layout principale

?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<fragment class="com.gcm.fragment.CommonFragment" 
      android:id="@+id/the_frag" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" /> 
<fragment class="com.gcm.fragment.SubFragment" 
      android:id="@+id/the_frag1" 
      android:layout_marginTop="130dip" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" />    


</LinearLayout> 

Nel mio frammento

package com.gcm.fragment; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.TextView; 

public class CommonFragment extends Fragment implements OnClickListener { 
    TextView txtIhaveIdea=null; 
    boolean menuVisible=false; 
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { 
     ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.collapsed_menu2, container, false); 

     txtIhaveIdea=(TextView)layout.findViewById(R.id.txtIhaveAnIdea); 
     txtIhaveIdea.setOnClickListener(this); 

     return layout; 
     } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if(!menuVisible) 
     { 
     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 
     fm.beginTransaction(); 
     Fragment fragOne = new SubFragment(); 
     ft.show(fragOne); 
     } 
     else 
     { 
      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction ft = fm.beginTransaction(); 

      fm.beginTransaction(); 
      Fragment fragOne = new SubFragment(); 
      ft.hide(fragOne); 
     } 

    } 



} 

Grazie

+0

fare riferimento a codice di riferimento per i frammenti Mostra/Nascondi data sul sito Android http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentHideShow.html – potter

+0

@kumar hai raggiunto il tuo compito? – AndroidOptimist

risposta

2

Semplicemente, creare un metodo pubblico nella vostra attività "padre". che nasconde il frammento.

Quindi dall'interno del frammento nell'evento clic ottieni il "genitore |" attività, lanciare e poi chiamare il metodo si è creato.

((ParentActitity)getActivity()).hideFragment(); 
0

È necessario utilizzare un'interfaccia per comunicare con la vostra attività genitore.

Date un'occhiata sul tutorial di Vogella, "3.4. comunicazione Applicazione con frammenti". Ecco l'link

2

Si potrebbe provare a ottenere framelayout o un frammento da id e cambiare la sua visibilità

View frag = findViewById(R.id.my_fragment); 
frag.setVisibility(View.VISIBLE); 
5

Considerando la questione è più di 2K .. una risposta può ancora aiutare i nuovi lettori in modo qui va:

  • non si vuole veramente avere FragmentManager e FragmentTransactions accadendo frammenti all'interno di non avere né calchi potenziali riferimenti nocivi alla vostra attività (s)

Quindi quello che faccio e funziona bene è impostata un'interfaccia al frammento e dare un metodo, diciamo needsHide():

public class MyFrag extends Fragment { 

public interface MyFragInterface { 

    public void needsHide(); 
} 

quindi implementare sulla vostra attività:

public class MainActivity extends FragmentActivity implements MyFrag.MyFragInterface { 
public void needsHide() { 

FragmentManager fragmentManager = getSupportFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    //find the fragment by View or Tag 
    MyFrag myFrag = (MyFrag)fragmentManager.findFragmentByTag(SOME_TAG); 
    fragmentTransaction.hide(myFrag); 
    fragmentTransaction.commit(); 
     //do more if you must 
}} 

L'unica parte che richiede il pensiero è quando chiamare needHide(), ciò che si potrebbe fare in onViewCreated di Fragment, poiché si è certi che non è troppo presto per il MainActivity per il commit delle transazioni. Se lo si inserisce onCreate() potrebbe non funzionare a seconda di ciò che si fa con i frammenti oter:

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 

    // Making sure Main activity implemented interface 
    try { 
     if (USE_A_CONDITION) { 
      ((MyFragInterface)this.getActivity()).needsHide(); 
     } 
    } catch (ClassCastException e) { 
     throw new ClassCastException("Calling activity must implement MyFragInterface"); 
    } 
    super.onViewCreated(view, savedInstanceState); 
} 
0

metodo hide(): nasconde un frammento esistente. Questo è rilevante solo per i frammenti le cui viste sono state aggiunte a un contenitore, in quanto ciò causerebbe la visualizzazione della vista.

il tuo codice:

@Override 
public void onClick(View v) { 
    if(!menuVisible) 
    { 
    FragmentManager fm = getFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    fm.beginTransaction(); 
    Fragment fragOne = new SubFragment(); 
    ft.show(fragOne); 
    } 
    else 
    { 
     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 

     fm.beginTransaction(); 
     // it's wrong , you just hide the fragment that not added to FragmentTransaction 
     Fragment fragOne = new SubFragment(); 
     ft.hide(fragOne); 
    } 

} 
-3

Di seguito il codice ha funzionato per me ..

View frag = findViewById(R.id.fragment); 
frag.setVisibility(View.GONE);//Or View.INVISBLE 
Problemi correlati