package com.caigang.test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
public class MainActivity extends TabActivity implements OnTabChangeListener,OnGestureListener {
private GestureDetector gestureDetector;
private FrameLayout frameLayout;
private CustomTabHost tabHost;
private TabWidget tabWidget;
private static final int FLEEP_DISTANCE = 120;
/** 记录当前分页ID */
private int currentTabID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = (CustomTabHost) findViewById(android.R.id.tabhost);
tabWidget = (TabWidget) findViewById(android.R.id.tabs);
tabHost.setOnTabChangedListener(this);
init();
gestureDetector = new GestureDetector(this);
new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
frameLayout = tabHost.getTabContentView();
}
private void init() {
setIndicator(R.drawable.icon1, 0, new Intent(this,TabActivity01.class));
setIndicator(R.drawable.icon2, 1, new Intent(this,TabActivity02.class));
setIndicator(R.drawable.icon3, 2, new Intent(this,TabActivity03.class));
setIndicator(R.drawable.icon4, 3, new Intent(this,TabActivity04.class));
}
private void setIndicator(int icon, int tabId, Intent intent) {
View localView = LayoutInflater.from(this.tabHost.getContext()).inflate(R.layout.tab, null);
((ImageView) localView.findViewById(R.id.tab_image)).setBackgroundResource(icon);
String str = String.valueOf(tabId);
TabHost.TabSpec localTabSpec = tabHost.newTabSpec(str).setIndicator(localView).setContent(intent);
tabHost.addTab(localTabSpec);
}
@Override
public void onTabChanged(String tabId) {
//tabId值为要切换到的tab页的索引位置
int tabID = Integer.valueOf(tabId);
for (int i = 0; i < tabWidget.getChildCount(); i++) {
if (i == tabID) {
tabWidget.getChildAt(Integer.valueOf(i)).setBackgroundColor(R.color.bule);
} else {
tabWidget.getChildAt(Integer.valueOf(i)).setBackgroundColor(R.color.white);
}
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
if (e1.getX() - e2.getX() <= (-FLEEP_DISTANCE)) {//从左向右滑动
currentTabID = tabHost.getCurrentTab() - 1;
if (currentTabID < 0) {
currentTabID = tabHost.getTabCount() - 1;
}
} else if (e1.getX() - e2.getX() >= FLEEP_DISTANCE) {//从右向左滑动
currentTabID = tabHost.getCurrentTab() + 1;
if (currentTabID >= tabHost.getTabCount()) {
currentTabID = 0;
}
}
tabHost.setCurrentTab(currentTabID);
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Android fleep滑动切换tab(切换带动画).zip项目安卓应用源码下载Android fleep滑动切换tab(切换带动画).zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
资源推荐
资源详情
资源评论
收起资源包目录
Android fleep滑动切换tab(切换带动画).zip (54个子文件)
Android fleep滑动切换tab(切换带动画)
Android fleep滑动切换tab(切换带动画)
1-1210241033010-L.png 153KB
1_121024103441_1.png 112KB
Android fleep滑动切换tab(切换带动画)
fleepTab
AndroidManifest.xml 785B
res
anim
slide_right_in.xml 200B
slide_left_in.xml 199B
slide_right_out.xml 199B
slide_left_out.xml 200B
layout
tab_third.xml 411B
tab_first.xml 399B
tab_second.xml 399B
tab_fourth.xml 400B
tab.xml 734B
main.xml 787B
drawable-ldpi
icon.png 2KB
drawable-mdpi
icon.png 3KB
drawable-hdpi
icon4.png 3KB
first.jpg 152KB
third.jpg 160KB
fourth.jpg 88KB
icon2.png 4KB
icon1.png 3KB
icon3.png 4KB
icon.png 4KB
second.jpg 197KB
values
color.xml 178B
strings.xml 169B
src
com
caigang
test
MainActivity.java 4KB
CustomTabHost.java 2KB
TabActivity01.java 289B
TabActivity04.java 290B
TabActivity03.java 289B
TabActivity02.java 290B
bin
com
caigang
test
MainActivity$1.class 1KB
TabActivity02.class 516B
R$color.class 442B
R.class 607B
CustomTabHost.class 2KB
MainActivity.class 5KB
R$anim.class 506B
R$layout.class 549B
R$attr.class 334B
R$id.class 415B
TabActivity04.class 516B
R$string.class 421B
TabActivity03.class 516B
TabActivity01.class 516B
R$drawable.class 628B
fleepTab.apk 635KB
classes.dex 9KB
resources.ap_ 628KB
.classpath 280B
assets
default.properties 378B
.project 844B
gen
com
caigang
test
R.java 2KB
共 54 条
- 1
资源评论
yxkfw
- 粉丝: 82
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功