2 a1388463837 a1388463837 于 2013.01.16 10:03 提问

TabHost滑动效果怎么实现?

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

26个回答

gamedragon
gamedragon   2013.01.16 10:08
已采纳

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

a1388463837
a1388463837 用ViewPager要怎么才能做到tab标签的滑动效果呢?事实上我最需要的是标签的滑动效果,而不是页面上的。
大约 5 年之前 回复
gamedragon
gamedragon SDK11主要是ActionBar的原因,仅仅VeiwPager是不需要SDK11的,那个是support.v4里面的。
大约 5 年之前 回复
a1388463837
a1388463837 要SDK11才可以的话就不怎么符合需求了,不过还是谢谢
大约 5 年之前 回复
gamedragon
gamedragon 用Eclipse,记得把SDK和ADT都升级到最新,然后New Android Project,跟着一步一步做,记得Minimal Required SDK要是11才行,然后BlankActivity,Navigation Type选Tabs + Swipe,出来就是你要的效果。
大约 5 年之前 回复
a1388463837
a1388463837 通过ADT向导能做出来?这个怎么搞?
大约 5 年之前 回复
z13759561330
z13759561330   2014.11.18 21:06
z13759561330
z13759561330   2014.11.18 21:06
a576699534
a576699534   2015.02.03 23:48
curie_87
curie_87   2013.01.16 15: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;
    }
}
a1388463837
a1388463837 请问这一段代码要怎么用呢?绑定在一个TabHost上作为他的监听器?还是说这个是一个自定义控件?可以直接当控件使用?
大约 5 年之前 回复
yongyong_21
yongyong_21   2013.01.16 15:52

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

a1388463837
a1388463837 用ViewPager要怎么实现呢?找了一下资料,没有看出个所以然,上网找了一些小demo,但是好像很多都是页面上实现了滑动,但是tab标签却没有
大约 5 年之前 回复
keyanxiaobai
keyanxiaobai   2014.11.18 10:24

actionbar+fragment

zpj779878443
zpj779878443   2014.11.18 13:36

viewpager+fragment,或者直接用viewpager

Y406972058
Y406972058   2014.11.18 15:44

手势 touch 以及viewpager 解决的办法很多呀!!

Y406972058
Y406972058   2014.11.18 15:44

手势 touch 以及viewpager 解决的办法很多呀!!

共26条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!