출처 : Bill Phillips, Android Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides) 3st Edition
■ The Need for UI Flexibility
- 리스트를 관리하는 부분 하나, 항목을 관리하는 부분 하나
- 항목을 클릭하는 것은 항목에 관련된 액티비티를 실행
- UI Flexibility: 액티비티의 뷰를 런타임 시점에서 디바이스에 관계없이 재구성하는것
■ Introducing Fragments
- Fragment : Controller Object. 액티비티가 UI 관리를 수행하도록 시킬수 있는 컨트롤러.
- UI Fragment : 각자 자신의 UI를 관리하는 Fragment. Fragment의 View는 UI elements포함
- Activity의 View에는 다수 Fragment의 View가 삽입될 지점이 있다.
- Activity는 Fragment로 분할된다.
- 액티비티의 뷰는 Fragment의 View인 FrameLayout으로 정의된다.
- 프래그먼트의 뷰는 Widget들로 구성된다.
■ Two types of fragments
- Fragment는 어떤 fragment를 implements하느냐에 따라 종류가 나뉜다.
- Native Fragment : 기기에서 지원하는 Fragment
- Support Fragment : 앱의 라이브러리에서 지원하는 Fragment
- 보통 Support Fragment를 많이 쓴다.
■ Adding dependencies in Android Studio
- AppCompat 라이브러리를 app/build.gradle에 추가
- build.gradle은 app전체에서 하나, app moudle에 하나 있다.
- dependency추가를 위해서는 app module에서 추가
1 2 3 4 5 6 7 8 9 | dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0-beta01' implementation 'com.android.support.constraint:constraint-layout:1.1.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.support:recyclerview-v7:28.0.0-beta01' } | cs |
- Dependency 추가한 다음에 Tools-> Android -> Sync Project with gradles files
■ Creating the Crime class
- UUID : 안드로이드 프레임워크에서 사용하는 자바 유틸리티. universally unique ID값 생성.
1 2 3 4 5 6 7 8 9 10 | public class Crime { private UUID mId; private String mTitle; private Date mDate; private boolean mSolved; public Crime() { mId = UUID.randomUUID(); mDate = new Date(); } } | cs |
■ The fragment lifecycle
- 프래그먼트는 액티비티를 대신해 작업하기 때문에 액티비티에 상응하는 프래그먼트의 라이프사이클이 필요하다.
- 프래그먼트 라이프사이클 메소드는 부모 액티비티에 의해 호출된다.
- 액티비티가 OS에 의해 호출되는것과는 다른 면이다.
- OS는 프래그먼트에 대해 아무런 정보도 알 수 없다.
■ Two approaches to hosting
- 액티비티에서 UI 프래그먼트를 호스팅하는 방법은 두가지가 있다.
1. 액티비티의 레이아웃에 프래그먼트 추가
2. 액티비티의 코드에서 프래그먼트 추가
- 유연성을 위해서는 코드에서 프래그먼트 추가하는 것이 좋다.
■ Defining a container view
- 프래그먼트를 사용하기 위한 액티비티의 레이아웃을 FrameLayout가 좋다.
- 프리뷰가 잘 안보이면 Build->Rebuild하거나 앱 껏다켜기
■ Defining CrimeFragment’s layout
- 프래그먼트는 LinearLayout으로 설정
■ Creating the CrimeFragment class
- Fragment는 extends Fragment를 통해 만들어진다.
- android.support.v4.app을 상속한다.
■ Implementing fragment lifecycle methods
- 뷰와 모델을 통해 개별 항목을 표시하고, 유저의 변경사항을 반영하는 역할을 한다.
- Fragment의 onCreate(Bundle)은 public인 반면 Activity의 onCreate(Bundle)은 protected이다. 그 이유는 어떠한 액티비티에서라도 프래그먼트가 호출될 수 있어야하기 때문이다.
- .onSaveInstanceState(Bundle)을 통해 상태를 저장하고 불러올 수 있다.
- onCreate(Bundle)에서는 프래그먼트 인스턴스를 설정할 수 있지만 프래그먼트의 레이아웃은 onCreateVIew에서 해야한다.
1 2 3 4 5 6 7 | @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, container, false); return v; } | cs |
- inflater를 통해 뷰를 불러오고, container는 프래그먼트 뷰의 부모를 의미한다, Bundle은 프래그먼트의 뷰를 부모의 뷰에 inflate할지 여부를 결정한다.
만약 코드로 자식의 뷰를 부모의 코드에 넣을 것이라면 false를 값으로 둔다.
■ Wiring widgets in a fragment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class CrimeFragment extends Fragment { private Crime mCrime; private EditText mTitleField; ... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_crime, container, false); mTitleField = (EditText) v.findViewById(R.id.crime_title); mTitleField.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged( CharSequence s, int start, int count, int after) { // This space intentionally left blank } @Override public void onTextChanged( CharSequence s, int start, int before, int count) { mCrime.setTitle(s.toString()); } @Override public void afterTextChanged(Editable s) { // This one too } }); return v; } } | cs |
- findViewById(int)를 통해 뷰를 찾아온다.
■ Adding a UI Fragment to the FragmentManager
- 프래그먼트를 관리하고 프래그먼트의 레이아웃을 액티비티의 뷰에 넣는 역할을 한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class CrimeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_crime); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragment_container); if (fragment == null) { fragment = new CrimeFragment(); fm.beginTransaction() .add(R.id.fragment_container, fragment) .commit(); } } } | cs |
'Web Programming > Android' 카테고리의 다른 글
5. Your Second Activity (0) | 2018.08.16 |
---|---|
3. The Activity Lifecycle (0) | 2018.08.14 |
2. Android and Model-ViewController (0) | 2018.08.14 |
1. Your First Android Application (0) | 2018.08.14 |