閱讀997 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android ActionBar Tabs學習筆記

本例主要實現用Tab切換不同的Fragment,點擊View顯示or隱藏ActionBar,把ActionBar 設為透明,使界麵更加友好,詳細代碼見資源裏的ActionBarTabs。

ActionBar Tab主要用於Fragment之間的切換,其必須要設置ActionBar.TabListener,詳細代碼如下ActionBarActivity.java:

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.MotionEvent;
import android.view.Window;

public class ActionBarActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//使ActionBar變得透明
		requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		setContentView(R.layout.main);

		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		// remove the activity title to make space for tabs
		actionBar.setDisplayShowTitleEnabled(false);

		AFragment aFragment = new AFragment(); 
		actionBar.addTab(actionBar.newTab().setText("Tab-A")            
				.setTabListener(new ListenerA(aFragment))); 
		
		BFragment bFragment = new BFragment(); 
		actionBar.addTab(actionBar.newTab().setText("Tab-B")            
				.setTabListener(new ListenerB(bFragment)));
	}
	//點擊顯示or隱藏ActionBar
	public boolean onTouchEvent(MotionEvent event){
		ActionBar bar = getActionBar();
		switch(event.getAction()){
			case MotionEvent.ACTION_UP:
				if(bar.isShowing()) bar.hide();
				else bar.show();
				break;
			default:
					break;
		}
		return true;
	}
	
	private class ListenerA implements ActionBar.TabListener {
		private AFragment mFragment;
		// Called to create an instance of the listener when adding a new tab
		public ListenerA(AFragment fragment) {
			mFragment = fragment;
		}
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.add(R.id.fragment, mFragment, null);
		}
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			ft.remove(mFragment);
		}
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// do nothing }
		}
	}

	private class ListenerB implements ActionBar.TabListener {
		private BFragment mFragment;

		// Called to create an instance of the listener when adding a new tab
		public ListenerB(BFragment fragment) {
			mFragment = fragment;
		}

		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.add(R.id.fragment, mFragment, null);
		}

		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			ft.remove(mFragment);
		}

		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// do nothing }
		}
	}
}

其中涉及到兩個Fragment,在前麵Fragment的筆記中講過,這裏就不再贅述。類AFragment實現如下,BFragment實現與這類似:  

public class AFragment extends Fragment {
	public View onCreateView(LayoutInflater inflater, ViewGroup container, 
			Bundle savedInstanceState) {        
		return inflater.inflate(R.layout.alayout, container, false);   
		}
	}


最後更新:2017-04-02 16:47:42

  上一篇:go Android應用開發之(Gson的使用)
  下一篇:go Struts中掃描包的層數問題