android-fragments 튜토리얼
android-fragments 시작하기
수색…
비고
단편은 안드로이드 앱의 사용자 인터페이스에서 매우 중요한 구성 요소입니다. Android 3.0 (Honeycomb) API에서 처음 소개되었습니다.
조각의 디자인 패러다임 이해하기
단편은 태블릿과 같은 대형 화면 장치에서 모듈 식의 유연한 UI를 주로 지원하기 위해 도입되었습니다.
조각은 활동에 의해 관리됩니다. 일반적으로 각 조각은 화면의 일부를 나타냅니다. 하나의 활동에 둘 이상의 단편이있을 수 있습니다. 단편은 하위 활동 이라고 할 수도 있습니다. 조각을 액티비티 레이아웃의 일부로 추가하면 액티비티의 뷰 계층 구조 내에있는 ViewGroup에 있으며 프래그먼트는 자체 뷰 레이아웃을 정의합니다.
수명
활동과 마찬가지로 단편에도 수명주기가 있습니다. 프래그먼트는 다음 이벤트에 대해 알림을받습니다.
- 활동에 연결 - onAttach (활동)
- 조각 만들기 - onCreate (번들)
- 뷰 생성 - onCreateView (LayoutInflater, ViewGroup, Bundle)
- 활동 생성 - onActivityCreated (번들)
- 뷰 상태 복원 됨 - onViewStateRestored (Bundle)
- 사용자에게 표시됨 - onStart ()
- 사용자 상호 작용 시작 - onResume ()
- 사용자 상호 작용 일시 중지 - onPause ()
- 사용자가 볼 수 없도록 설정 - onStop ()
- 보기 파괴 - onDestroyView ()
- 조각 파기 - onDestroy ()
- 액티비티와의 분리 - onDetach ()
프로그래머는 다양한 라이프 사이클 콜백 메소드를 오버라이드해야합니다. 일반적으로 onCreate (), onCreateView () 및 onPause () 메소드를 구현합니다.
프래그먼트의 서브 클래스
- DialogFragment - 부동 대화 상자를 표시합니다.
- ListFragment - 항목 목록을 표시합니다.
- PreferenceFragment - 설정 작업을 만드는 데 유용합니다.
참고 문헌
파편 추가하기
조각을 정적으로 추가하기
파일 : 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>
파일 : 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>
파일 : 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>
파일 : 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);
}
}
파일 : 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);
}
}
파일 : 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);
}
}
조각을 동적으로 추가하기
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은 조각 컨테이너 역할을합니다.
MainActivity 클래스
파일 : 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();
}
}