Android新手在这里,我有一个函数在我的主活动刷新天气数据;它通过在两个片段中调用函数来实现这一点,这两个片段从web API中提取新数据。当你点击刷新按钮/改变位置时,我想要两个片段布局交叉褪色,我似乎无法让animateLayoutChanges ="true"去做我所期望的(当视图设置为视图时交叉褪色)。离开并回到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,并添加了一个ViewPropertyAnimator到每个片段的changeCity()函数(用于更新视图中显示的数据)。
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);
}
更新数据和淡出视图的调用我放置在一个runnable中,这个runnable是由ViewPropertyAnimator函数withEndAction(Runnable runnable)
调用的,它只在当前动画完成时运行,所以视图淡出-> endAction runnable运行,数据更新->视图淡出。