android 左右滑动切换TAB

  • X1_427836
    了解作者
  • 98.5KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-19 06:19
    上传日期
android TAB 屏幕滑动实现tab切换
TabSlide.rar
内容介绍
package com.demo.test; import com.demo.test.R; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.TabHost; import android.widget.TextView; import android.widget.Toast; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabWidget; public class MainActivity extends TabActivity implements OnTabChangeListener,OnGestureListener { private GestureDetector gestureDetector; private CustomTabHost tabHost; private TabWidget tabWidget; private boolean isOut = false; 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); createTab1(); createTab2(); createTab3(); createTab4(); gestureDetector = new GestureDetector(this); new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return false; } }; } public void createTab1() { TabHost.TabSpec localTabSpec = this.tabHost.newTabSpec("0"); View localView = getLayoutInflater().inflate(R.layout.tab_indicator, null); ImageView localImageView = (ImageView) localView.findViewById(R.id.icon); TextView localTextView = (TextView) localView.findViewById(R.id.title); localImageView.setImageResource(R.drawable.concern); localTextView.setText("开机加速"); Intent localIntent = new Intent(this, Tab1Activity.class); localTabSpec.setIndicator(localView).setContent(localIntent); this.tabHost.addTab(localTabSpec); } private void createTab2() { TabHost.TabSpec localTabSpec1 = this.tabHost.newTabSpec("1"); View localView = getLayoutInflater().inflate(R.layout.tab_indicator, null); ImageView localImageView = (ImageView) localView.findViewById(R.id.icon); TextView localTextView = (TextView) localView.findViewById(R.id.title); localImageView.setImageResource(R.drawable.lastest); localTextView.setText("缓存清理"); TabHost.TabSpec localTabSpec2 = localTabSpec1.setIndicator(localView); Intent localIntent = new Intent(this, Tab2Activity.class); localTabSpec2.setContent(localIntent); this.tabHost.addTab(localTabSpec1); } private void createTab3() { TabHost.TabSpec localTabSpec1 = this.tabHost.newTabSpec("2"); View localView = getLayoutInflater().inflate(R.layout.tab_indicator, null); ImageView localImageView = (ImageView) localView.findViewById(R.id.icon); TextView localTextView = (TextView) localView.findViewById(R.id.title); localImageView.setImageResource(R.drawable.concessions); localTextView.setText("进程管理"); TabHost.TabSpec localTabSpec2 = localTabSpec1.setIndicator(localView); Intent localIntent = new Intent(this, Tab3Activity.class); localTabSpec2.setContent(localIntent); this.tabHost.addTab(localTabSpec1); } private void createTab4() { TabHost.TabSpec localTabSpec1 = this.tabHost.newTabSpec("3"); View localView = getLayoutInflater().inflate(R.layout.tab_indicator, null); ImageView localImageView = (ImageView) localView .findViewById(R.id.icon); TextView localTextView = (TextView) localView.findViewById(R.id.title); localImageView.setImageResource(R.drawable.function); localTextView.setText("节电管理"); TabHost.TabSpec localTabSpec2 = localTabSpec1.setIndicator(localView); Intent localIntent = new Intent(this, Tab4Activity.class); localTabSpec2.setContent(localIntent); this.tabHost.addTab(localTabSpec1); } @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)); } else { tabWidget.getChildAt(Integer.valueOf(i)); } } } @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学习
      Android学习Android学习Android学习Android学习Android学习Android学习
    • Android678
      Android678Android678Android678Android678Android678Android678Android678Android678Android678Android678
    • android 例子
      android 例子android 例子android 例子android 例子android 例子
    • android图片
      android精美小图片android精美小图片android精美小图片android精美小图片
    • android课件
      清华老师上课的android课件。ch_01android概述ch_02android开发基础ch_03资源的访问ch_04android用户界面ch_05Ativity和Intent。其他自己看。
    • AndroidKiller
      安卓App反编译工具AndroidKiller\AndroidKiller\AndroidKiller
    • AndroidSample
      Android示例 这是一个示例Android应用,用于降低Google Play服务和Android Volley的使用率。 有关更多详细信息,请参阅以下链接: Google Play服务: Android凌空抽射:
    • android source
      android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source
    • Android 教程
      Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android ...
    • android资料
      Android程序员指南 Android_UI开发专题 Android程序开发初级教程 android的五大类各种常用类的资料整理 Android高手进阶教程