我正在使用Fragment主细节体系结构
所有Fragment类都具有相同的逻辑
是否可以将Fragment与布局“分离”并仅使用一个Fragment类,
所以此代码:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
if("001".equalsIgnoreCase(id)){
arguments.putString(Fragment001.ARG_ITEM_ID, id);
Fragment001 fragment = new Fragment001();
fragment.setArguments(arguments); fragManager.replace(R.id.item_detail_container, fragment);
}
else if("002".equalsIgnoreCase(id)){
arguments.putString(Fragment002.ARG_ITEM_ID, id);
Fragment002 fragment = new Fragment002();
fragment.setArguments(arguments);
fragManager.replace(R.id.item_detail_container, fragment);
}
fragManager.commit();
会变成类似:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
GenericFragment fragment = new GenericFragment();
fragment.setUiId(id)
fragManager.replace(R.id.item_detail_container, fragment);
fragManager.commit();
是的,您可以检查并选择要在onCreateView中使用的布局...
public static final String ARG_ITEM_ID1 = "fragment001";
public static final String ARG_ITEM_ID2 = "fragment002";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
String id = "fragment001";
if(ARG_ITEM_ID1.equalsIgnoreCase(id)){
rootView = inflater.inflate(R.layout.fragment_1_layout, container, false);
}
else {
rootView = inflater.inflate(R.layout.fragment_2_layout, container, false);
}
return rootView;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句