当我将设备横向旋转为纵向或纵向设备时,横向设备无法刷新UI。我正在使用片段“onCreateView”和“onConfigurationChanged”绘制布局,remote_gesture和remote_navigation有两种布局用于纵向和横向。最后将android:configChanges="orientation|screenSize|keyboardHidden"
添加到AndroidManifest.xml
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//View rootView = (View)inflater.inflate(R.layout.remote_navigation, container, false);
View rootView = (View)inflater.inflate(R.layout.fragment_remote, container, false);
viewSwitcher =(ViewSwitcher)rootView.findViewById(R.id.remote_viewswitcher);
remoteGesture = (View)inflater.inflate(R.layout.remote_gesture, null);
remoteNav = (View)inflater.inflate(R.layout.remote_navigation, null);
viewSwitcher.addView(remoteNav);
viewSwitcher.addView(remoteGesture);
setupButtons(rootView);
return rootView;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.w(TAG, "rrr... onConfigurationChanged...");
ViewGroup container = (ViewGroup) getView();
//container.removeAllViewsInLayout();
LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = (View)inflater.inflate(R.layout.fragment_remote, container, false);
viewSwitcher = (ViewSwitcher) rootView.findViewById(R.id.remote_viewswitcher);
remoteGesture = (View) inflater.inflate(R.layout.remote_gesture, null);
remoteNav = (View)inflater.inflate(R.layout.remote_navigation, null);
viewSwitcher.addView(remoteNav);
viewSwitcher.addView(remoteGesture);
}
我该如何解决这个问题?
答案 0 :(得分:1)
在onConfigurationChanged
()上,删除视图,然后再添加新的。这就像强制刷新一样。示例代码:
viewSwitcher.removeAllViews()
方法@ ViewGroup removeAllViews()
的定义另一个类似的示例代码,通用版本:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Refresh views by removing first
ViewGroup viewGroup = (ViewGroup) getView();
viewGroup.removeAllViewsInLayout();
...
}