Szukaj…


Uwagi

Fragmenty są bardzo ważnymi elementami interfejsu użytkownika w aplikacjach na Androida. Zostały one wprowadzone po raz pierwszy w interfejsie API systemu Android 3.0 (Honeycomb).


Zrozumienie paradygmatu projektowania fragmentów

Fragmenty zostały wprowadzone przede wszystkim do obsługi modułowego i elastycznego interfejsu użytkownika na urządzeniach z dużym ekranem, takich jak tablety.

Fragmentami zarządza aktywność. Zwykle każdy fragment reprezentuje część ekranu. W działaniu może znajdować się więcej niż jeden fragment. Fragmenty można również nazwać poddziałaniami . Po dodaniu fragmentu jako części układu działania znajduje się on w grupie ViewGroup w hierarchii widoków działania, a fragment definiuje własny układ widoku.


KOŁO ŻYCIA

Podobnie jak działania, fragmenty mają również cykl życia. Fragment zostanie powiadomiony o kolejnych zdarzeniach.

  1. Dołącz do aktywności - onAttach (Activity)
  2. Utwórz fragment - onCreate (pakiet)
  3. Utwórz widok - onCreateView (LayoutInflater, ViewGroup, Bundle)
  4. Tworzenie aktywności - onActivityCreated (pakiet)
  5. Wyświetl stan przywrócony - onViewStateRestored (pakiet)
  6. Widoczny dla użytkownika - onStart ()
  7. początek interakcji użytkownika - onResume ()
  8. pauza interakcji użytkownika - onPause ()
  9. Niewidoczny dla użytkownika - onStop ()
  10. Po zniszczeniu widoku - onDestroyView ()
  11. Zniszcz fragment - onDestroy ()
  12. Odłącz się od działania - onDetach ()

Jako programista powinieneś zastąpić różne metody wywołania zwrotnego cyklu życia, zwykle implementujemy metody onCreate (), onCreateView () i onPause ().


Podklasy fragmentu

  1. DialogFragment - do wyświetlania swobodnego okna dialogowego
  2. ListFragment - Do wyświetlania listy elementów
  3. PreferenceFragment - Przydatny do tworzenia aktywności ustawień

Bibliografia

  1. https://developer.android.com/guide/components/fragments.html
  2. https://developer.android.com/reference/android/app/Fragment.html

Dodawanie fragmentów

Statyczne dodawanie fragmentu

Plik: Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" >  
  
    <fragment  
        android:id="@+id/fragment2"  
        android:name="com.example.fragmentexample.Fragment2"  
        android:layout_width="0px"  
        android:layout_height="match_parent"   
        android:layout_weight="1"  
        />  
  
    <fragment  
        android:id="@+id/fragment1"  
        android:name="com.example.fragmentexample.Fragment1"  
        android:layout_width="0px"  
        android:layout_height="match_parent"  
        android:layout_weight="1"  
         />  
  
</LinearLayout>  

Plik: fragment1.xml

<?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="match_parent"  
    android:orientation="vertical"  
    android:background="#00ff00"  
     >  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="fragment frist"  
        android:textAppearance="?android:attr/textAppearanceLarge" />  
  
</LinearLayout>  

Plik: fragment2.xml

<?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="match_parent"  
    android:orientation="vertical"  
    android:background="#0000ff"  
     >  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Second Fragment"  
        android:textAppearance="?android:attr/textAppearanceLarge" />  
  
</LinearLayout>

Plik: MainActivity.java

package com.example.fragmentexample;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
    }  
}  

Plik: Fragment1.java

package com.example.fragmentexample;  
  
import android.app.Fragment;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
  
public class Fragment1 extends Fragment {  
    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        return inflater.inflate(R.layout.fragment1,container, false);  
    }  
  
}  

Plik: Fragment2.java

package com.example.fragmentexample;  
  
import android.app.Fragment;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
  
public class Fragment2 extends Fragment {  
      
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        return inflater.inflate(R.layout.fragment2,container, false);  
    }  
  
}  

Dynamiczne dodawanie fragmentu

Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" >  
  
    <FrameLayout  
        android:id="@+id/container1"   
        android:layout_width="0px"  
        android:layout_height="match_parent"   
        android:layout_weight="1"  
        />  
  
    <FrameLayout  
        android:id="@+id/container2"   
        android:layout_width="0px"  
        android:layout_height="match_parent"  
        android:layout_weight="1"  
         />  
  
</LinearLayout>

FrameLayout działa jak kontener fragmentów.

Klasa MainActivity

Plik: MainActivity.java

package com.example.fragmentexample;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        loadFragment(this, R.id.container1,new Fragment1(),"fragment1");
        loadFragment(this, R.id.container2,new Fragment2(),"fragment2");    
    }  

    public static void loadFragment(Activity activity, int containerId, Fragment fragment, String tag)
    {
        activity.getSupportFragmentManager().beginTransaction().
            replace(containerId, fragment,tag).commitAllowingStateLoss();
    }
}  


Modified text is an extract of the original Stack Overflow Documentation
Licencjonowany na podstawie CC BY-SA 3.0
Nie związany z Stack Overflow