colorAnimation中无法访问的语句

时间:2017-11-16 13:35:04

标签: java android android-fragments

在开始之前,我只是想让你知道我是Android编程的新手,所以这个问题实际上可能有点傻。
在我的mainActivity.java文件中,我使用3个viewPager片段获得了标签式布局:fragment1fragment2fragment3
我希望当第二个片段进入时,窗口的背景颜色会平滑变化,为此我从颜色动画中获得了一个valueAnimator:

public class fragment2 extends Fragment {
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragmentlayout2, null);

        int colorFrom = getResources().getColor(R.color.white);
        int colorTo = getResources().getColor(R.color.colorFrame2);
        ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
        colorAnimation.setDuration(250); // milliseconds
        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                getView().findViewById(R.layout.fragmentlayout2).setBackgroundColor((int) animator.getAnimatedValue());

            }

        });
        colorAnimation.start();
    }
}

当我尝试运行程序时,它会返回有关colorFrom语句的unreachable statement错误。我试过解决它,但它不会消失!任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

return的末尾移动onCreateView,因为return会发送回控件而不会更进一步

 @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        int colorFrom = getResources().getColor(R.color.white);
        int colorTo = getResources().getColor(R.color.colorFrame2);
        ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
        colorAnimation.setDuration(250); // milliseconds
        colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                getView().findViewById(R.layout.fragmentlayout2).setBackgroundColor((int) animator.getAnimatedValue());

            }

        });
        colorAnimation.start();
    return inflater.inflate(R.layout.fragmentlayout2, null);
    // ^^^^^^^^
    }