a1388463837
琴弦欲奏
2013-01-16 02:03
采纳率: 71.4%
浏览 12.7k

TabHost滑动效果怎么实现?

我需要弄几个分页,就是TabHost的形式的。但是在切换选项页面的时候TabHost标签栏中的标签与标签的切换带有滑动的动画效果,最好是页面也有滑动效果。我想知道这样的效果是怎么做的?用Tabhost?ActivityGround?还是其他什么方法?要怎么实现?最好有demo可以看一下,谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

17条回答 默认 最新

  • gamedragon
    gamedragon 2013-01-16 02:08
    已采纳

    用ViewPager,用Eclipse里ADT的向导就能做出来,当然不是基于TabHost的,是基于ActionBar的,不过原理是一样的。

    点赞 评论
  • curie_87
    Curie-87 2013-01-16 07:49

    试试这段代码:

    import android.view.View;
        import android.view.animation.AccelerateInterpolator;
        import android.view.animation.Animation;
        import android.view.animation.TranslateAnimation;
        import android.widget.TabHost;
        import android.widget.TabHost.OnTabChangeListener;
    
    /**
     * A custom OnTabChangeListener that uses the TabHost its related to to fetch information about the current and previous
     * tabs. It uses this information to perform some custom animations that slide the tabs in and out from left and right.
     * 
     * @author Daniel Kvist
     * 
     */
    public class AnimatedTabHostListener implements OnTabChangeListener
    {
    
        private static final int ANIMATION_TIME = 240;
        private TabHost tabHost;
        private View previousView;
        private View currentView;
        private int currentTab;
    
        /**
         * Constructor that takes the TabHost as a parameter and sets previousView to the currentView at instantiation
         * 
         * @param tabHost
         */
        public AnimatedTabHostListener(TabHost tabHost)
        {
            this.tabHost = tabHost;
            this.previousView = tabHost.getCurrentView();
        }
    
        /**
         * When tabs change we fetch the current view that we are animating to and animate it and the previous view in the
         * appropriate directions.
         */
        @Override
        public void onTabChanged(String tabId)
        {
    
            currentView = tabHost.getCurrentView();
            if (tabHost.getCurrentTab() > currentTab)
            {
                previousView.setAnimation(outToLeftAnimation());
                currentView.setAnimation(inFromRightAnimation());
            }
            else
            {
                previousView.setAnimation(outToRightAnimation());
                currentView.setAnimation(inFromLeftAnimation());
            }
            previousView = currentView;
            currentTab = tabHost.getCurrentTab();
    
        }
    
        /**
         * Custom animation that animates in from right
         * 
         * @return Animation the Animation object
         */
        private Animation inFromRightAnimation()
        {
            Animation inFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
            return setProperties(inFromRight);
        }
    
        /**
         * Custom animation that animates out to the right
         * 
         * @return Animation the Animation object
         */
        private Animation outToRightAnimation()
        {
            Animation outToRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
            return setProperties(outToRight);
        }
    
        /**
         * Custom animation that animates in from left
         * 
         * @return Animation the Animation object
         */
        private Animation inFromLeftAnimation()
        {
            Animation inFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
            return setProperties(inFromLeft);
        }
    
        /**
         * Custom animation that animates out to the left
         * 
         * @return Animation the Animation object
         */
        private Animation outToLeftAnimation()
        {
            Animation outtoLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
            return setProperties(outtoLeft);
        }
    
        /**
         * Helper method that sets some common properties
         * @param animation the animation to give common properties
         * @return the animation with common properties
         */
        private Animation setProperties(Animation animation)
        {
            animation.setDuration(ANIMATION_TIME);
            animation.setInterpolator(new AccelerateInterpolator());
            return animation;
        }
    }
    
    点赞 评论
  • yongyong_21
    yongyong_21 2013-01-16 07:52

    你可以通过使用 ViewPager和 PageIndicator 来实现。
    为和旧的Android版本兼容,或者有更多其它选择。使用ViewPagerIndicator。请参考:https://github.com/JakeWharton/Android-ViewPagerIndicator/

    点赞 评论
  • keyanxiaobai
    keyanxiaobai 2014-11-18 02:24

    actionbar+fragment

    点赞 评论
  • zpj779878443
    coder-pig 2014-11-18 05:36

    viewpager+fragment,或者直接用viewpager

    点赞 评论
  • z13759561330
    z13759561330 2014-11-18 13:07
    点赞 评论
  • linzhiying111
    linzhiying111 2014-11-19 01:22

    要想实现滑动效果,最好不要用TabHost了,这个组件已经慢慢的被淘汰了,建议你使用ViewPager

    点赞 评论
  • xdwyyan
    loaLakers 2014-11-19 08:43
    点赞 评论
  • xiongchengcc
    xiongchengcc 2014-11-25 01:21

    利用nineold包结合你滑动的距离可以实现滑动式他tabhost

    点赞 评论
  • u014228527
    技术力量猛于虎 2014-11-27 02:13
    点赞 评论
  • dj714330240
    dj714330240 2014-11-27 02:47

    用Eclipse,记得把SDK和ADT都升级到最新,然后New Android Project,跟着一步一步做,记得Minimal Required SDK要是11才行,然后BlankActivity,Navigation Type选Tabs + Swipe,出来就是你要的效果。

    点赞 评论
  • u012131702
    欧拉啊旺 2014-11-30 05:19

    好像,都是有ViewPage实现的。因为它有滑动效果……;

    而且ViewPager的滑动是这样实现的:
    public interface PageTransformer {
    /**
    * Apply a property transformation to the given page.
    *
    * @param page Apply the transformation to this page
    * @param position Position of page relative to the current front-and-center
    * position of the pager. 0 is front and center. 1 is one full
    * page position to the right, and -1 is one page position to the left.
    */
    public void transformPage(View page, float position);
    }

    点赞 评论
  • u010794180
    Android之禅 2014-12-06 15:51

    给你我的博客链接:主流移动开发框架(2)——fragment+fragmenttabhost实现底部菜单可滑动。

    http://blog.csdn.net/u010794180/article/details/41621003

    绝对是你想要的效果。附有demo给你下载。

    点赞 评论
  • lxq_xsyu
    lxq_xsyu 2014-12-10 12:04

    你可以参考一下我的自定义组件系列【3】希望对你有帮助。 http://blog.csdn.net/dawanganban/article/details/24007215

    点赞 评论
  • lovemichael2
    lovemichael2 2014-12-22 00:48

    用TabHost应该是可以实现的

    点赞 评论
  • chencheng_7758
    走啊丶去拯救世界 2014-12-24 02:44

    用Eclipse,记得把SDK和ADT都升级到最新,然后New Android Project,跟着一步一步做,记得Minimal Required SDK要是11才行,然后BlankActivity,Navigation Type选Tabs + Swipe,出来就是你要的效果。

    点赞 评论
  • a576699534
    骚然勿外 2015-02-03 15:48
    点赞 评论

相关推荐