动画布局刷新交叉渐变

时间:2015-09-12 12:52:15

标签: android android-fragments android-animation

Android noob在这里,我的主要活动中有一个功能可以刷新天气数据;它通过调用两个片段中的函数来实现这一点,这两个片段从Web API中提取新数据。当你点击刷新按钮/更改位置时,我希望两个片段布局交叉淡入淡出,我似乎无法获得animateLayoutChanges =" true"做我期望的事情(当视图设置为View.GONE并返回View.VISIBLE时交叉淡入淡出)。我是按错误的顺序做事吗?

我的代码:

 public void refreshCity(){

        //This block sets references to the fragment layouts and sets them to GONE

        RelativeLayout wfLayout =  (RelativeLayout)findViewById(R.id.fragment_weather);
        LinearLayout ffLayout = (LinearLayout)findViewById(R.id.fragment_forecast);
        wfLayout.setVisibility(View.GONE);
        ffLayout.setVisibility(View.GONE);

        //This block gets references to the fragments themselves and calls the
        //changeCity function in each with the current city - this block definitely works

        FragmentManager fm = getSupportFragmentManager();
        WeatherFragment wf = (WeatherFragment)fm
                .findFragmentByTag(makeFragmentName(R.id.pager, 0));
        ForecastFragment ff = (ForecastFragment)fm
                .findFragmentByTag(makeFragmentName(R.id.pager, 1));
        CityPreference cf = new CityPreference(this);
        wf.changeCity(cf.getCity());
        ff.changeCity(cf.getCity());

        //I then set the layouts back to visible

        wfLayout.setVisibility(View.VISIBLE);
        ffLayout.setVisibility(View.VISIBLE);
    }

片段刷新并显示数据,但没有淡入淡出。两个片段布局中的animateLayoutChanges都设置为true,是否有一些保护用于引用它们引用的片段之外的布局?非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

所以我提出的解决方案;我使用xml文件中的animateLayoutChanges进行了报废,并为每个片段的changeCity()函数添加了一个ViewPropertyAnimator(用于更新视图中显示的数据)。

public void changeCity(final String city){
        final LinearLayout layout = (LinearLayout)getActivity()
                .findViewById(R.id.fragment_forecast);
        layout.animate().setDuration(600);
        layout.animate().alpha(0);
        Runnable endAction = new Runnable() {
            @Override
            public void run() {
                updateForecastData(city);
                layout.animate().alpha(1);
            }
        };
        layout.animate().withEndAction(endAction);
    }

更新数据并淡化视图的调用我置于使用ViewPropertyAnimator函数withEndAction(Runnable runnable)调用的runnable中,该函数仅在当前动画完成后运行,因此视图淡出 - >运行endAction runnable,更新数据 - >意见逐渐消失。