静态加载
新建fragment的布局
新建一个fragment的布局,就是该fragment要显示的内容:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Button" /> </LinearLayout>
新建fragment类
新建fragment类,继承自Fragment或者它的子类,重写onCreateView()方法,在该方法中调用infalter.inflate()方法加载fragment的布局文件,最后返回加载的view对象:
public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); return view; } }
修改Activity的布局文件
在需要加载Fragment的Activity对应的布局文件中添加fragment的标签,name属性是全限定类名,要包含Fragment的包名:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/my_fragment" android:name="com.example.fragmenttest.MyFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
Activity加载布局文件
Activity在onCreate( )方法中调用setContentView()加载布局文件:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
动态加载
主要代码:
getFragmentManager().beginTransaction().replace(R.id.layout, my_fragment).commit();
fragment与Activity的交互
组件获取
Fragment获得Activity中的组件:
getActivity().findViewById(R.id.button);
Activity获得Fragment中的组件(根据id和tag都可以):
getFragmentManager.findFragmentByid(R.id.my_fragment);
数据传递
Activity传递数据给Fragment
在Activity中创建Bundle数据包,调用Fragment实例的setArguments(bundle) 从而将Bundle数据包传给Fragment,然后Fragment中调用getArguments获得 Bundle对象,然后进行解析
Fragment传递数据给Activity
在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口, Fragment就可以通过回调接口传数据:
1. 定义一个回调接口(Fragment中)
public interface CallBack { /*定义一个获取信息的方法*/ public void getResult(String result); }
2. 接口回调(Fragment中)
public void getData(CallBack callBack){ /*获取文本框的信息,当然你也可以传其他类型的参数*/ String msg = editText.getText().toString(); callBack.getResult(msg); }
3. 使用接口回调方法读数据(Activity中)
leftFragment.getData(new CallBack() { @Override public void getResult(String result) { Toast.makeText(MainActivity.this, "-->>" + result, 1).show(); } });
Fragment与Fragment之间的数据互传
找到要接受数据的fragment对象,直接调用setArguments传数据进去,通常的话是replace时,即fragment跳转的时候传数据的,那么只需要在初始化要跳转的Fragment 后调用他的setArguments方法传入数据
如果是两个Fragment需要即时传数据,而非跳转的话,就需要先在Activity获得f1传过来的数据, 再传到f2了,就是以Activity为媒介
示例代码如下:
FragmentManager fManager = getSupportFragmentManager( ); FragmentTransaction fTransaction = fManager.beginTransaction(); Fragmentthree t1 = new Fragmentthree(); Fragmenttwo t2 = new Fragmenttwo(); Bundle bundle = new Bundle(); bundle.putString("key",id); t2.setArguments(bundle); fTransaction.add(R.id.fragmentRoot, t2, "123"); fTransaction.addToBackStack(t1); fTransaction.commit();