2015-06-29 123 views
0

我試圖添加動畫時,選項卡更改,我有一個列表視圖作爲我的選項卡,現在我有動畫工作。使用這種TabHost動畫與更改選項卡

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

但隨着我的名單來看,我有它一個頭,和我想要的頭留下來,只是列表更改,我怎麼能改變這個?

回答

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