Fragment는 액티비티처럼 화면을 띄워주는 View의 종류중 하나인데,
Activity안에 포함시킬 수 있다는 치명적인 장점을 가졌기때문에 어플을 만들때는 아주 필수적인 요소이다.
프래그먼트의 생명주기 (Life Cycle)이다.
기본적으로 액티비티와 크게 다르지는 않아서 어렵진 않으나 참고하기엔 좋다.
굳이 외우는것보단 필요할때 찾아보면 되니 깊게 보진 말자.
매우 기초적인 주석은 빼놓았지만 조금이라도 안드로이드를 만져봤다면 이해하기 어렵지 않다.
서비스 자체는 초급난이도정도지만 초보자들에게는 생소하기때문에 어려울 수 있다.
코드를 그대로 옮기기만 하면 문제없이 잘 동작할것이다.
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bino.messanger.MainActivity"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="400dp">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_weight="1"
android:id="@+id/fragment_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="onClick"/>
<Button
android:layout_weight="1"
android:id="@+id/fragment_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="onClick"/>
</LinearLayout>
</LinearLayout>
- MainActivity.java
package bino.messanger;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private final int FRAGMENT1 = 1;
private final int FRAGMENT2 = 2;
private Button Fragment_Button_1, Fragment_Button_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment_Button_1 = (Button) findViewById(R.id.fragment_button_1);
Fragment_Button_2 = (Button) findViewById(R.id.fragment_button_2);
callFragment(FRAGMENT1);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.fragment_button_1:
callFragment(FRAGMENT1);
break;
case R.id.fragment_button_2:
callFragment(FRAGMENT2);
break;
}
}
private void callFragment(int flagment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// 프래그먼트를 사용하기위해 호출하였다.
switch (flagment) {
case 1:
// fragment1
One_Fragment_Activity fragment1 = new One_Fragment_Activity();
transaction.replace(R.id.fragment_container, fragment1);
transaction.commit();
break;
case 2:
// fragment2
Two_Fragment_Activity fragment2 = new Two_Fragment_Activity();
transaction.replace(R.id.fragment_container, fragment2);
transaction.commit();
break;
}
}
}
- One_Fragment_Activity.java
package bino.messanger;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Bino on 2017-09-06.
*/
public class One_Fragment_Activity extends Fragment {
public One_Fragment_Activity() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.one_fragment, container, false);
}
}
- Two_Fragment_Activity.java
package bino.messanger;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Bino on 2017-09-06.
*/
public class Two_Fragment_Activity extends Fragment {
public Two_Fragment_Activity() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.two_fragment, container, false);
}
}
- one_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"/>
</LinearLayout>
- two_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"/>
</LinearLayout>