读取通讯录,短信,编辑短信
public class MainActivity extends TabActivity implements OnClickListener {
private TabHost mTabHost;
private View mSlideView; // 滑动背景
private int basicWidth; // 第一等份的宽度
private int startX = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 消除标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mSlideView = findViewById(R.id.slide_view);
findViewById(R.id.rl_conversation).setOnClickListener(this);
findViewById(R.id.rl_fodler).setOnClickListener(this);
findViewById(R.id.rl_group).setOnClickListener(this);
// 获得视图树的观察者
// final ViewTreeObserver viewTreeObserver = mSlideView.getViewTreeObserver();
// 监听全局布局
mSlideView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 只用一次, 移除掉监听事件
mSlideView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// 初始化滑动view的宽高
LayoutParams lp = (LayoutParams) mSlideView.getLayoutParams();
View llConversation = findViewById(R.id.ll_conversation);
lp.width = llConversation.getWidth();
lp.height = llConversation.getHeight();
lp.leftMargin = llConversation.getLeft();
mSlideView.setLayoutParams(lp);
// 得到第一等分的宽度
basicWidth = findViewById(R.id.rl_conversation).getWidth();
}
});
// 添加会话页签
addTabSpec("conversation", "会话",
R.drawable.tab_conversation, new Intent(this, ConversationUI.class));
// 添加文件夹页签
addTabSpec("folder", "文件夹",
R.drawable.tab_folder, new Intent(this, FolderUI.class));
// 添加群组页签
addTabSpec("group", "群组",
R.drawable.tab_group, new Intent(this, GroupUI.class));
}
/**
* 添加一个页签
* @param tag 页签的标识(唯一的id)
* @param label 标题
* @param iconID 图片
* @param intent 内容(指向activity)
*/
private void addTabSpec(String tag, String label, int iconID, Intent intent) {
// 根据tag创建一个页签
TabSpec tabSpec = mTabHost.newTabSpec(tag);
// 设置页签的标题和图片
tabSpec.setIndicator(label, getResources().getDrawable(iconID));
// 设置页签对应的内容(指向的是activity)
tabSpec.setContent(intent);
mTabHost.addTab(tabSpec);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rl_conversation: // 会话
if(!"conversation".equals(mTabHost.getCurrentTabTag())) {
mTabHost.setCurrentTabByTag("conversation");
startTranslateAnimation(startX, 0);
startX = 0;
}
break;
case R.id.rl_fodler: // 文件夹
if(!"folder".equals(mTabHost.getCurrentTabTag())) {
mTabHost.setCurrentTabByTag("folder");
startTranslateAnimation(startX, basicWidth);
startX = basicWidth;
}
break;
case R.id.rl_group: // 群组
if(!"group".equals(mTabHost.getCurrentTabTag())) {
mTabHost.setCurrentTabByTag("group");
startTranslateAnimation(startX, basicWidth * 2);
startX = basicWidth * 2;
}
break;
default:
break;
}
}
/**
* 执行位移动画
* @param fromXDelta x轴开始的偏移量
* @param toXDelta x轴结束的偏移量
*/
private void startTranslateAnimation(float fromXDelta, float toXDelta) {
TranslateAnimation anim = new TranslateAnimation(
fromXDelta, toXDelta,
0, 0);
anim.setDuration(500);
anim.setFillAfter(true);
mSlideView.startAnimation(anim);
}
}




评论