在Fragment的onCreateView方法中,做如下处理:
private View parentView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (parentView == null) {
parentView = inflater.inflate(R.layout.fragment_singer, container, false);
//在这里做一些初始化处理
initChoiceLayout();
} else {
ViewGroup viewGroup = (ViewGroup) parentView.getParent();
if (viewGroup != null)
viewGroup.removeView(parentView);
}
return parentView;
}
该方法已经验证,真实有效