※ 프레그먼트란?
- 프레그먼트는 일종의 뷰이다. 하지만 액티비티 처럼 생명주기가 있다. 즉 생명주기가 있는 뷰이다.
- 아래 이미지에서 왼쪽 프레그먼트와 오른쪽 프레그먼트를 나눠놨다. 버튼을 누를때마다 각기 다른 프레그먼트가
생성되었다가 종료된다.(생명주기가 액티비티와 같다.)
- 프레그먼트는 잘못 사용하면 정보전달이 잘 안될 수 있으므로 모바일화면보단 테블릿 화면에 사용ㅎ
- 프레그먼트를 구현하는 방법
- 레이아웃 파일(xml) 이용 (화면이 변하지 않을 때 이용)
- 소스파일(.java) 이용 (화면이 계속 변할 때 이용)
사진출처 : 서울위즈 앱 창업자를 위한 안드로이드 실무 동영상
※ 프레그먼트 .java파일을 이용한 구현
MainActivity
public class MainActivity extends Activity {
Button btn1,btn2,btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.bt_01);
btn2 = (Button) findViewById(R.id.bt_02);
btn3 = (Button) findViewById(R.id.bt_03);
btn1.setOnClickListener(onclick);
btn2.setOnClickListener(onclick);
btn3.setOnClickListener(onclick);
addFirstFragMent();
}
OnClickListener onclick = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bt_01 :
addFirstFragMent();
break;
case R.id.bt_02 :
addSecondFragMent();
break;
case R.id.bt_03 :
addThirdFragMent();
break;
}
}
};
//.java 파일을이용한 프레그먼트 구현 부분
// 액티비티에 프레그먼트를 붙이는 부분
private void addFirstFragMent(){
//프레그먼트 객체 생성
FirstFragMent firstFragment = new FirstFragMent(MainActivity.this);
//프레그먼트 매니저 생성
FragmentManager fragmentManager = getFragmentManager();
//프레그먼트 트랜잭션 객체 생성
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//프레그먼트 replace();
fragmentTransaction.replace(R.id.ll_right, firstFragment);
//프레그먼트 commit();
fragmentTransaction.commit();
}
private void addSecondFragMent(){
SecondFragMent secondFragment = new SecondFragMent(MainActivity.this);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ll_right, secondFragment);
fragmentTransaction.commit();
}
private void addThirdFragMent(){
ThirdFragMent thirdFragemnt = new ThirdFragMent(MainActivity.this);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ll_right, thirdFragemnt);
fragmentTransaction.commit();
}
//프레그먼트 클래스 제작
public static class FirstFragMent extends Fragment{
private Context context;
public FirstFragMent(Context context){
this.context = context;
}
//onCreateView 구현
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag_first, container, false);
Button btn = (Button) view.findViewById(R.id.bt_frag_first);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_frag_first :
Toast.makeText(context, "FirstFragmentBtn", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
return view;
}
}
public static class SecondFragMent extends Fragment{
private Context context;
public SecondFragMent(Context context){
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag_second, container, false);
Button btn = (Button) view.findViewById(R.id.bt_frag_second);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_frag_second :
Toast.makeText(context, "SecondFragmentBtn", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
return view;
}
}
public static class ThirdFragMent extends Fragment{
private Context context;
public ThirdFragMent(Context context){
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag_third, container, false);
Button btn = (Button) view.findViewById(R.id.bt_frag_third);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_frag_third :
Toast.makeText(context, "ThridFragmentBtn", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
return view;
}
}
//레이아웃 xml을 이용한 프레그먼트 구현
publicstaticclass LeftFragMent extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag_left, container, false);
return view;
}
}
}
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.androidex.MainActivity" >
<LinearLayout
android:orientation="vertical"
android:id="@+id/ll_left"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#00ff00">
//레이아웃 xml을 이용한 프레그먼트 구현
<fragment
class="com.example.androidex.MainActivity$LeftFragMent"
android:id="@+id/fm_left"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="@+id/ll_right"
android:layout_width="0px"
android:layout_weight="3"
android:layout_height="match_parent"
android:background="#00ffff">
</LinearLayout>
</LinearLayout>
frag_left
<?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" >
<Button
android:id="@+id/bt_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Btn1"/>
<Button
android:id="@+id/bt_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Btn2"/>
<Button
android:id="@+id/bt_03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Btn3"/>
</LinearLayout>
frag_first
//프레그먼트 xml 레이아웃 제작
<?xmlversion="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" >
<TextView
android:id="@+id/tv_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:background="#ff0000"
android:textColor="#ffffff"
android:textStyle="bold|italic"
android:text="Right Fragment"
/>
<Button
android:id="@+id/bt_frag_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="FirstFragBtn"
/>
</LinearLayout>
frag_second
<?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" >
<TextView
android:id="@+id/tv_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:background="#ff0000"
android:textColor="#ffffff"
android:textStyle="bold|italic"
android:text="Right Fragment"
/>
<Button
android:id="@+id/bt_frag_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SecondFragBtn"
/>
</LinearLayout>
frag_third
<?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" >
<TextView
android:id="@+id/tv_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:background="#ff0000"
android:textColor="#ffffff"
android:textStyle="bold|italic"
android:text="Right Fragment"
/>
<Button
android:id="@+id/bt_frag_third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ThirdFragBtn"
/>
'플밍 is 뭔들 > 안드로이드_인강' 카테고리의 다른 글
11-1. 팝업대화상자 (0) | 2016.11.27 |
---|---|
10-1. 액션바 & 옵션메뉴 (0) | 2016.11.27 |
08-1. 인텐트 (0) | 2016.11.27 |
07-2. 액티비티 생명주기 테스트 및 로그 활용법 (0) | 2016.11.27 |
07-1. 액티비티 (0) | 2016.11.27 |