1. 程式人生 > >Android聊天軟體的開發(一)--預備知識

Android聊天軟體的開發(一)--預備知識

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.PopupMenu.OnMenuItemClickListener;
public class MainActivity extends ActionBarActivity implements OnMenuItemClickListener{
	private Context mContext;
	private PopupMenu mAddMenu;
	private PopupMenu mManageMenu;
	private ActionBar mActionBar;
	
	private ChatFragment mChatFragment;  
	private ContactsFragment mContactsFragment;
	
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
        	super.onCreate(savedInstanceState);
        	setContentView(R.layout.main);
        
        	mContext = this;
        	mChatFragment = new ChatFragment(); 
        	mContactsFragment = new ContactsFragment();
        	setActionBar();
       
    	}
    
	private void setActionBar()
    	{
    		mActionBar = getSupportActionBar();
       	 	mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//     	 	mActionBar.setDisplayShowTitleEnabled(false);//隱藏ActionBar的title
        
        	TabListener listener = new MyTabListener();
        	mActionBar.addTab(mActionBar.newTab().setIcon(R.drawable.main_tab_chat).setText(R.string.fragment_chat).setTabListener(listener));  
        mActionBar.addTab(mActionBar.newTab().setIcon(R.drawable.main_tab_contacts).setText

(R.string.fragment_contacts).setTabListener(listener));
        
    	}
	
    
    	/**新增ActionBar的Menu*/
    	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return super.onCreateOptionsMenu(menu);
	}

    	/**新增Menu的下拉子選單*/
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_manage:
			if(mManageMenu == null)
			{
				mManageMenu = new PopupMenu(mContext,
		    			findViewById(R.id.action_manage));
		    	mManageMenu.inflate(R.menu.main_item_manage);
		    	mManageMenu.setOnMenuItemClickListener(this);
			}
			mManageMenu.show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	/**監聽子選單的點選事件*/
	@Override
	public boolean onMenuItemClick(MenuItem menuItem) {
		switch (menuItem.getItemId()) {
		case R.id.menu_userInfo:
		//TODO ...
		break;
		case R.id.menu_setting:
		//TODO ...
		break;
		}
		return false;
	}
	
	/**TabListener實現類*/
	class MyTabListener implements TabListener{
		public void onTabSelected(Tab tab, FragmentTransaction arg1) {
			
			switch (tab.getPosition()) {  
			case 0://聊天介面
				getSupportFragmentManager().beginTransaction()  
				.replace(R.id.main_container, mChatFragment).commit();  
				break;  
			case 1://通訊錄介面
				getSupportFragmentManager().beginTransaction()  
				.replace(R.id.main_container, mContactsFragment).commit();  
				break;  
			}  
		}
		public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		}
		public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		}
	}
}