带有更改选项卡的TabHost动画

时间:2015-06-29 03:41:51

标签: java android listview animation

我试图在标签更改时添加动画,我有一个列表视图作为我的标签,现在我有动画工作。使用这个

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

这个

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

基于以下帖子:

Android - TabActivity with Transition animation

但是在我的列表视图中,我有一个标题,我希望标题保持不变,只是更改列表,我怎么能改变它?

1 个答案:

答案 0 :(得分:0)

您只需拨打findViewById()getTabHost().getCurrentView()方法即可找到ListView并为其设置动画。

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