可以滑动界面切换tab

  • s8_734428
    了解作者
  • 1.8MB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-10 09:16
    上传日期
可以滑动的界面切换tabhost 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; } }
fleepTab.rar
内容介绍
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; } }
评论
    相关推荐
    • Beginning Android
      本人收集的android的英文电子书(pdf)目录: 全部在本人资料中能下载 Android.Application.Development.pdf Android Essentials.pdf Android programmer's guide Beginning Android-Apress.pdf pro-android.pdf ...
    • android Droidex
      Droidex可以将开发者附加的Android设备的屏幕内容复制到你的开发设备屏幕上,通常,如果你使用DroidEx来进行演示可谓是事半功倍,因为你可以直接将一台Android设备与笔记本或投影相连,而客户也可以通过这些设备来...
    • Android播放器
      Android自定义全屏VideoView,仿主流视频客户端,左右滑屏快退快进;上下滑屏,手指在左半屏时,调整亮度;上下滑屏,手指在右半屏时,调整音量;横竖屏无缝自动切换。
    • Android APK IDE
      APK改之理(APK IDE)是一款可视化的、易用的、快捷的、一体化的安卓APK修改工具,集成了ApkTool、Dex2jar、JD-GUI等Apk修改工具,集Apk反编译、Apk查壳、加密解密、Apk调试分析、Apk打包、Apk签名,支持语法高亮的...
    • appmark:行动Android IDE
      AppMark 伙计们,由于知道Kotlin并且无法在Android上移植真实... 免费的开源移动android IDE 特征: DesignLite( ) 便捷的代码编辑器 视觉字符串编辑器 视觉布局编辑器 APK编译器 Gradle(很快) 提示(很快)
    • android service demo
      android开发中sercie的简单事例 package com.example.servicedemoactivity; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class ...
    • JRebel-android-ide-studio_crack_2.0.30
      这是最新版JRebel,版本是2.0.30。 支持android ide studio2.0,内附简明安装、激活以及使用教程。该工具仅用于学习交流。
    • AndroidAuto
      android auto开发官方的文档,确认以下这些对你有用再下载: SDK Contents The SDK is distributed as Android Pre-Release Software pursuant to the Android Automotive Pre-Release Software License Agreement. ...
    • Android_Studio IDE 使用配置
      Android_Studio IDE 使用配置 包含有市面上常用配置4份,个人配置1份 并且提供文档说明,可自行扩展。
    • AndroidProject
      AndroidProject Learning Android Programming and Android Projects SimpleCalc/ 下为实现的基于Activity跳转和进行数据交换与通信的简单计算器app。 是为《智能手机编程》 智能手机编程课程组 编写 中央广播电视...