fragment 是android3.0引入的新的api ,起初是在平板上使用的,后来谷歌移植到了 手机中 fragment 是不能单独存在的 必须依附在activity中使用,fragment有自己的生命周期,但是fragment的生命周期必须在activity处于活动的时候才会生效,当activity销毁的时候fragment也会被销毁,
下面我就介绍一下activity与fragment之间如何跳转 以及 如何传递数据.
首先定义一个mani.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.tdcq.fragment_two_chuandi.MainActivity"> <LinearLayout android:background="#fff000" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="activity跳转fragment" android:id="@+id/button" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是activity"/> </LinearLayout> <FrameLayout android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> </FrameLayout> <FrameLayout android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> </FrameLayout> </LinearLayout>下面是在main 中的代码 在代码中做详细的介绍
FragmentTransaction 可以调用同时调用多个方法, 如add()添加 ,remove()移除, replace()替换, 然后调用commit()方法提交事务
在此demo中一个activity中有两个fragment 那么在点击返回键的时候会产生将整个activity中所有的fragment全部关闭的情况那么就需要用到下面的方法, addToBackStack() 此方法的含义是 返回到替换fragment之前的状态, 当有多个fragment的时候 点击返回键或逐步将fragment 关闭直到最后一个关闭后 ,在关闭activity
fragment1 中的 布局文件
定义了两个按钮 一个接收activity中的 文本内容 一个是点击跳转下一个fragment2,并携带数据
<?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="wrap_content" android:background="@color/colorAccent" android:orientation="vertical"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取activity的内容"/> <TextView android:id="@+id/fragment_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment1的界面"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转fragment2"/> </LinearLayout>fragment1中的代码文件
在掉跳转到fragment2 中时携带数据, 这里是传递的参数是在fragment2中定义了一个方法利用bundle 传输数据包的形式传递
package com.tdcq.fragment_two_chuandi; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class HomeFragment extends Fragment { private FragmentManager fm; private FragmentTransaction fragmentTransaction; private Button mButton1; private Button mButton2; private View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = View.inflate(getActivity(), R.layout.fragment_1, null); mButton1 = (Button) view.findViewById(R.id.button1); mButton2 = (Button) view.findViewById(R.id.button2); mButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView textView = (TextView) getActivity().findViewById(R.id.textview); //获取文本中的内容并将其转换成字符串 显示在土司中 Toast.makeText(getActivity(),textView.getText().toString(),Toast.LENGTH_SHORT).show(); } }); mButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fm = getFragmentManager(); //获取片段的管理器 fragmentTransaction= fm.beginTransaction(); //事务:要么同时生效 , 要么同时失败 //再跳转到界面的同时调用fragment2中的方法将参数传递过去 Fragment2 fragment2 = Fragment2.newInstance("从Fragment1传来参数到fragment2中"); //使用fragment替换现有布局 id 要替换的fragment 对象 fragmentTransaction.replace(R.id.fragment2, fragment2); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); return view; } }fragment 2中的 xml
这个就可以放一个文本就可以了 为了方便查看 在这里给了一个文本提示
<?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="wrap_content" android:background="@color/colorPrimary" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment2的界面"/> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> </LinearLayout>fragment2 中的代码 获取fragment1 中的数据 显示到textview中
package com.tdcq.fragment_two_chuandi; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment2 extends Fragment { private View view; private TextView textview2; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = View.inflate(getActivity(), R.layout.fragment_2, null); textview2 = (TextView) view.findViewById(R.id.textview2); //获取参数 String mParam1 = getArguments().getString("text"); //将参数设置给文本视图 textview2.setText(mParam1); return view; } public static Fragment2 newInstance(String text) { Fragment2 fragment = new Fragment2(); Bundle args = new Bundle(); args.putString("text", text); //向fragment传入参数 fragment.setArguments(args); return fragment; } }源码下载地址: http://download.csdn.net/detail/q9104422999/9603227