Fragment知识整理

横竖屏切换改变

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. int width = getWindowManager().getDefaultDisplay().getWidth();  
  2.         int height = getWindowManager().getDefaultDisplay().getHeight();  
  3.           
  4.         Fragment1 fragment1 = new Fragment1();  
  5.         Fragment2 fragment2 = new Fragment2();  
  6.           
  7.         //定义片段管理器  
  8.         FragmentManager fm=getFragmentManager();  
  9.         //定义片段事务处理  
  10.         FragmentTransaction ft=fm.beginTransaction();  
  11.           
  12.         //判断横竖屏  
  13.         if(width>height){  
  14.             ft.replace(android.R.id.content, fragment1);  
  15.         }else{  
  16.             ft.replace(android.R.id.content, fragment2);  
  17.         }  
  18.           
  19.         ft.commit();  

Activity向Fragment传数据

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. // 创建Bundle,准备向Fragment传入参数  
  2.         Bundle arguments = new Bundle();  
  3.         arguments.putInt(BookDetailFragment.ITEM_ID, id);  
  4.         // 创建BookDetailFragment对象  
  5.         BookDetailFragment fragment = new BookDetailFragment();  
  6.         // 向Fragment传入参数  
  7.         fragment.setArguments(arguments);  
  8.         // 使用fragment替换book_detail_container容器当前显示的Fragment  
  9.         getFragmentManager().beginTransaction()  
  10.             .replace(R.id.book_detail_container, fragment)  
  11.             .commit();   
 

Fragment获取数据

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. if (getArguments().containsKey(ITEM_ID))  
  2.         {  
  3.             book = BookContent.ITEM_MAP.get(getArguments()  
  4.                 .getInt(ITEM_ID)); //①  
  5.         }  

Fragment显示布局文件

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. // 重写该方法,该方法返回的View将作为Fragment显示的组件  
  2.     @Override  
  3.     public View onCreateView(LayoutInflater inflater  
  4.         , ViewGroup container, Bundle savedInstanceState)  
  5.     {  
  6.         // 加载/res/layout/目录下的fragment_book_detail.xml布局文件  
  7.         View rootView = inflater.inflate(R.layout.fragment_book_detail,  
  8.                 container, false);  
  9.         if (book != null)  
  10.         {  
  11.             // 让book_title文本框显示book对象的title属性  
  12.             ((TextView) rootView.findViewById(R.id.book_title))  
  13.                     .setText(book.title);  
  14.             // 让book_desc文本框显示book对象的desc属性  
  15.             ((TextView) rootView.findViewById(R.id.book_desc))  
  16.                 .setText(book.desc);      
  17.         }  
  18.         return rootView;  
  19.     }  

FragmentTransaction调用commit()方法之后,要你重新调用fragmentManager的beginTransaction()方法

 
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1.     fragmentManager=getFragmentManager();  
  2.       
  3.     abnormalFragment=new ExecuteAbnormalFragment();  
  4.     normalFragment = new ExecuteNormalFragment();  
  5.       
  6.     status=(RadioGroup)findViewById(R.id.status);  
  7.     status.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  8.         @Override  
  9.         public void onCheckedChanged(RadioGroup arg0, int checkedId) {  
  10.             fragmentTransaction=fragmentManager.beginTransaction();  
  11.             if(checkedId==R.id.abnormal){  
  12.                 Toast.makeText(getApplicationContext(), "不正常", Toast.LENGTH_SHORT).show();  
  13.                 fragmentTransaction.replace(R.id.status_show, abnormalFragment);  
  14. /整体语句执行方法           getFragmentManager().beginTransaction().replace(R.id.status_show, abnormalFragment).commit();  
  15.   
  16.             }else{  
  17.                 Toast.makeText(getApplicationContext(), "正常", Toast.LENGTH_SHORT).show();  
  18.                 fragmentTransaction.replace(R.id.status_show, normalFragment);  
  19. /                   getFragmentManager().beginTransaction().replace(R.id.status_show, normalFragment).commit();  
  20.             }  
  21.               
  22.             fragmentTransaction.commit();  
  23.         }  
  24.     });  
  25.       
  26. }  
posted @ 2017-04-22 18:31  天涯海角路  阅读(149)  评论(0)    收藏  举报