动画布局刷新交叉淡入淡出

Animating a layout refresh crossfade

Android 菜鸟,我的 main activity 中有一个功能可以刷新天气数据;它通过调用两个片段中的函数来实现这一点,这些片段从网络 API 中提取新数据。当您点击刷新 button/change 位置时,我希望两个片段布局交叉淡入淡出,但我似乎无法让 animateLayoutChanges = "true" 执行我期望的操作(当视图设置为 [= 时交叉淡入淡出) 19=] 并回到 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,是否有一些保护来引用它们引用的片段之外的布局?非常感谢任何帮助!

所以我想到了解决方案;我放弃了使用 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);
    }

我将更新数据和淡出视图的调用放在 运行nable 中,该函数使用 ViewPropertyAnimator 函数 withEndAction(Runnable runnable) 调用,只有 运行s 一次当前动画已经结束,因此视图淡出 -> endAction 运行nable 是 运行,数据已更新 -> 视图淡入。