2011-12-08 21 views
7

Jak zaimplementować ukrywanie fragmentu wewnątrz fragmentu w systemie Android? Dodałem dwa fragmenty wewnątrz aktywności. Jeden fragment zawierający menu i jeden fragment zawiera podmenu. Mam dużo przycisku w fragmencie menu, jak dom, pomysł itp. Jeśli kliknę przycisk pomysłu. Muszę pokazać podmenu. Jeśli ponownie kliknę przycisk pomysłu, muszę ukryć podmenu. Czy ktokolwiek może podać przykład lub jak uzyskać dostęp do jednego fragmentu widoku w innym fragmencie?Jak zaimplementować pokaż i ukryj fragment wewnątrz fragmentu w Androidzie

to jest mój główny układ

?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> 

In My fragmentu

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); 
     } 

    } 



} 

Dzięki

+0

Patrz kodem referencyjnym dla fragmentów hide/show podane na stronie http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentHideShow.html android – potter

+0

@kumar wykonałeś zadanie ur? – AndroidOptimist

Odpowiedz

2

Wystarczy utworzyć metodę publiczną w swojej "działalności" macierzystej. który ukrywa fragment.

Następnie z fragmentu wydarzenia kliknięcia otrzymasz "rodzic" aktywność, rzucić go, a następnie wywołać metodę cię stworzył.

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

Musisz użyć interfejsu do komunikacji z działalności macierzystej.

Spójrz na samouczku Vogella za „3.4. komunikacji aplikacji z fragmentami”. Oto link

2

można spróbować dostać framelayout lub fragment przez ID i zmienić jego widoczność

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

Zważywszy na to pytanie ma ponad 2K .. odpowiedzią może jeszcze pomóc nowych czytelników tak tu idzie:

  • Tak naprawdę nie chcą mieć FragmentManager i FragmentTransactions dzieje wewnątrz fragmenty nie mają odlewnictwa ani potencjalnie szkodliwych odnośników do swojej działalności (ów)

Więc co robię i działa dobrze ustawiony jest interfejs do fragmentu i dać sposób, powiedzmy needsHide():

public class MyFrag extends Fragment { 

public interface MyFragInterface { 

    public void needsHide(); 
} 

Następnie wdrożyć go na działalność:

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 
}} 

Jedyną częścią, która wymaga przemyślenia, jest to, kiedy wywołać needHide(), to możesz zrobić w swoim fragmencie onViewCreated, ponieważ masz pewność, że nie jest zbyt wcześnie, aby Twoja MainActivity mogła dokonywać transakcji. Jeśli umieścić go onCreate() może nie działać w zależności od tego, co zrobić z oter fragmentów:

@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

metoda hide(): Ukrywa istniejący fragment. Dotyczy to tylko fragmentów, których widoki zostały dodane do kontenera, ponieważ spowoduje to ukrycie widoku.

kod:

@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

poniższy kod pracował dla mnie ..

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