Android > ActionBar - Tab + Fragment ±¸Çö ¹æ¹ý
µî·ÏÀÏ : 2017-07-05 18:53
Á¶È¸¼ö : 53,019
ActionBar¿¡ TabÀ» ±¸ÇöÇÒ¶§´Â °¡±ÞÀû Fragment¸¦ »ç¿ëÇϽô °ÍÀÌ ÄÚµå°ü¸® ¹× ¸®¼Ò½º°ü¸®¿¡ ¿ëÀÌÇÕ´Ï´Ù.
±âÁ¸ÀÇ GB ÀÌÇÏÀÇ ¹öÀü¿¡¼´Â TabActivity¸¦ ÅëÇØ ViewGroupÀ¸·Î TabÀ» »ý¼º ¹× °ü¸®¸¦ Çß¾ú´Ù.
ÇÏÁö¸¸ ICSºÎÅÍ´Â À§ÀÇ ¹æ¹ýº¸´Ù ActionBar¿Í Fragment¸¦ ÀÌ¿ëÇØ TabÀ» ±¸¼ºÇϱ⸦ ±ÇÇÑ´Ù. (API 13 ºÎÅÍ Àû¿ë)
Fragment »ç¿ë¹æ¹ýÀ» ¾Ë¾Æº¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
Tab ¸Þ´º Á¦ÀÛ ¼ø¼ ÀÔ´Ï´Ù.
1. ÇöÀç ActivityÀÇ ActionBar ·¹ÆÛ·±½º¸¦ ¹Þ¾Æ¿Â´Ù.
2. ActionBar¸¦ Navigation Mode·Î ¼³Á¤ÇÑ´Ù.
3. ActionBar¿¡ Tab¿¡ °ü·ÃµÈ ¸®½º³Ê¸¦ µî·ÏÇÏ°í, °¢ Tab¿¡ µé¾î°¥ Fragment¸¦ ¼³Á¤ÇÑ´Ù.
¸ÕÀú TabÀ» Ŭ¸¯ÇÒ¶§ ³ª¿À´Â layout À» µðÀÚÀÎ ÇÕ´Ï´Ù.
layout Æú´õ¿¡ layout_frag01.xml ÆÄÀÏÀ» ¸¸µì´Ï´Ù.
<?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/user_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="frag01" />
</LinearLayout>
layout Æú´õ¿¡ layout_frag02.xml ÆÄÀÏÀ» ¸¸µì´Ï´Ù.
<?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/user_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="frag02" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
layout Æú´õ¿¡ layout_frag03.xml ÆÄÀÏÀ» ¸¸µì´Ï´Ù.
<?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/user_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="frag03" />
</LinearLayout>
MainActivity.java ÆÄÀÏ ³»¿ë ÀÔ´Ï´Ù.
public class MainActivity extends Activity {
ActionBar actionBar; //ActionBar ÂüÁ¶º¯¼ö
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.layout_main);
ActionBar abar = getActionBar();
abar.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
// tab
Tab tab1 = abar.newTab();
tab1.setText("tab_1");
tab1.setIcon(R.drawable.btn_tab1); //Tab ¸Þ´º¿¡ ¾ÆÀÌÄÜÀ» »ðÀÔÇÒ ¼ö ÀÖ´Ù.
TabTestListener<Fragment01> a = new TabTestListener<Fragment01>(this, "1", Fragment01.class);
tab1.setTabListener(a);
Tab tab2 = abar.newTab();
tab2.setText("tab_2");
tab2.setIcon(R.drawable.btn_tab2); //Tab ¸Þ´º¿¡ ¾ÆÀÌÄÜÀ» »ðÀÔÇÒ ¼ö ÀÖ´Ù.
TabTestListener<Fragment02> b = new TabTestListener<Fragment02>(this, "1", Fragment02.class);
tab2.setTabListener(b);
Tab tab3 = abar.newTab();
tab3.setText("tab_3");
tab3.setIcon(R.drawable.btn_tab1); //Tab ¸Þ´º¿¡ ¾ÆÀÌÄÜÀ» »ðÀÔÇÒ ¼ö ÀÖ´Ù.
TabTestListener<Fragment03> c = new TabTestListener<Fragment03>(this, "1", Fragment03.class);
tab3.setTabListener(c);
abar.addTab(tab1);
abar.addTab(tab2);
abar.addTab(tab3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.item1) {
return true;
}
return super.onOptionsItemSelected(item);
}
class TabTestListener<T extends Fragment> implements TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabTestListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null)
{
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null)
{
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}
Fragment01.java , Fragment02.java , Fragment03.java ÈÀÏ ³»¿ë ÀÔ´Ï´Ù.
public class Fragment01 extends Fragment {
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.layout_frag01, null);
return view;
}
}
public class Fragment02 extends Fragment {
Context mContext;
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.layout_frag02, null);
Button btn11 = (Button) view.findViewById(R.id.button1);
mContext = container.getContext();
btn11.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast toast = Toast.makeText(mContext, "button1 Ŭ¸¯!!!!",Toast.LENGTH_LONG);
toast.show();
}
});
return view;
}
}
public class Fragment03 extends Fragment {
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.layout_frag03, null);
return view;
}
}