横竖屏切换改变
- int width = getWindowManager().getDefaultDisplay().getWidth();
- int height = getWindowManager().getDefaultDisplay().getHeight();
-
- Fragment1 fragment1 = new Fragment1();
- Fragment2 fragment2 = new Fragment2();
-
-
- FragmentManager fm=getFragmentManager();
-
- FragmentTransaction ft=fm.beginTransaction();
-
-
- if(width>height){
- ft.replace(android.R.id.content, fragment1);
- }else{
- ft.replace(android.R.id.content, fragment2);
- }
-
- ft.commit();
Activity向Fragment传数据
- Bundle arguments = new Bundle();
- arguments.putInt(BookDetailFragment.ITEM_ID, id);
-
- BookDetailFragment fragment = new BookDetailFragment();
-
- fragment.setArguments(arguments);
-
- getFragmentManager().beginTransaction()
- .replace(R.id.book_detail_container, fragment)
- .commit();
Fragment获取数据
- if (getArguments().containsKey(ITEM_ID))
- {
- book = BookContent.ITEM_MAP.get(getArguments()
- .getInt(ITEM_ID));
- }
Fragment显示布局文件
- @Override
- public View onCreateView(LayoutInflater inflater
- , ViewGroup container, Bundle savedInstanceState)
- {
-
- View rootView = inflater.inflate(R.layout.fragment_book_detail,
- container, false);
- if (book != null)
- {
-
- ((TextView) rootView.findViewById(R.id.book_title))
- .setText(book.title);
-
- ((TextView) rootView.findViewById(R.id.book_desc))
- .setText(book.desc);
- }
- return rootView;
- }
FragmentTransaction调用commit()方法之后,要你重新调用fragmentManager的beginTransaction()方法
- fragmentManager=getFragmentManager();
-
- abnormalFragment=new ExecuteAbnormalFragment();
- normalFragment = new ExecuteNormalFragment();
-
- status=(RadioGroup)findViewById(R.id.status);
- status.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup arg0, int checkedId) {
- fragmentTransaction=fragmentManager.beginTransaction();
- if(checkedId==R.id.abnormal){
- Toast.makeText(getApplicationContext(), "不正常", Toast.LENGTH_SHORT).show();
- fragmentTransaction.replace(R.id.status_show, abnormalFragment);
- /整体语句执行方法 getFragmentManager().beginTransaction().replace(R.id.status_show, abnormalFragment).commit();
-
- }else{
- Toast.makeText(getApplicationContext(), "正常", Toast.LENGTH_SHORT).show();
- fragmentTransaction.replace(R.id.status_show, normalFragment);
- / getFragmentManager().beginTransaction().replace(R.id.status_show, normalFragment).commit();
- }
-
- fragmentTransaction.commit();
- }
- });
-
- }
posted @
2017-04-22 18:31
天涯海角路
阅读(
149)
评论()
收藏
举报