实现了分页的滑动效果,做的demo流畅运行

注:貌似支持的样式(控件)有一定的限制,我试过短信的listview页面,暂无法实现滑动效果

java文件:MainActivity.java、Activity1.java、Activity2.java、Activity3.java、Activity4.java

MainActivity.java

package com.example.tabhostmove;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; public class MainActivity extends TabActivity {
private TabHost tabHost; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
} private void init() {
// TODO Auto-generated method stub tabHost = getTabHost();
// 页面1
TabSpec spec1 = tabHost.newTabSpec("1");
spec1.setIndicator("1", getResources().getDrawable(R.drawable.ic_launcher));
Intent intent1 = new Intent(this, Activity1.class);
spec1.setContent(intent1); // 页面2
TabSpec spec2 = tabHost.newTabSpec("2");
spec2.setIndicator("2", getResources().getDrawable(R.drawable.ic_launcher));
Intent intent2 = new Intent(this, Activity2.class);
spec2.setContent(intent2); // 页面3
TabSpec spec3 = tabHost.newTabSpec("3");
spec3.setIndicator("3", getResources().getDrawable(R.drawable.ic_launcher));
Intent intent3 = new Intent(this, Activity3.class);
spec3.setContent(intent3); // 页面4
TabSpec spec4 = tabHost.newTabSpec("4");
spec4.setIndicator("4", getResources().getDrawable(R.drawable.ic_launcher));
Intent intent4 = new Intent(this, Activity4.class);
spec4.setContent(intent4); tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.addTab(spec4); } private GestureDetector detector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() { @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if ((e2.getRawX() - e1.getRawX()) > 80) {
showNext();
return true;
} if ((e1.getRawX() - e2.getRawX()) > 80) {
showPre();
return true;
}
return super.onFling(e1, e2, velocityX, velocityY);
} }); @Override
public boolean onTouchEvent(MotionEvent event) {
detector.onTouchEvent(event);
return super.onTouchEvent(event);
} /**
* 当前页面索引
*/
int i = 0; /**
* 显示下一个页面
*/
protected void showNext() {
// 三元表达式控制3个页面的循环.
//tabHost.setCurrentTab(i = i == 3 ? i = 0 : ++i);
//Log.i("kennet", i + "");
//四个页面的下一个循环
switch(i)
{
case 0:
i++;
tabHost.setCurrentTab(i);
break;
case 1:
i++;
tabHost.setCurrentTab(i);
break;
case 2:
i++;
tabHost.setCurrentTab(i);
break;
case 3:
i=0;
tabHost.setCurrentTab(i);
break; }
} /**
* 显示前一个页面
*/
protected void showPre() {
// 三元表达式控制3个页面的循环.
//tabHost.setCurrentTab(i = i == 0 ? i = 3 : --i);
//四个页面的上一个循环
switch(i)
{
case 0:
i=3;
tabHost.setCurrentTab(i);
break;
case 1:
i--;
tabHost.setCurrentTab(i);
break;
case 2:
i--;
tabHost.setCurrentTab(i);
break;
case 3:
i--;
tabHost.setCurrentTab(i);
break; }
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

xml布局文件:activity_main.xml、activit1.xml、activit2.xml、activit3.xml、activit4.xml

activity_main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout> </TabHost>

注:activity1、2、3、4是测试的页面,随便建几个即可,别忘了在AndroidManifest.xml里注册页面的活动

实现效果:

【Android UI】顶部or底部菜单的循环滑动效果一的更多相关文章

  1. Android应用主界面底部菜单实现

    介绍 现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的  <---我是底部菜单 原理 在很久以前,可以通过TabActivity实现相关功能,自从Fr ...

  2. Xamarin.Android 利用Fragment实现底部菜单

    效果图: 第一步:添加引用 引用 Crosslight.Xamarin.Android.Support.v7.AppCompat 这个包. 第二步:绘制Main和Fragment界面 fg_home. ...

  3. Android自定义控件系列(四)—底部菜单(下)

    转载请注明出处:http://www.cnblogs.com/landptf/p/6290862.html 在app中经常会用到底部菜单的控件,每次都需要写好多代码,今天我们用到了前几篇博客里的控件来 ...

  4. 【Android UI设计与开发】9:滑动菜单栏(一)开源项目SlidingMenu的使用和示例

    一.SlidingMenu简介 相信大家对SlidingMenu都不陌生了,它是一种比较新的设置界面或配置界面的效果,在主界面左滑或者右滑出现设置界面效果,能方便的进行各种操作.很多优秀的应用都采用了 ...

  5. Android中实现整个视图切换的左右滑动效果

    Android中提供了一个Gallary,可以实现图片或者文本的左右滑动效果. 如何让整个视图都能实现左右滑动,达到类似于Gallary的效果呢?可以直接用一个开源的ViewFlow来实现.   项目 ...

  6. 【Android UI设计与开发】10:滑动菜单栏(二)SlidingMenu 动画效果的实现

    其实就是在显示菜单栏时,有个动画的效果.代码比较简单,下面进行说明. 1.效果图如下,手机上查看效果更佳 2.代码实现,这里只讲解动画效果的实现,具体代码可在源代码中查看 <1> 先定义一 ...

  7. Android 怎样实现 焦点图的 无线循环滑动的状态?

    參考网址:http://my.oschina.net/xsk/blog/119167 总体的架构:ViewPgaer 中直接嵌套  IamgeView 方案一:  重写Viewpager 这样有局限性 ...

  8. GitHub上受欢迎的Android UI Library

    GitHub上受欢迎的Android UI Library 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 Vi ...

  9. android UI进阶之用【转】

    android UI进阶之用ViewPager实现欢迎引导页面 摘要: ViewPager需要android-support-v4.jar这个包的支持,来自google提供的一个附加包.大家搜下即可. ...

随机推荐

  1. PHP 的魔术方法及其应用

    PHP中将所有__(两个下划线)开头的类方法作为魔术方法,这方法之所以称为魔术方法是因为其实现的功能就如变魔术一样感觉很神奇.在特定的事件下触发,这真的很酷. **__construct()** 这个 ...

  2. Delphi 的RTTI机制浅探3(超长,很不错)

    转自:http://blog.sina.com.cn/s/blog_53d1e9210100uke4.html 目录========================================== ...

  3. WebRequest请求错误(服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF)

    WebRequest请求错误(服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF)解决办法,天津config文件,增加一个配置如下 <?x ...

  4. VC6下 try catch 在release下的杯具(默认情况下,要加上throw语句catch才不会被优化掉)

    IDE:VC6 今天遇到一个小问题,把我郁闷了好久,××医生的VulEngine不时在wcsstr处发生crash,加了一番强大的参数检查后,再加上了强大的try catch,其实不是很喜欢用try和 ...

  5. MJPhotoBrowser 用法

    一.使用方法:   #import "MJPhotoBrowser.h" #import "MJPhoto.h"   - (void)tapPhoto:(UIT ...

  6. 三个臭皮匠,顶上一个诸葛亮——在Google Ideathon上Design Thinking分享

    4月26日很荣幸的被邀请参加Google Ideathon做Design Thinking的分享. 这次主要分享了Design Thinking的基本方法流程,以及在真实项目的运用.现在整理一下当时选 ...

  7. 递归导致的StackOverflow的分析

    递归在多层次遍历时尤为重要,这里我们不讲递归的实现,来谈谈递归的内存占用情况. 如下代码,当我们运行时很简单,StackOverflowException瞬间抛出:这里确实是“瞬间”出错了,线程堆栈溢 ...

  8. poi 操作Excel 以及大数据量导出

    maven 依赖 (版本必须一致,否则使用SXSSFworkbook 时程序会报错) <dependency> <groupId>org.apache.poi</grou ...

  9. 【数据结构】31、hashmap=》resize 扩容,不测不知道,一测吓一跳

    来来来,今天就跟hashmap杠到底... 不要叫我杠精了,主要是还是被问到hashmap的时候,我并不能很清晰明了得告知这种数据结构到底是一个什么构造,里面细节并不了解 既然这样,我们就把他解析一波 ...

  10. 学习过程中遇到的python内置函数,后续遇到会继续补充进去

    1.python内置函数isinstance(数字,数字类型),判断一个数字的数字类型(int,float,comple).是,返回True,否,返回False2.python内置函数id()可以查看 ...