Android应用经典主界面框架之中的一个:仿QQ (使用Fragment, 附源代码)——修改版...

该文章主要改动于CSDN某大神的一篇文章,本人认为这篇文章的面向对象非常透彻,以下分享例如以下可学习的几点:

Android应用经典主界面框架之中的一个:仿QQ (使用Fragment, 附源代码)

1.通过&符号实现计算优化:(后来通过问同事,说是计算机通过位运算 效率比平时的switch效率高,并解说了该算法的原理。)

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. public class Constant {  
  2.   
  3.     public static final int SIGN_FRAGMENT_MESSAGE=0x01 <<1;  
  4.     public static final int SIGN_FRAGMENT_CONTACTS=0x01 <<2;  
  5.     public static final int SIGN_FRAGMENT_NEWS=0x01 <<3;  
  6.     public static final int SIGN_FRAGMENT_SETTENGS=0x01 <<4;  
  7.       
  8. }  

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     public void onClickCallBack(int itemID) {  
  3.         String tag = "";  
  4.         if ((itemID & Constant.SIGN_FRAGMENT_MESSAGE) != 0) {  
  5.             tag = Constant.STR_FRAGMENT_MESSAGE;  
  6.         } else if ((itemID & Constant.SIGN_FRAGMENT_CONTACTS) != 0) {  
  7.             tag = Constant.STR_FRAGMENT_CONTACTS;  
  8.         } else if ((itemID & Constant.SIGN_FRAGMENT_NEWS) != 0) {  
  9.             tag = Constant.STR_FRAGMENT_NEWS;  
  10.         } else if ((itemID & Constant.SIGN_FRAGMENT_SETTENGS) != 0) {  
  11.             tag = Constant.STR_FRAGMENT_SETTINGS;  
  12.         }  
  13.         mHeaderPanelLayout.setText(tag);  
  14.         setTabSection(tag);  
  15.     }  

 

 

2.通过onLayout对底部栏中间的button进行“动态”调整

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  3.         super.onLayout(changed, l, t, r, b);  
  4.         layoutItem(l, t, r, b);  
  5.     }  
  6.   
  7.     private void layoutItem(int left, int top, int right, int bottom) {  
  8.         int allChildWidth=0;  
  9.         int num=getChildCount();  
  10.         for (int i = 0; i < num; i++) {  
  11.             allChildWidth+=getChildAt(i).getWidth();  
  12.         }  
  13.         int absoluteWidth=right-left-getPaddingLeft()-getPaddingRight();  
  14.         int blankWidth=(absoluteWidth-allChildWidth)/(num-1);  
  15.         //设置第2 3个button的间距  
  16.         LayoutParams params1=(LayoutParams) mContactsBtn.getLayoutParams();  
  17.         params1.leftMargin=blankWidth;  
  18.         mContactsBtn.setLayoutParams(params1);  
  19.         LayoutParams params2=(LayoutParams) mNewsBtn.getLayoutParams();  
  20.         params2.leftMargin=blankWidth;  
  21.         mNewsBtn.setLayoutParams(params2);  
  22.     }  


3.两种实例化布局的应用:

 

1)通过layoutInflater.

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. public ImageText(Context context, AttributeSet attrs) {  
  2.     super(context, attrs);  
  3.     LayoutInflater.from(context).inflate(R.layout.image_text_layout, this,true);  
  4.     mImageView=(ImageView) findViewById(R.id.iv_imgae_text);  
  5.     mTextiew=(TextView) findViewById(R.id.tv_imgae_text);  
  6. }  

 

 

2)通过onFinishInflater()

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <org.lean.ui.BottomPanelLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:background="#FFF3F3F3"  
  7.     android:paddingLeft="20dp"  
  8.     android:paddingRight="20dp"  
  9.     android:layout_alignParentBottom="true" >  
  10.   
  11.     <org.lean.ui.ImageText  
  12.         android:id="@+id/message_btn"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true" />  
  16.   
  17.     <org.lean.ui.ImageText  
  18.         android:id="@+id/contacts_btn"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_toRightOf="@id/message_btn" />  
  22.   
  23.     <org.lean.ui.ImageText  
  24.         android:id="@+id/news_btn"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_toRightOf="@id/contacts_btn" />  
  28.   
  29.     <org.lean.ui.ImageText  
  30.         android:id="@+id/settings_btn"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_alignParentRight="true" />  
  34.   
  35. </org.lean.ui.BottomPanelLayout>  

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2.     protected void onFinishInflate() {  
  3.         super.onFinishInflate();  
  4.         mMessageBtn=(ImageText) findViewById(R.id.message_btn);  
  5.         mContactsBtn=(ImageText) findViewById(R.id.contacts_btn);  
  6.         mNewsBtn=(ImageText) findViewById(R.id.news_btn);  
  7.         mSettingsBtn=(ImageText) findViewById(R.id.settings_btn);  
  8.         initClickEvent();  
  9.     }  

 

4.代理实现数据传递(IOS中最经常使用的一种设计模式)

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. public class BottomPanelLayout extends RelativeLayout implements OnClickListener{  
  2.       
  3.     private BottomPanelCallBackProtocal mCallBackProtocal;  
  4.       
  5.     //代理协议  
  6.     public void setCallBackProtocal(BottomPanelCallBackProtocal callBackProtocal) {  
  7.         this.mCallBackProtocal = callBackProtocal;  
  8.     }  
  9.       
  10.     public interface BottomPanelCallBackProtocal{  
  11.         public void onClickCallBack(int itemID);  
  12.     }  
  13.   
  14.     /**   
  15.      * 1.改动本身样式  
  16.      * 2.对外声明事件 
  17.      */  
  18.     @Override  
  19.     public void onClick(View v) {  
  20.         initBottomPanel();  
  21.         int index=-1;  
  22.         switch (v.getId()) {  
  23.             case R.id.message_btn:  
  24.                 index=Constant.SIGN_FRAGMENT_MESSAGE;  
  25.                 mMessageBtn.setChecked(index);  
  26.                 break;  
  27.             case R.id.contacts_btn:  
  28.                 index=Constant.SIGN_FRAGMENT_CONTACTS;  
  29.                 mContactsBtn.setChecked(index);  
  30.                 break;  
  31.             case R.id.news_btn:  
  32.                 index=Constant.SIGN_FRAGMENT_NEWS;  
  33.                 mNewsBtn.setChecked(index);  
  34.                 break;  
  35.             case R.id.settings_btn:  
  36.                 index=Constant.SIGN_FRAGMENT_SETTENGS;  
  37.                 mSettingsBtn.setChecked(index);  
  38.                 break;  
  39.             default:  
  40.                 break;  
  41.         }  
  42.         if (mCallBackProtocal!=null) {  
  43.             mCallBackProtocal.onClickCallBack(index);  
  44.         }  
  45.     }  
  46.       
  47. }  

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. public class MainActivity extends Activity implements  
  2.         BottomPanelCallBackProtocal {  
  3.     @Override  
  4.     public void onClickCallBack(int itemID) {  
  5.         String tag = "";  
  6.         if ((itemID & Constant.SIGN_FRAGMENT_MESSAGE) != 0) {  
  7.             tag = Constant.STR_FRAGMENT_MESSAGE;  
  8.         } else if ((itemID & Constant.SIGN_FRAGMENT_CONTACTS) != 0) {  
  9.             tag = Constant.STR_FRAGMENT_CONTACTS;  
  10.         } else if ((itemID & Constant.SIGN_FRAGMENT_NEWS) != 0) {  
  11.             tag = Constant.STR_FRAGMENT_NEWS;  
  12.         } else if ((itemID & Constant.SIGN_FRAGMENT_SETTENGS) != 0) {  
  13.             tag = Constant.STR_FRAGMENT_SETTINGS;  
  14.         }  
  15.         mHeaderPanelLayout.setText(tag);  
  16.         setTabSection(tag);  
  17.     }  
  18.   
  19. }  


5.改动原来Fragment跳转的代码(之前方法ensureTransaction()是在粘贴或者消除的时候都要推断,但作为一个事务,仅仅须要保证事物仅仅有一个開始就可以,而不须要每次都调用)

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. private void setTabSection(String tag) {  
  2.         if (TextUtils.equals(tag, currFagTag)) {  
  3.             return;  
  4.         }  
  5.         ensureTransaction();  
  6.         if (currFagTag != null && !currFagTag.equals("")) {  
  7.             detachFragment(getFragment(currFagTag));  
  8.         }  
  9.         attachFragment(R.id.fragment_panel, getFragment(tag), tag);  
  10.         commitTransaction();  
  11.     }  

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. private void ensureTransaction() {  
  2.         if (mFragmentTransaction == null) {  
  3.             mFragmentTransaction = mFragmentManager.beginTransaction();  
  4.             mFragmentTransaction  
  5.                     .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);  
  6.         }  
  7.     }  


6.Fragment对Fragment进行跳转并传值的改进。(这里试验从MessageFragment 点击textview跳转到 ContactFragment );

 

1>在MessageFragment 中

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2. public void onActivityCreated(Bundle savedInstanceState) {  
  3.     super.onActivityCreated(savedInstanceState);  
  4.     getActivity().findViewById(R.id.msg_tv).setOnClickListener(new OnClickListener() {  
  5.           
  6.         @Override  
  7.         public void onClick(View v) {  
  8.             ((MainActivity)getActivity()).setTabSection(Constant.STR_FRAGMENT_CONTACTS);  
  9.         }  
  10.     });  
  11. }  

 

 

2>在ContactFragment 中声明数据代理

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. //声明一个变量,该变量存储该Fragment所须要的一切參数 当刷新View时手动调用其更新数据  
  2. private ContactFragmentCallBack mContactFragmentCallBack;  
  3.   
  4. //声明该接口  
  5. public interface ContactFragmentCallBack{  
  6.     //说明该Fragment更新时须要一个String对象  
  7.     public String getContentStr();  
  8. }  

 

3>MessageFragment 实现该代理

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. public class MessageFragment extends BaseFragment implements ContactFragmentCallBack{  
  2.     @Override  
  3.     public String getContentStr() {  
  4.         return "abc";  
  5.     }  
  6. }  

 

 

4>在ContactFragment 中回调

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. @Override  
  2. public void onResume() {  
  3.     super.onResume();  
  4.     MainActivity.currFagTag=Constant.STR_FRAGMENT_CONTACTS;  
  5.       
  6.     //通过取出 存储于上个Fragment中的数据  
  7.     Fragment f=((MainActivity)getActivity()).getFragment(Constant.STR_FRAGMENT_MESSAGE);  
  8.     if (f!=null&&f instanceof ContactFragmentCallBack) {  
  9.         mContactFragmentCallBack=(ContactFragmentCallBack)f;  
  10.         TextView textView=(TextView) ((MainActivity)getActivity()).findViewById(R.id.contact_tv);  
  11.         textView.setText(mContactFragmentCallBack.getContentStr());  
  12.     }  
  13. }  


改动后的项目源代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值