【问题标题】:tabhost implement sliding effectstabhost 实现滑动效果
【发布时间】:2014-11-24 11:54:41
【问题描述】:

我尝试使用此解决方案: How to implement TabHost sliding effect?

但不做任何事情,我的意思是一切都保持不变。 我认为实现很简单:

我在这里包含代码解决方案:

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;
    }
}

所以只需创建一个AnimatedTabHostListener 对象并通过setOnTabChangedListener() 方法将其提供给tabHost
我调试并调用了侦听器的方法......但我没有看到任何动画。怎么可能?

【问题讨论】:

    标签: android animation android-tabhost


    【解决方案1】:

    试试这个来自 github https://github.com/jittya/SwipeableTabView的代码

    Java 代码

    LinearLayout SwipableViewTabConntainer=(LinearLayout)findViewById(R.id.SwipableTabViewContainer);
            ArrayList<String> titles=new ArrayList<String>();
            titles.add("One");
            titles.add("Two");
            titles.add("Three");
            titles.add("Four");
            titles.add("Five");
    
    
            ArrayList<Fragment> fragments=new ArrayList<Fragment>();
            for(int i=0;i<titles.size();i++)
            {
                fragments.add(new Dummyfragment(titles.get(i)));
            }
    
            SwipeTabView swipeTabView=new SwipeTabView(this,titles,fragments,getSupportFragmentManager());
            SwipableViewTabConntainer.addView(swipeTabView);
    

    Xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:id="@+id/SwipableTabViewContainer"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent">
    
        </LinearLayout>
    
    
    </LinearLayout>
    

    【讨论】:

    • 我认为它只适用于“FragmentActivity”,我正在片段内工作。关键是在片段中我不能使用 getSupportFragmentManager(),在 FragmentActivity 中是...
    • 它也适用于片段,但活动应该是片段活动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2013-05-16
    相关资源
    最近更新 更多