Tabhost does not call the onTabChanged () method

0

Because when triggering the Tab does not call the method onTabChanged() .

public class MainActivity extends TabActivity {  private TabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Resources res = getResources();
            mTabHost = getTabHost();
            TabHost.TabSpec spec;
            Intent intent;


            //Home
            intent = new Intent(this, HelloActivity.class);
            spec = mTabHost.newTabSpec("novaos")
                            .setIndicator("tab1", res.getDrawable(R.drawable.ic_launcher))
                                            .setContent(intent);
            mTabHost.addTab(spec);

            intent = new Intent(this, HelloActivity.class);
            spec = mTabHost.newTabSpec("novaos")
                            .setIndicator("tab2", res.getDrawable(R.drawable.ic_launcher))
                                            .setContent(intent);
            mTabHost.addTab(spec);
            intent = new Intent(this, HelloActivity.class);
            spec = mTabHost.newTabSpec("novaos")
                            .setIndicator("tab3", res.getDrawable(R.drawable.ic_launcher))
                                            .setContent(intent);
            mTabHost.addTab(spec);


                            for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
                            {
                                mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#333333"));
                            }
                            mTabHost.getTabWidget().setCurrentTab(0);
                      //      mTabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#505050"));



    }

    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
     * @return 
     */
    public void 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.
     */
    public void onTabChanged(String tabId)
    {

         Toast.makeText(getBaseContext(), "Stop!", Toast.LENGTH_LONG).show();
        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;
    }}

So no animation happens. What would be the problem?

    
asked by anonymous 05.06.2014 / 01:39

1 answer

4

According to the Android API 19 documentation, available at this link , the onTabChanged method does not exist in this class. So for this to work you need to register an event listener on the mTabHost object. The code below exemplifies:

mTabHost.setOnTabChangedListener(new OnTabChangeListener()
{
    @Override
    public void onTabChanged(String tabId)
    {
        Toast.makeText(getBaseContext(), "Stop!", Toast.LENGTH_LONG).show();
        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();
    }
});

However, keep in mind that the TabActivity class is obsolete and there are other alternatives to creating tabs on android. Learn more

    
05.06.2014 / 02:16