¾È±Ô °øºÎ¹æ

Android > ActionBar - Tab ¸Þ´º CsutomView Àû¿ëÇßÀ» °æ¿ì Tab ¼±ÅÃ/ÇØÁ¦½Ã À̹ÌÁö º¯°æ¹æ¹ý

µî·ÏÀÏ : 2017-07-05 19:00 Á¶È¸¼ö : 53,021

ActionBar Tab ¸Þ´º¸¦ CuatomView Àû¿ëÇÏ´Â ¼Ò½º´Â ¾Æ·¡¿Í °°½À´Ï´Ù.
 

	   icons = new int[] {
	            R.drawable.tab1_on,
	            R.drawable.tab2_on,
	            R.drawable.tab3_on,
	            R.drawable.tab4_on
	    };
	    icons_selected = new int[] {
	            R.drawable.tab1_off,
	            R.drawable.tab2_off,
	            R.drawable.tab3_off,
	            R.drawable.tab4_off
	    };


	View tabView1 = getLayoutInflater().inflate(R.layout.layout_actionbar_tab, null);
	View tabView2 = getLayoutInflater().inflate(R.layout.layout_actionbar_tab, null);
	View tabView3 = getLayoutInflater().inflate(R.layout.layout_actionbar_tab, null);
	
	TextView tabText1 = (TextView) tabView1.findViewById(R.id.tabText);
	TextView tabText2 = (TextView) tabView2.findViewById(R.id.tabText);
	TextView tabText3 = (TextView) tabView3.findViewById(R.id.tabText);

	tabText1.setText(R.string.sometext1);
	tabText2.setText(R.string.sometext2);
	tabText3.setText(R.string.sometext3);

	tabImage1 = (ImageView) tabView1.findViewById(R.id.tabIcon);
	tabImage2 = (ImageView) tabView2.findViewById(R.id.tabIcon);
	tabImage3 = (ImageView) tabView3.findViewById(R.id.tabIcon);
	
	tabImage1.setImageDrawable(getResources().getDrawable(R.drawable.tab1_on));
	tabImage2.setImageDrawable(getResources().getDrawable(R.drawable.tab2_on));
	tabImage3.setImageDrawable(getResources().getDrawable(R.drawable.tab3_on));
	

	Tab tab1 = actionBar.newTab();
	//tab1.setText("¸Þ´º1");		
	//tab1.setIcon(null);
	tab1.setCustomView(tabView1);  
	TabTestListener a = new TabTestListener(this, "1", Fragment01.class);
	tab1.setTabListener(a);

	
	Tab tab2 = actionBar.newTab();
	//tab2.setText("¸Þ´º2");
	//tab2.setIcon(icons[1]);
	tab2.setCustomView(tabView2);
	TabTestListener b = new TabTestListener(this, "1", Fragment02.class);
	tab2.setTabListener(b);

	Tab tab3 = actionBar.newTab();
	//tab3.setText("tab_3");
	//tab3.setIcon(icons[2]);
	tab3.setCustomView(tabView3);
	
	
	TabTestListener c = new TabTestListener(this, "1", Fragment03.class);
	tab3.setTabListener(c);
	

	actionBar.addTab(tab1);
	actionBar.addTab(tab2);
	actionBar.addTab(tab3);


layout_actionbar_tab.xml ·¹À̾ƿô ÆÄÀÏ À» tab1, tab2, tab3 ¿¡¼­ ÂüÁ¶Çؼ­ Àû¿ëÇÒ ¼ö ÀÖ´Ù.

ÅÇÀ» ¼±ÅÃ/ÇØÁ¦µÉ °æ¿ì TabTestListener À̺¥Æ®°¡ ¹ß»ýÇÏ°Ô µÈ´Ù.

onTabSelected ,  onTabUnselected ¸Þ¼­µå¿¡¼­ ¾ÆÀÌÄÜÀ» º¯°æÇÒ ¼ö ÀÖÀ¸¸ç , ActionBar °´Ã¼ÀÇ Á¤º¸µµ ¹Ù²Ü ¼ö ÀÖ½À´Ï´Ù.

±×¿¡ ´ëÇÑ ¿¹Á¦¸¦ È®ÀÎÇØ º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
 
	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)	 {
		}
	}	


¾Æ·¡ ¼Ò½º틑 tabImage1, tabImage2, tabImage3 °´Ã¼ÀÇ À̹ÌÁö¸¦ ¹Ù²Ù´Â ¿¹Á¦ ÀÔ´Ï´Ù.

tab.getPosition() ¸Þ¼­µå¸¦ ÀÌ¿ëÇؼ­ ÇöÀç ¹ß»ýÇÑ Tab À̺¥Æ®ÀÇ index °ªÀ» ±¸ÇÒ ¼ö ÀÖ½À´Ï´Ù.


 
	class TabTestListener implements TabListener {
		private Fragment mFragment;
		private final Activity mActivity;
		private final String mTag;
		private final Class mClass;
	
		public TabTestListener(Activity activity, String tag, Class clz)	 {
			mActivity = activity;
			mTag = tag;
			mClass = clz;
		}
	
		public void onTabSelected(Tab tab, FragmentTransaction ft)	 {
			
			//getActionBar().getSelectedTab().setIcon(icons_selected[tab.getPosition()]);	//¾×¼Ç¹Ù ¹è°æÀ̹ÌÁö º¯°æ
			
			if(tab.getPosition()==0)
			{
			tabImage1.setImageDrawable(getResources().getDrawable(icons_selected[tab.getPosition()]));
			}			
			else if(tab.getPosition()==1)
			{
			tabImage2.setImageDrawable(getResources().getDrawable(icons_selected[tab.getPosition()]));
			}
			else if(tab.getPosition()==2)
			{
			tabImage3.setImageDrawable(getResources().getDrawable(icons_selected[tab.getPosition()]));
			}
			
			
			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)	 {

			//getActionBar().getSelectedTab().setIcon(icons[tab.getPosition()]);		//¾×¼Ç¹Ù ¹è°æÀ̹ÌÁö º¯°æ
			

			if(tab.getPosition()==0)
			{
			tabImage1.setImageDrawable(getResources().getDrawable(icons[tab.getPosition()]));
			}			
			else if(tab.getPosition()==1)
			{
			tabImage2.setImageDrawable(getResources().getDrawable(icons[tab.getPosition()]));
			}
			else if(tab.getPosition()==2)
			{
			tabImage3.setImageDrawable(getResources().getDrawable(icons[tab.getPosition()]));
			}
			
			if (mFragment != null)
			{
				ft.detach(mFragment);
			}
		}
	
		public void onTabReselected(Tab tab, FragmentTransaction ft)	 {
		}
	}	
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.