代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
仿QQ的tab滑动的代码
作者:
黄弘玄
/ 发布于
2013/10/11
/
658
public class MainActivity extends ActivityGroup { private RelativeLayout layout; private RelativeLayout layout1; private RelativeLayout layout2; private RelativeLayout layout3; private RelativeLayout bodylayout; private ImageView tab1; private ImageView tab2; private ImageView tab3; private ImageView first; private int current = 1; //默认选中第一个,可以动态的改变此参数值 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initUI(); } private void initUI(){ layout = (RelativeLayout) findViewById(R.id.root); layout1 = (RelativeLayout) findViewById(R.id.layout1); layout2 = (RelativeLayout) findViewById(R.id.layout2); layout3 = (RelativeLayout) findViewById(R.id.layout3); bodylayout = (RelativeLayout) findViewById(R.id.bodylayout); tab1 = (ImageView) findViewById(R.id.tab1); tab1.setOnClickListener(onClickListener); tab2 = (ImageView) findViewById(R.id.tab2); tab2.setOnClickListener(onClickListener); tab3 = (ImageView) findViewById(R.id.tab3); tab3.setOnClickListener(onClickListener); RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rl.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); first = new ImageView(this); first.setTag("first"); first.setImageResource(R.drawable.topbar_select); // 默认选中项 switch (current) { case 1: layout1.addView(first, rl); current = R.id.tab1; break; case 2: layout2.addView(first, rl); current = R.id.tab2; break; case 3: layout3.addView(first, rl); current = R.id.tab3; break; default: break; } View view = getLocalActivityManager().startActivity("index", new Intent(MainActivity.this, Tab1.class)) .getDecorView(); bodylayout.addView(view); } private boolean isAdd = false; // 是否添加过 top_select private int select_width; // top_select_width private int select_height; // top_select_height private int firstLeft; // 第一次添加后的左边距***** private int startLeft; // 起始左边距 // 添加一个view,移除一个view private void replace() { switch (current) { case R.id.tab1: changeTop(layout1); break; case R.id.tab2: changeTop(layout2); break; case R.id.tab3: changeTop(layout3); break; default: break; } } private void changeTop(RelativeLayout relativeLayout){ ImageView old = (ImageView) relativeLayout.findViewWithTag("first");; select_width = old.getWidth(); select_height = old.getHeight(); RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(select_width, select_height); rl.leftMargin = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft(); rl.topMargin = old.getTop() + ((RelativeLayout)old.getParent()).getTop(); // 获取起始位置 firstLeft = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft(); ImageView iv = new ImageView(this); iv.setTag("move"); iv.setImageResource(R.drawable.topbar_select); layout.addView(iv , rl); relativeLayout.removeView(old); } private OnClickListener onClickListener = new OnClickListener(){ public void onClick(View v) { if(!isAdd){ replace(); // 初次使用移除old 添加新的top_select为RelativeLayout所使用 isAdd = true; } ImageView top_select = (ImageView) layout.findViewWithTag("move"); int tabLeft; int endLeft = 0; boolean run = false; switch (v.getId()) { case R.id.tab1: if (current != R.id.tab1) { // 中心位置 tabLeft = ((RelativeLayout) tab1.getParent()).getLeft() + tab1.getLeft() + tab1.getWidth() / 2; // 最终位置 endLeft = tabLeft - select_width / 2; current = R.id.tab1; run = true; bodylayout.removeAllViews(); View view = getLocalActivityManager().startActivity("index", new Intent(MainActivity.this, Tab1.class)) .getDecorView(); bodylayout.addView(view); } break; case R.id.tab2: if (current != R.id.tab2) { tabLeft = ((RelativeLayout) tab2.getParent()).getLeft() + tab2.getLeft() + tab2.getWidth() / 2; endLeft = tabLeft - select_width / 2; current = R.id.tab2; run = true; bodylayout.removeAllViews(); View view = getLocalActivityManager().startActivity("index", new Intent(MainActivity.this, Tab2.class)) .getDecorView(); bodylayout.addView(view); } break; case R.id.tab3: if (current != R.id.tab3) { tabLeft = ((RelativeLayout) tab3.getParent()).getLeft() + tab3.getLeft() + tab3.getWidth() / 2; endLeft = tabLeft - select_width/2; current = R.id.tab3; run = true; bodylayout.removeAllViews(); View view = getLocalActivityManager().startActivity("index", new Intent(MainActivity.this, Tab3.class)) .getDecorView(); bodylayout.addView(view); } break; default: break; } if(run){ TranslateAnimation animation = new TranslateAnimation(startLeft, endLeft - firstLeft, 0, 0); startLeft = endLeft - firstLeft; // 重新设定起始位置 animation.setDuration(400); animation.setFillAfter(true); top_select.bringToFront(); top_select.startAnimation(animation); } } }; }
试试其它关键字
tab滑动
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
黄弘玄
贡献的其它代码
(
4
)
.
仿QQ的tab滑动的代码
.
实现用户登录,系统命令的实现
.
虚拟内存管理系统
.
模拟自由落体
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3