Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android-改進((仿QQ))框架源碼

android-改進((仿QQ))框架源碼

編輯:關於Android編程

該文章主要修改於CSDN某大神的一篇文章,本人覺得這篇文章的面向對象很透徹,下面分享如下可學習的幾點:

Android應用經典主界面框架之一:仿QQ (使用Fragment, 附源碼)

1.通過&符號實現計算優化:(後來通過問同事,說是計算機通過位運算 效率比平時的switch效率高,並講解了該算法的原理。)

public class Constant {

	public static final int SIGN_FRAGMENT_MESSAGE=0x01 <<1;
	public static final int SIGN_FRAGMENT_CONTACTS=0x01 <<2;
	public static final int SIGN_FRAGMENT_NEWS=0x01 <<3;
	public static final int SIGN_FRAGMENT_SETTENGS=0x01 <<4;
	
}

@Override
	public void onClickCallBack(int itemID) {
		String tag = "";
		if ((itemID & Constant.SIGN_FRAGMENT_MESSAGE) != 0) {
			tag = Constant.STR_FRAGMENT_MESSAGE;
		} else if ((itemID & Constant.SIGN_FRAGMENT_CONTACTS) != 0) {
			tag = Constant.STR_FRAGMENT_CONTACTS;
		} else if ((itemID & Constant.SIGN_FRAGMENT_NEWS) != 0) {
			tag = Constant.STR_FRAGMENT_NEWS;
		} else if ((itemID & Constant.SIGN_FRAGMENT_SETTENGS) != 0) {
			tag = Constant.STR_FRAGMENT_SETTINGS;
		}
		mHeaderPanelLayout.setText(tag);
		setTabSection(tag);
	}

2.通過onLayout對底部欄中間的按鈕進行“動態”調整

@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		layoutItem(l, t, r, b);
	}

	private void layoutItem(int left, int top, int right, int bottom) {
		int allChildWidth=0;
		int num=getChildCount();
		for (int i = 0; i < num; i++) {
			allChildWidth+=getChildAt(i).getWidth();
		}
		int absoluteWidth=right-left-getPaddingLeft()-getPaddingRight();
		int blankWidth=(absoluteWidth-allChildWidth)/(num-1);
		//設置第2 3個按鈕的間距
		LayoutParams params1=(LayoutParams) mContactsBtn.getLayoutParams();
		params1.leftMargin=blankWidth;
		mContactsBtn.setLayoutParams(params1);
		LayoutParams params2=(LayoutParams) mNewsBtn.getLayoutParams();
		params2.leftMargin=blankWidth;
		mNewsBtn.setLayoutParams(params2);
	}

3.兩種實例化布局的應用:

1)通過layoutInflater.

	public ImageText(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.image_text_layout, this,true);
		mImageView=(ImageView) findViewById(R.id.iv_imgae_text);
		mTextiew=(TextView) findViewById(R.id.tv_imgae_text);
	}

2)通過onFinishInflater()




    

    

    

    


@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		mMessageBtn=(ImageText) findViewById(R.id.message_btn);
		mContactsBtn=(ImageText) findViewById(R.id.contacts_btn);
		mNewsBtn=(ImageText) findViewById(R.id.news_btn);
		mSettingsBtn=(ImageText) findViewById(R.id.settings_btn);
		initClickEvent();
	}

4.代理實現數據傳遞(IOS中最常用的一種設計模式)

public class BottomPanelLayout extends RelativeLayout implements OnClickListener{
	
	private BottomPanelCallBackProtocal mCallBackProtocal;
	
	//代理協議
	public void setCallBackProtocal(BottomPanelCallBackProtocal callBackProtocal) {
		this.mCallBackProtocal = callBackProtocal;
	}
	
	public interface BottomPanelCallBackProtocal{
		public void onClickCallBack(int itemID);
	}

	/**  
	 * 1.修改本身樣式 
	 * 2.對外聲明事件
	 */
	@Override
	public void onClick(View v) {
		initBottomPanel();
		int index=-1;
		switch (v.getId()) {
			case R.id.message_btn:
				index=Constant.SIGN_FRAGMENT_MESSAGE;
				mMessageBtn.setChecked(index);
				break;
			case R.id.contacts_btn:
				index=Constant.SIGN_FRAGMENT_CONTACTS;
				mContactsBtn.setChecked(index);
				break;
			case R.id.news_btn:
				index=Constant.SIGN_FRAGMENT_NEWS;
				mNewsBtn.setChecked(index);
				break;
			case R.id.settings_btn:
				index=Constant.SIGN_FRAGMENT_SETTENGS;
				mSettingsBtn.setChecked(index);
				break;
			default:
				break;
		}
		if (mCallBackProtocal!=null) {
			mCallBackProtocal.onClickCallBack(index);
		}
	}
	
}


public class MainActivity extends Activity implements
		BottomPanelCallBackProtocal {
	@Override
	public void onClickCallBack(int itemID) {
		String tag = "";
		if ((itemID & Constant.SIGN_FRAGMENT_MESSAGE) != 0) {
			tag = Constant.STR_FRAGMENT_MESSAGE;
		} else if ((itemID & Constant.SIGN_FRAGMENT_CONTACTS) != 0) {
			tag = Constant.STR_FRAGMENT_CONTACTS;
		} else if ((itemID & Constant.SIGN_FRAGMENT_NEWS) != 0) {
			tag = Constant.STR_FRAGMENT_NEWS;
		} else if ((itemID & Constant.SIGN_FRAGMENT_SETTENGS) != 0) {
			tag = Constant.STR_FRAGMENT_SETTINGS;
		}
		mHeaderPanelLayout.setText(tag);
		setTabSection(tag);
	}

}

5.修改原來Fragment跳轉的代碼(之前方法ensureTransaction()是在粘貼或者消除的時候都要判斷,但作為一個事務,只需要保證事物只有一個開始即可,而不需要每次都調用)

private void setTabSection(String tag) {
		if (TextUtils.equals(tag, currFagTag)) {
			return;
		}
		ensureTransaction();
		if (currFagTag != null && !currFagTag.equals("")) {
			detachFragment(getFragment(currFagTag));
		}
		attachFragment(R.id.fragment_panel, getFragment(tag), tag);
		commitTransaction();
	}

private void ensureTransaction() {
		if (mFragmentTransaction == null) {
			mFragmentTransaction = mFragmentManager.beginTransaction();
			mFragmentTransaction
					.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		}
	}

6.Fragment對Fragment進行跳轉並傳值的改進。(這裡試驗從MessageFragment 點擊textview跳轉到 ContactFragment );

1>在MessageFragment 中

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getActivity().findViewById(R.id.msg_tv).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).setTabSection(Constant.STR_FRAGMENT_CONTACTS);
			}
		});
	}

2>在ContactFragment 中聲明數據代理

	//聲明一個變量,該變量存儲該Fragment所需要的一切參數 當刷新View時手動調用其更新數據
	private ContactFragmentCallBack mContactFragmentCallBack;
	
	//聲明該接口
	public interface ContactFragmentCallBack{
		//說明該Fragment更新時需要一個String對象
		public String getContentStr();
	}

3>MessageFragment 實現該代理

public class MessageFragment extends BaseFragment implements ContactFragmentCallBack{
	@Override
	public String getContentStr() {
		return "abc";
	}
}

4>在ContactFragment 中回調

	@Override
	public void onResume() {
		super.onResume();
		MainActivity.currFagTag=Constant.STR_FRAGMENT_CONTACTS;
		
		//通過取出 存儲於上個Fragment中的數據
		Fragment f=((MainActivity)getActivity()).getFragment(Constant.STR_FRAGMENT_MESSAGE);
		if (f!=null&&f instanceof ContactFragmentCallBack) {
			mContactFragmentCallBack=(ContactFragmentCallBack)f;
			TextView textView=(TextView) ((MainActivity)getActivity()).findViewById(R.id.contact_tv);
			textView.setText(mContactFragmentCallBack.getContentStr());
		}
	}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved