我正在使用一个名为SwipeRefreshLayout的小部件,当有人推动视图时刷新我的片段.
要重新创建我必须使用的活动:
SwipeRefreshLayout mSwipeRefreshLayout; public static LobbyFragment newInstance() { return new LobbyFragment(); } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.fragment_lobby,container,false); receiver = new MysqLReceiver(); rlLoading = (RelativeLayout) view.findViewById(R.id.rlLoading); gvLobby = (GridView) view.findViewById(R.id.gvLobby); updateList(); mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.mSwipeRefreshLayout); mSwipeRefreshLayout.setColorSchemeResources(R.color.pDarkGreen,R.color.pDarskSlowGreen,R.color.pLightGreen,R.color.pFullLightGreen); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { getActivity().recreate(); } }); return view; }
但我不想重新创建包含视图分页器的完整活动,我想重新创建片段.我怎样才能做到这一点?
解决方法
您可以使用 :
getSupportFragmentManager().beginTransaction() .replace(R.id.container,LobbyFragment.newInstance()).commit();
要重新创建片段
注意:getSupportFragmentManager()如果您正在使用支持片段和AppCompatActivity,如果您使用的是框架片段类,则需要使用getFragmentManager()