如何在监听器中使用上下文? [英] How to use context inside listener?

查看:200
本文介绍了如何在监听器中使用上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

else if (v == mSettings)
{
    if (disappearView.getVisibility() == View.VISIBLE)
    {
        AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); // start alpha, end alpha
        fadeOutAnimation.setDuration(1000); // time for animation in milliseconds
        fadeOutAnimation.setFillAfter(true); // make the transformation persist
        Animation out = AnimationUtils.makeOutAnimation(this, true);
        disappearView.startAnimation(out);
        disappearView.setVisibility(View.INVISIBLE);
        out.setAnimationListener(new Animation.AnimationListener()
        {
            public void onAnimationEnd(Animation animation)
            {
                disappearView.setVisibility(View.GONE);
                Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
                bannerView.startAnimation(in);
                bannerView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationStart(Animation animation) { }
        });
    }
    else {
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        disappearView.startAnimation(in);
        disappearView.setVisibility(View.VISIBLE);
        bannerView.setVisibility(View.INVISIBLE);
        bannerView.setVisibility(View.GONE);
    }
}

这一切都在animationListener的本节之外进行

It all works apart from this section in the animationListener:

Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);

它想要一个有效的上下文,但是它正在接收animationListener,我应该给它什么上下文,上下文在这里确实使我感到困惑.

It wants a valid context but it is receiving an animationListener, what context do I give it, context really confuses me here.

推荐答案

将其更改为:

Animation in = AnimationUtils.loadAnimation(MyActivityName.this, android.R.anim.fade_in);

它想要扩展上下文的任何类的实例.由于它在匿名内部类内部,因此当您使用this时,是指内部类实例,而不是Activity.我的代码段引用了包装匿名内部类的Activity类.由于活动扩展了上下文,所以这是一个有效的参数.

It wants an instance of any class that extends Context. Since it is inside an anonymous inner class, when you use this you are referring to the inner class instance, and not to your Activity. My snippet refers to the Activity class that wraps the anonymous inner class. Since Activity extends Context, this is a valid argument.

这篇关于如何在监听器中使用上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆