[Ch3-8] Fragment 두번째! 가로형, 세로형 화면에 따라 동적으로 반응하는 프래그먼트 만들기 (Using dynamic Fragments depending on landscape/portrait mode)
지난번에 이어서 두 번째 프래그먼트 시간이다.
지난번 아주 유익한 시간을 갖었으니, 혹여나 첫 번째를 못 본 병아리는 지난번(Ch3-7) 꺼를 우선 보기 바란다.

지난번에는 activity_main.xml 에 프래그먼트 두개를 고정하여서 만들었다. 이번에는 동적으로 반응하여 프래그먼트를 보여주는 시간을 갖도록 하자!
지난번 생성했던 프로젝트를 이어서 사용할 예정이니, 참고바란다. (https://mckal-story.tistory.com/29)
자 오늘의 목표를 우선 확인해보자!


오늘 만들것의 순서는 우선 간단하다.
1. activity_main.xml 주석처리 (comment)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fragment_dynamic.MainActivity">
<!-- 주석처리 시작
<fragment
android:name="com.example.fragment_dynamic.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<fragment
android:name="com.example.fragment_dynamic.Fragment2"
android:id="@+id/fragment2"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
주석처리 끝 -->
</LinearLayout>
1.1 우선 정적으로 프래그먼트를 배치하는 것이 아니라, 동적으로/기능적으로 프래그먼트를 보여주는 것임으로, activity_main.xml 에 지난번에 배열한 fragment 1과 fragment 2를 주석 처리한다.
이제 기능만 넣으면 된다.
2. MainActivity.java의 화면 형태를 읽어 프래그먼트를 쏴주는 기능 프로그래밍
package com.example.fragment_dynamic;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getSupportFragmentManager(); // fragmentManager 인스턴스 생성
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // fragmentTransaction 인스턴스 생성
// --- 현재 디스플레이 정보 가져오기 --
DisplayMetrics display = this.getResources().getDisplayMetrics(); // display 인스턴스 생성
int width = display.widthPixels;
int height = display.heightPixels;
if (width > height) // 가로가 세로보다 길면 (가로형 화면; landscape mode)
{
// -- 가로형 화면 --
Fragment1 fragment1 = new Fragment1();
// android.R.id.content 를 화면에 따라 배치해보자.
fragmentTransaction.replace(android.R.id.content, fragment1); // fragment1 과 화면 연결
}
else // 그 이외에는 세로형 모드 (portrait mode)
{
// --세로형 모드
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(android.R.id.content, fragment2); // fragment2 와 화면 연결
}
fragmentTransaction.commit();
setContentView(R.layout.activity_main);
}
}
2.1 우선 프래그먼트를 배치하기 전, 현재 디바이스의 화면이 가로형(landscape)인지, 세로형(portrait)인지부터 확인한다.
- display 인스턴스로 현재 화면의 폭(width)과 높이(height)를 대조하여 화면을 찾는다.
2.2 가로형이면 android.R.id.content를 fragment1과 연결한다.
- fragmentTransaction.replace를 사용하여, 기존의 activity를 화면으로 사용하지 않고 fragment를 화면으로 사용하겠다고 선언한다.
- 세로형도 마찬가지로 해준다.
--------
오늘은 정말 간단하다. 이렇게 하면 가로, 세로로 디바이스를 돌릴 때마다, 각기 다른(내가 사전에 지정한) 프래그먼트를 보여줄 거다.
?????????????????????????????????????????????????????????????????????????????
혹시나 가상 디바이스 (AVD)를 사용하는데, 화면을 잘 나오는 것 같으나, 제대로 동작하지 않는 병아리가 있나??

그러면 저기쯤에 화면 회전 모드가 있을 텐데, 저거를 활성화 한 뒤, 다시 시도해보자. (내 거는 왜 아이콘이 안 나오는지 모르겠다 ㅡㅡ )