该文章主要改动于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对底部栏中间的button进行“动态”调整

@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个button的间距
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>
@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. .net 开源组件
  2. 洛谷P1119 灾后重建[Floyd]
  3. 一个简单的RMAN自动备份脚本
  4. 值得 Web 开发人员收藏的16款 HTML5 工具
  5. php中rsa加密解密验证
  6. Teleport Ultra/Teleport Pro的冗余代码批量清理方法
  7. Android--学习记录
  8. MS16-016 提权EXP
  9. php异常处理示例
  10. BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路
  11. 5 个在 Linux 中管理文件类型和系统时间的有用命令
  12. 如何“任性”使用Android的drawText()
  13. Ubuntu 问题解决汇总
  14. 实现Runnable接口和扩展Thread使用场景
  15. Android Weekly Notes Issue #248
  16. 【HDU 2966 k-dimensional Tree 入个门】
  17. DRF 视图和路由
  18. bat遍历目录
  19. day2-python工具的选择使用
  20. LeetCode 11. [&#128065;] Container With Most Water &amp; two pointers

热门文章

  1. string s = HttpContext.Current.Server.MapPath(&quot;&quot;);
  2. Java RMI 简介及其优劣势总结
  3. p95 3.5、3.8
  4. Windows 配置JAVA的环境变量
  5. Ajax学习之小结
  6. 网络编程——TCP连接
  7. maxContainerCapability 设置不足
  8. mapreduce 关于小文件导致任务缓慢的问题
  9. (Problem 53)Combinatoric selections
  10. Cocos2d—X游戏开发之VS2010 控制台输出中文,模拟器中文乱码问题解决