android让你的TabHost滑动起来

简介:

在Android应用中,一般TabActivity和若干个Tab选项卡(TabWidget)。如果选项卡的数量超过了5个,就不适合放到一个屏幕中,这样可以让这些选项卡滑动起来。

滑动的选项卡的实现有好几种方式,在这些方式中,最简单也是我最满意的还是在原生的TabActivity上修改,将上面的选项卡改为可滑动的状态。这样既有新的滑动的效果,也保留了原有TabActivity的各项功能。

实现Tab可滑动基本的思路就是把上面的TabWidget放到一个HorizontalScrollView中,让TabWidget滑动起来。不过如果仅仅修改XML还是会产生问题,就是没有办法控制每个选项卡的宽度。所以还是需要在程序中设置每个选项卡的宽度。例如:

[java]  view plain copy
  1. // 设置窗口的宽度   
  2. DisplayMetrics dm = new DisplayMetrics();   
  3. getWindowManager().getDefaultDisplay().getMetrics(dm);   
  4. int screenWidth = dm.widthPixels;   
  5. if (count < 4) {   
  6.     for (int i = 0; i < count; i++) {   
  7.         // 设置每个选项卡的宽度   
  8.         tabWidget.getChildTabViewAt(i).setMinimumWidth(screenWidth / 4);   
  9.     }   
  10. }  


自定义TabActivity主界面的XML:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">   
  5.     <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"  
  6.         android:layout_height="fill_parent">   
  7.         <LinearLayout android:orientation="vertical"  
  8.             android:layout_width="fill_parent" android:layout_height="fill_parent">   
  9.             <HorizontalScrollView android:layout_height="wrap_content"  
  10.                 android:layout_width="fill_parent" android:scrollbars="none">   
  11.                 <TabWidget android:id="@android:id/tabs"  
  12.                     android:layout_width="fill_parent" android:layout_height="60dp" />   
  13.             </HorizontalScrollView>   
  14.             <FrameLayout android:id="@android:id/tabcontent"  
  15.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  16.                 android:layout_weight="1" />   
  17.         </LinearLayout>   
  18.     </TabHost>   
  19. </LinearLayout>  

效果图如下


上面的tab是可以滑动的,屏幕左右滑动,tab也会切换


[java]  view plain copy
  1. package com.xu81.testflip;  
  2.   
  3. import java.util.Vector;  
  4.   
  5. import android.content.Context;  
  6. import android.util.AttributeSet;  
  7. import android.view.MotionEvent;  
  8. import android.view.VelocityTracker;  
  9. import android.view.View;  
  10. import android.view.ViewConfiguration;  
  11. import android.view.ViewGroup;  
  12. import android.widget.Scroller;  
  13.   
  14. public class ScrollLayout extends ViewGroup {  
  15.   
  16.     private Scroller mScroller;  
  17.     private VelocityTracker mVelocityTracker;  
  18.     private int mCurScreen;  
  19.     private int mDefaultScreen = 0;  
  20.     private static final int TOUCH_STATE_REST = 0;  
  21.     private static final int TOUCH_STATE_SCROLLING = 1;  
  22.     private static final int SNAP_VELOCITY = 500;  
  23.     private int mTouchState = TOUCH_STATE_REST;  
  24.     private int mTouchSlop;  
  25.     private float mLastMotionX;  
  26.     private int sensitivity = 30;  
  27.     private boolean spring;  
  28.     private Vector<LayoutChangeListener> listeners;  
  29.   
  30.     public ScrollLayout(Context context, AttributeSet attrs) {  
  31.         super(context, attrs);  
  32.         // TODO Auto-generated constructor stub  
  33.         mScroller = new Scroller(context);  
  34.         mCurScreen = mDefaultScreen;  
  35.         mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();  
  36.         listeners = new Vector<LayoutChangeListener>();  
  37.     }  
  38.   
  39.     public void addChangeListener(LayoutChangeListener listener) {  
  40.         listeners.add(listener);  
  41.     }  
  42.   
  43.     @Override  
  44.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  45.         // TODO Auto-generated method stub  
  46.         int childLeft = 0;  
  47.         final int childCount = getChildCount();  
  48.         for (int i = 0; i < childCount; i++) {  
  49.             final View childView = getChildAt(i);  
  50.             if (childView.getVisibility() != View.GONE) {  
  51.                 final int childWidth = childView.getMeasuredWidth();  
  52.                 childView.layout(childLeft, 0, childLeft + childWidth,  
  53.                         childView.getMeasuredHeight());  
  54.                 childLeft += childWidth;  
  55.             }  
  56.         }  
  57.     }  
  58.   
  59.     @Override  
  60.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  61.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  62.         final int width = MeasureSpec.getSize(widthMeasureSpec);  
  63.         final int widthMode = MeasureSpec.getMode(widthMeasureSpec);  
  64.         if (widthMode != MeasureSpec.EXACTLY) {  
  65.             throw new IllegalStateException(  
  66.                     "ScrollLayout only canmCurScreen run at EXACTLY mode!");  
  67.         }  
  68.   
  69.         final int heightMode = MeasureSpec.getMode(heightMeasureSpec);  
  70.         if (heightMode != MeasureSpec.EXACTLY) {  
  71.             throw new IllegalStateException(  
  72.                     "ScrollLayout only can run at EXACTLY mode!");  
  73.         }  
  74.   
  75.         // The children are given the same width and height as the scrollLayout  
  76.         final int count = getChildCount();  
  77.         for (int i = 0; i < count; i++) {  
  78.             getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);  
  79.         }  
  80.         scrollTo(mCurScreen * width, 0);  
  81.     }  
  82.   
  83.     public void snapToDestination() {  
  84.         final int screenWidth = getWidth();  
  85.         final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;  
  86.         snapToScreen(destScreen);  
  87.     }  
  88.   
  89.     public void snapToScreen(int whichScreen) {  
  90.         // get the valid layout page  
  91.         int lastIndex = mCurScreen;  
  92.         whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));  
  93.         if (getScrollX() != (whichScreen * getWidth())) {  
  94.   
  95.             final int delta = whichScreen * getWidth() - getScrollX();  
  96.             mScroller.startScroll(getScrollX(), 0, delta, 0,  
  97.                     Math.abs(delta) * 2);  
  98.             mCurScreen = whichScreen;  
  99.             invalidate(); // Redraw the layout  
  100.         }  
  101.         for (LayoutChangeListener listener : listeners)  
  102.             listener.doChange(lastIndex, whichScreen);  
  103.     }  
  104.   
  105.     public void setToScreen(int whichScreen) {  
  106.         whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));  
  107.         mCurScreen = whichScreen;  
  108.         scrollTo(whichScreen * getWidth(), 0);  
  109.     }  
  110.   
  111.     public int getCurScreen() {  
  112.         return mCurScreen;  
  113.     }  
  114.   
  115.     @Override  
  116.     public void computeScroll() {  
  117.         // TODO Auto-generated method stub  
  118.         if (mScroller.computeScrollOffset()) {  
  119.             scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  
  120.             postInvalidate();  
  121.         }  
  122.     }  
  123.   
  124.     public boolean isSpring() {  
  125.         return spring;  
  126.     }  
  127.   
  128.     public void setSpring(boolean spring) {  
  129.         this.spring = spring;  
  130.     }  
  131.   
  132.     @Override  
  133.     public boolean onTouchEvent(MotionEvent event) {  
  134.         // TODO Auto-generated method stub  
  135.         if (mVelocityTracker == null)  
  136.             mVelocityTracker = VelocityTracker.obtain();  
  137.         mVelocityTracker.addMovement(event);  
  138.         final int action = event.getAction();  
  139.         final float x = event.getX();  
  140.         switch (action) {  
  141.         case MotionEvent.ACTION_DOWN:  
  142.             if (!mScroller.isFinished())  
  143.                 mScroller.abortAnimation();  
  144.             mLastMotionX = x;  
  145.             break;  
  146.         case MotionEvent.ACTION_MOVE:  
  147.             int deltaX = (int) (mLastMotionX - x);  
  148.             if (Math.abs(deltaX) > sensitivity) {  
  149.                 // 左滑动为正数、右为负数  
  150.                 if (spring) {  
  151.                     scrollBy(deltaX, 0);  
  152.                     mLastMotionX = x;  
  153.                 } else {  
  154.                     final int childCount = getChildCount();  
  155.                     boolean max = mCurScreen < childCount - 1;  
  156.                     boolean min = mCurScreen > 0;  
  157.                     boolean canMove = deltaX > 0 ? (max ? true : false)  
  158.                             : (min ? true : false);  
  159.                     if (canMove) {  
  160.                         scrollBy(deltaX, 0);  
  161.                         mLastMotionX = x;  
  162.                     }  
  163.                 }  
  164.             }  
  165.             break;  
  166.         case MotionEvent.ACTION_UP:  
  167.             final VelocityTracker velocityTracker = mVelocityTracker;  
  168.             velocityTracker.computeCurrentVelocity(1000);  
  169.             int velocityX = (int) velocityTracker.getXVelocity();  
  170.             if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {  
  171.                 // Fling enough to move left  
  172.                 snapToScreen(mCurScreen - 1);  
  173.             } else if (velocityX < -SNAP_VELOCITY  
  174.                     && mCurScreen < getChildCount() - 1) {  
  175.                 // Fling enough to move right  
  176.                 snapToScreen(mCurScreen + 1);  
  177.             } else {  
  178.                 snapToDestination();  
  179.             }  
  180.             if (mVelocityTracker != null) {  
  181.                 mVelocityTracker.recycle();  
  182.                 mVelocityTracker = null;  
  183.             }  
  184.             mTouchState = TOUCH_STATE_REST;  
  185.             break;  
  186.         case MotionEvent.ACTION_CANCEL:  
  187.             mTouchState = TOUCH_STATE_REST;  
  188.             break;  
  189.         }  
  190.         return true;  
  191.     }  
  192.   
  193.     @Override  
  194.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  195.         // TODO Auto-generated method stub  
  196.         final int action = ev.getAction();  
  197.         if ((action == MotionEvent.ACTION_MOVE)  
  198.                 && (mTouchState != TOUCH_STATE_REST))  
  199.             return true;  
  200.         final float x = ev.getX();  
  201.         switch (action) {  
  202.         case MotionEvent.ACTION_MOVE:  
  203.             final int xDiff = (int) Math.abs(mLastMotionX - x);  
  204.             if (xDiff > mTouchSlop)  
  205.                 mTouchState = TOUCH_STATE_SCROLLING;  
  206.             break;  
  207.         case MotionEvent.ACTION_DOWN:  
  208.             mLastMotionX = x;  
  209.             mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST  
  210.                     : TOUCH_STATE_SCROLLING;  
  211.             break;  
  212.         case MotionEvent.ACTION_CANCEL:  
  213.         case MotionEvent.ACTION_UP:  
  214.             mTouchState = TOUCH_STATE_REST;  
  215.             break;  
  216.         }  
  217.         return mTouchState != TOUCH_STATE_REST;  
  218.     }  
  219.   
  220. }  



这是模仿qq的滑动实现,和tab不是一个思路,也比较美观实用可以借鉴

源码下载

ScrollTab例子下载

qq滑动特效下载

相关文章
|
3月前
|
JavaScript Android开发
使用贝叶斯曲线滑动安卓屏幕(autojsPro7)
使用贝叶斯曲线滑动安卓屏幕(autojsPro7)
63 0
|
3月前
|
Android开发 Kotlin 索引
Android Compose——ScrollableTabRow和LazyColumn同步滑动
Android Compose——ScrollableTabRow和LazyColumn同步滑动
|
8月前
|
移动开发 Android开发
h5滑动底部兼容安卓
h5滑动底部兼容安卓
59 0
|
4月前
|
XML Java Android开发
Android App手势冲突处理中上下左右滑动的处理以及侧滑边缘菜单的讲解及实战(附源码 可直接使用)
Android App手势冲突处理中上下左右滑动的处理以及侧滑边缘菜单的讲解及实战(附源码 可直接使用)
67 0
|
7月前
|
XML Java Android开发
Android 仿抖音直播滑动清屏,完美解决滑动冲突
Android 仿抖音直播滑动清屏,完美解决滑动冲突
|
8月前
|
Android开发
Android 中ViewPager嵌套RecyclerView出现滑动冲突的解决方案
Android 中ViewPager嵌套RecyclerView出现滑动冲突的解决方案
703 0
|
9月前
|
Android开发
11-appium-滑动元素到视图中-Android
11-appium-滑动元素到视图中-Android
|
10月前
|
Android开发
Android自定义支持滑动监听View
Android自定义支持滑动监听View
335 0
|
API Android开发
Android View滑动相关的基础知识点
*本文涉及到的知识点:MotionEvent、ViewConfiguration、VelocityTracker 、GestureDetector、scrollTo、scrollBy、Scroller、OverScroller*
|
XML Android开发 数据格式
Android仿淘宝、京东Banner滑动查看图文详情
本文基于 `ViewPager2` 实现的 `Banner` 效果,进而实现了仿淘宝、京东`Banner`滑动至最后一页时继续滑动来查看图文详情的效果。
174 0