標籤:android style blog http color java 使用 os
該文章主要修改於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;}
@Overridepublic 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對底部欄中間的按鈕進行“動態”調整
@Overrideprotected 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()
<?xml version="1.0" encoding="utf-8"?><org.lean.ui.BottomPanelLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFF3F3F3" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_alignParentBottom="true" > <org.lean.ui.ImageText android:id="@+id/message_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" /> <org.lean.ui.ImageText android:id="@+id/contacts_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/message_btn" /> <org.lean.ui.ImageText android:id="@+id/news_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/contacts_btn" /> <org.lean.ui.ImageText android:id="@+id/settings_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" /></org.lean.ui.BottomPanelLayout>
@Overrideprotected 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.對外聲明事件 */@Overridepublic 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 implementsBottomPanelCallBackProtocal {@Overridepublic 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 中
@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);getActivity().findViewById(R.id.msg_tv).setOnClickListener(new OnClickListener() {@Overridepublic 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{@Overridepublic String getContentStr() {return "abc";}}
4>在ContactFragment 中回調
@Overridepublic 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());}}