android-改进<<仿QQ>>框架源代码
该文章主要改动于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());
}
}
android-改进<<仿QQ>>框架源代码的更多相关文章
- android 自定义控件二之仿QQ长按删除
自定义Dialog 1.先上个效果图:
- Android应用经典主界面框架之中的一个:仿QQ (使用Fragment, 附源代码)
备注:代码已传至https://github.com/yanzi1225627/FragmentProject_QQ 欢迎fork,如今来审视这份代码,非常多地方写的不太好,欢迎大家指正.有时间我会继 ...
- Android自定义View实现仿QQ实现运动步数效果
效果图: 1.attrs.xml中 <declare-styleable name="QQStepView"> <attr name="outerCol ...
- android桌面悬浮窗仿QQ手机管家加速效果
主要还是用到了WindowManager对桌面悬浮进行管理. 需要一个火箭的悬浮窗 一个发射台悬浮窗 ,判断火箭是否放到了发射台,如果放上了,则使用AsyTask 慢慢将火箭的图片往上移.结束后., ...
- android 自定义scrollview 仿QQ空间效果 下拉伸缩顶部图片,上拉回弹 上拉滚动顶部title 颜色渐变
首先要知道 自定义scrollview 仿QQ效果 下拉伸缩放大顶部图片 的原理是监听ontouch事件,在MotionEvent.ACTION_MOVE事件时候,使用不同倍数的系数,重置布局位置[ ...
- Android实现高仿QQ附近的人搜索展示
本文主要实现了高仿QQ附近的人搜索展示,用到了自定义控件的方法 最终效果如下 1.下面展示列表我们可以使用ViewPager来实现(当然如果你不觉得麻烦,你也可以用HorizontalScrollVi ...
- Android项目源码界面超级华丽的仿QQ最新版本
这是一个我们比较熟悉的一款应用,高仿专仿最新QQ应用源码,也是一个高仿QQ最新版本的项目,界面超级华丽,使用了大量的自定义控件,项目里实现了部分功能,例如WIFI-FTP(把手机变成FTP服务端,可以 ...
- Android插件化的思考——仿QQ一键换肤,思考比实现更重要!
Android插件化的思考--仿QQ一键换肤,思考比实现更重要! 今天群友希望写一个关于插件的Blog,思来想去,插件也不是很懂,只是用大致的思路看看能不能模拟一个,思路还是比较重要的,如果你有兴趣的 ...
- Android特效专辑(六)——仿QQ聊天撒花特效,无形装逼,最为致命
Android特效专辑(六)--仿QQ聊天撒花特效,无形装逼,最为致命 我的关于特效的专辑已经在CSDN上申请了一个专栏--http://blog.csdn.net/column/details/li ...
随机推荐
- 在不同Activity之间传递数据的四种常用方法
在Android中传递数据的方法非常多,本次介绍4中比较常用的数据传递方法: 1.通过Intent传递数据 2.通过静态变量(static)传递数据 3.通过剪贴板(Clipboard)传递数据 4. ...
- sql server中关于批处理与脚本的简单介绍
1.批处理 批处理指的是包含一条或多条T-SQL语句的语句组,这组语句从应用程序一次性地发送到SQL Server服务器执行.SQL Server服务器将批处理语句编译成一个可执行单元(即执行计划), ...
- 在数组中找几个数的和等于某个数[LeetCode]
首先明确一点,这个方面的问题设计到的知识点是数组的查找的问题.对于类似的这样的查找操作的具体办法就是三种解决方法: 1.暴力算法,多个for循环,很高的时间复杂度 2.先排序,然后左右夹逼,但是这样会 ...
- java的IO流包装不当导致从网页获取的数据出现乱码
从网页上获取数据时必须要注意字符集的问题.处理不慎确实苦不堪言. 例如通过URL连接时,将字节流InputStream包装成字符流(以便直接存为String)时,一定要注意加上charsetName这 ...
- wampServer 修改mySql 的root用户密码
刚安装好时 密码是空的 所以不用输入直接回车就能进入 修改密码参考http://www.cnblogs.com/hooray/archive/2011/07/23/2114792.htmlhttp ...
- 试用阿里云RDS的MySQL压缩存储引擎TokuDB
以前就用过自己搭建MySQL服务器的两种存储引擎MyISAM和InnoDB(也用过一点Memory方式),在今年初转向阿里云关系型数据库服务RDS的时候,看到可调参数中有一个TokuDB,不过不太了解 ...
- MB_CHANGE_DOCUMENT使用方法
REFRESH IT_MSEG. CLEAR IT_MSEG. SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_MSEG ...
- <Win32_16>来看看标准菜单和右键菜单的玩法
日常应用中,菜单主要分为两种:(1) 标准菜单(处于应用程序菜单栏处的菜单) (2)右键快捷菜单 几乎你所见过或使用过的软件中,都有它俩儿 为应用程序添加它们的基本步骤: (1)用代码或者IDE ...
- linux内核源码阅读之facebook硬盘加速flashcache之二
flashcache数据结构都在flashcache.h文件中,但在看数据结构之前,需要先过一遍flashcache是什么,要完成哪些功能?如果是自己设计这样一个系统的话,大概要怎么设计. 前面讲过, ...
- 使用@class和#import的细节问题
在.h头文件中导入其它头文件可以使用#import语句,从而在该头文件下使用另一个文件中的类和成员,但是我在使用#import语句时却遇到了以下问题: 首先写一个ViewController类: #i ...