`
helloandroid
  • 浏览: 272696 次
  • 性别: Icon_minigender_1
  • 来自: 成都
博客专栏
107f8db3-b009-3b79-938a-dafddb49ea79
Android腾讯微博客户...
浏览量:94578
社区版块
存档分类
最新评论

fleep滑动切换tab(切换带动画)

阅读更多
从右向左滑动,tab页切换后的效果




主要代码1:继承TabHost覆写setCurrentTab(int index)方法
@Override
	public void setCurrentTab(int index) {
		//index为要切换到的tab页索引,currentTabIndex为现在要当前tab页的索引
		int currentTabIndex = getCurrentTab();
		
		//设置当前tab页退出时的动画
		if (null != getCurrentView()){//第一次进入MainActivity时,getCurrentView()取得的值为空
			if (currentTabIndex == (tabCount - 1) && index == 0) {//处理边界滑动
				getCurrentView().startAnimation(slideLeftOut);
			} else if (currentTabIndex == 0 && index == (tabCount - 1)) {//处理边界滑动
				getCurrentView().startAnimation(slideRightOut);
			} else if (index > currentTabIndex) {//非边界情况下从右往左fleep
				getCurrentView().startAnimation(slideLeftOut);
			} else if (index < currentTabIndex) {//非边界情况下从左往右fleep
				getCurrentView().startAnimation(slideRightOut);
			}
		}
		
		super.setCurrentTab(index);
		
		//设置即将显示的tab页的动画
		if (currentTabIndex == (tabCount - 1) && index == 0){//处理边界滑动
			getCurrentView().startAnimation(slideLeftIn);
		} else if (currentTabIndex == 0 && index == (tabCount - 1)) {//处理边界滑动
			getCurrentView().startAnimation(slideRightIn);
		} else if (index > currentTabIndex) {//非边界情况下从右往左fleep
			getCurrentView().startAnimation(slideLeftIn);
		} else if (index < currentTabIndex) {//非边界情况下从左往右fleep
			getCurrentView().startAnimation(slideRightIn);
		}
	}


2:实现OnGestureListener接口,覆写onFling()方法
@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;
	}
  • 大小: 115.5 KB
  • 大小: 99.8 KB
分享到:
评论
1 楼 u010675012 2014-09-11  
楼主,你的这个源码根本运行不了啊,或报错的
09-11 09:49:12.857: E/AndroidRuntime(763): at android.widget.TabWidget.onFocusChange(TabWidget.java:524)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.view.View.onFocusChanged(View.java:4589)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.view.View.handleFocusGainInternal(View.java:4377)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.view.ViewGroup.handleFocusGainInternal(ViewGroup.java:562)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.view.View.requestFocusNoSearch(View.java:6665)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.view.View.requestFocus(View.java:6644)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.view.ViewGroup.requestFocus(ViewGroup.java:2326)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.view.View.requestFocus(View.java:6611)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.view.View.requestFocus(View.java:6590)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.widget.TabWidget.focusCurrentTab(TabWidget.java:461)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.widget.TabHost.setCurrentTab(TabHost.java:410)
09-11 09:49:12.857: E/AndroidRuntime(763): at com.caigang.test.CustomTabHost.setCurrentTab(CustomTabHost.java:56)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.widget.TabHost.addTab(TabHost.java:240)
09-11 09:49:12.857: E/AndroidRuntime(763): at com.caigang.test.CustomTabHost.addTab(CustomTabHost.java:35)
09-11 09:49:12.857: E/AndroidRuntime(763): at com.caigang.test.MainActivity.setIndicator(MainActivity.java:63)
09-11 09:49:12.857: E/AndroidRuntime(763): at com.caigang.test.MainActivity.init(MainActivity.java:52)
09-11 09:49:12.857: E/AndroidRuntime(763): at com.caigang.test.MainActivity.onCreate(MainActivity.java:37)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.app.Activity.performCreate(Activity.java:5104)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-11 09:49:12.857: E/AndroidRuntime(763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

相关推荐

Global site tag (gtag.js) - Google Analytics