您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 可以滑动界面切换tab
  所属分类: Android
  开发工具:
  文件大小: 1mb
  下载次数: 0
  上传时间: 2011-11-29
  提 供 者: lihong******
 详细说明: 可以滑动的界面切换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 exte nds 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; } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: tabhost
 输入关键字,在本站1000多万海量源码库中尽情搜索: