简体   繁体   中英

TabHost Animation with changed tab

I trying to add an animation to when the tabs change, I have a list view as my tabs, and right now I have got animation working. Using this

    getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
     public void onTabChanged(String tabId)
     {
            View currentView = getTabHost().getCurrentView();
            if (getTabHost().getCurrentTab() > currentTab)
            {
                currentView.setAnimation( inFromRightAnimation() );
            }
            else
            {
                currentView.setAnimation( outToRightAnimation() );
            }

            currentTab = getTabHost().getCurrentTab();
     }
});

And this

public 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);
    inFromRight.setDuration(240);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
}

public Animation outToRightAnimation()
{
    Animation outtoLeft = 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);
    outtoLeft.setDuration(240);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
}

Based on the following post:

Android - TabActivity with Transition animation

But with my list view, I have a header on it, and I want the header to stay and just have the list change, how could I change this?

You can simply call findViewById() method of getTabHost().getCurrentView() chain, to find your ListView and animate it.

getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
        View currentView = getTabHost().getCurrentView();
        ListView listView = (ListView) currentView.findViewById(R.id.your_list_id);
        if (getTabHost().getCurrentTab() > currentTab)
        {
            listView.setAnimation( inFromRightAnimation() );
        }
        else
        {
            listView.setAnimation( outToRightAnimation() );
        }

        currentTab = getTabHost().getCurrentTab();
 }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM