class MainActivity
package com.example.choi.mystudy17;
import android.app.*;
import android.content.*;
import android.os.*;
import android.support.v7.app.*;
import android.util.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
Button bt_01, bt_02, bt_03;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_01=(Button)findViewById(R.id.bt_01);
bt_02=(Button)findViewById(R.id.bt_02);
bt_03=(Button)findViewById(R.id.bt_03);
bt_01.setOnClickListener(onClickListener);
bt_02.setOnClickListener(onClickListener);
bt_03.setOnClickListener(onClickListener);
addFirstFragMent();
}
private void addFirstFragMent() {
FirstFragMent firstFragment = new FirstFragMent(MainActivity.this);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ll_right, firstFragment);
fragmentTransaction.commit();
}
private void addSecondFragMent() {
SecondFragMent secondFragment = new SecondFragMent();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ll_right, secondFragment);
fragmentTransaction.commit();
}
private void addThirdFragMent() {
ThirdFragMent thirdFragment = new ThirdFragMent();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ll_right, thirdFragment);
fragmentTransaction.commit();
}
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_01:
addFirstFragMent();
break;
case R.id.bt_02:
addSecondFragMent();
break;
case R.id.bt_03:
addThirdFragMent();
break;
}
}
};
public static class LeftFragMent extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_left, container, false);
return view;
}
}
public static class FirstFragMent extends Fragment {
private Context mContext;
public FirstFragMent(Context c) {
mContext = c;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frg_first, container, false);
Button btn = (Button) view.findViewById(R.id.bt_frag_first);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt_frag_first:
Toast.makeText(mContext, "FirstFragMentBtn", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
return view;
}
}
public static class SecondFragMent extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frg_second, container, false);
return view;
}
}
public static class ThirdFragMent extends Fragment {
static final String TAG = "ThirdFragMent";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreateView()");
View view = inflater.inflate(R.layout.frg_third, container, false);
return view;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
Log.i(TAG, "onStart()");
super.onStart();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
Log.i(TAG, "onResume()");
super.onResume();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
Log.i(TAG, "onPause()");
super.onPause();
}
@Override
public void onStop() {
// TODO Auto-generated method stub
Log.i(TAG, "onStop()");
super.onStop();
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
Log.i(TAG, "onDestroyView()");
super.onDestroyView();
}
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.choi.mystudy17.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="@android:color/holo_green_light" >
<fragment
class="com.example.choi.mystudy17.MainActivity$LeftFragMent"
android:id="@+id/fm_left"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="@+id/ll_right"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="@android:color/holo_green_dark">
</LinearLayout>
</LinearLayout>
frag_left.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:text="Btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_01"
android:paddingBottom="20dp"
android:background="@android:color/holo_orange_light"
android:textAlignment="center"
tools:textAlignment="center"
android:textColor="@android:color/background_light" />
<Button
android:text="Btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_02"
android:paddingBottom="20dp"
android:background="@android:color/holo_red_light"
android:textColor="@android:color/background_light" />
<Button
android:text="Btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_03"
android:paddingBottom="20dp"
android:background="@android:color/holo_blue_dark"
android:textColor="@android:color/background_light" />
</LinearLayout>
frg_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorPressedHighlight"
android:orientation="vertical"
>
<TextView
android:text="첫번째프래그먼트"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="@android:color/background_light" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt_frag_first"
/>
</LinearLayout>
R.layout.frg_second
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/textColorLink"
>
<TextView
android:text="두번째프래그먼트"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_weight="1"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="@android:color/background_light" />
</LinearLayout>
R.layout.frg_third
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
>
<TextView
android:text="세번째프래그먼트"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_weight="1"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="@android:color/background_light" />
</LinearLayout>
댓글 ( 4)
댓글 남기기