Fragment和Activity间的通讯的实现

1.      fragment中获取它所在的ActivitygetActivity();

2.      Activity获取某个Fragment对象可以通过FragmentManger类的findFragmentById(intid)(当是静态注册时有规定id)或findFragmentByTag(String tag)(当动态注册时添加和替换时有规定tag值)

通过上面获取的对象可以拿到类中的变量

传递数据

Activity向fragment传递数据可以通过Fragment类的setArguments(Bundlebundle)进行传递

Fragment向Activity传递数据或Activity需要在Fragment运行中进行实时的通信:在Fragment中定义一个内部会调接口,在让包含该Fragment的Activity实现该回调接口这样就可以将数据传递给Activity.

布局:

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"

    android:id="@+id/ll_show" >

 

   

     

     <LinearLayout

          android:layout_width="match_parent"

          android:layout_height="50dp"

      android:orientation="horizontal">

          <Button

             android:id="@+id/one"

             android:layout_width="match_parent"

             android:layout_height="match_parent"

             android:text="FragmentOne"

             android:layout_weight="1"/>

         

          <Button

             android:id="@+id/three"

             android:layout_width="match_parent"

             android:layout_height="match_parent"

             android:text="FragmentThree"

             android:layout_weight="1"/>

     LinearLayout>

 

LinearLayout>

 

public class MyFragment extends Activityimplements View.OnClickListener,FragmentOne.HasChange{

         FragmentManagermanger;

         FragmentfragmentOne,fragmentThree;

        

         @Override

         protectedvoid onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_fragment);

                   manger=getFragmentManager();

                   fragmentOne=newFragmentOne(this);

                   fragmentThree=new FragmentThree();

                   ButtonfragmentOne=(Button) findViewById(R.id.one);

                   fragmentOne.setOnClickListener(this);

//               Button fragemnttwo=(Button)findViewById(R.id.two);

//               fragemnttwo.setOnClickListener(this);

                   Buttonfragmentthree=(Button) findViewById(R.id.three);

                   fragmentthree.setOnClickListener(this);

         }

 

         @Override

         publicvoid onClick(View arg0) {

                   switch(arg0.getId()){

                   caseR.id.one:

                            FragmentTransactiontransaction=manger.beginTransaction();//每个事务对应一系列Fragment变化,每次都需要一个新的

                            transaction.add(R.id.ll_show,fragmentOne, "one");//添加一个fragment,当容器上没有Fragment时需要添加一个Fragemnt,该Fragment会直接显示,若容器上已有一个Fragment在次添加后不会显示新的,若希望看到新的只有替换旧的,或隐藏旧的

                            transaction.show(fragmentOne);//当fragment隐藏后再次显示的时候调用

                            transaction.commit();

                            break;

                   caseR.id.three:

                            FragmentTransactiontransactionss=manger.beginTransaction();

                            transactionss.replace(R.id.ll_show,fragmentThree, "three");

                            transactionss.addToBackStack(null);//参数是回退栈的一个标志,可以为空//将本操作添加到会退栈中,点击返回按钮会回退到本操作之前的状态

                            transactionss.commit();

                            break;

                   }

         }

 

         @Override

         publicvoid onCreateViewChange() {

                   Toast.makeText(this,"FragmentOne创建视图成功", Toast.LENGTH_LONG).show();        

         }

 

         @Override

         publicvoid onDestroyViewChange() {

                   Toast.makeText(this,"FragmentOne销毁视图成功", Toast.LENGTH_LONG).show();

         }

}

 

public class FragmentOne extends Fragment {

         ViewrootView;

         HasChangechange;

        

         publicFragmentOne(HasChange change){

                   this.change=change;

         }

         @Override

         publicView onCreateView(LayoutInflater inflater,

                            ViewGroupcontainer, Bundle savedInstanceState) {

                   rootView=inflater.inflate(R.layout.fragment_one,container, false);

                   Log.e("fragment_error","framentone_onCreateView()");

                   change.onCreateViewChange();

                   returnrootView;

         }

         @Override

         publicvoid onDestroyView() {

                   super.onDestroyView();

                   if(rootView!=null){

                            ViewGroupgroup=(ViewGroup) rootView.getParent();

                            group.removeView(rootView);

                            change.onDestroyViewChange();

                   }

         }

        

         publicinterface HasChange{

                   publicvoid onCreateViewChange();

                   publicvoid onDestroyViewChange();

         }

}

 

FragmentManger类的方法

findFragmentById()

findFragmentByTag()

popBackStack():是将Fragment从后台栈中弹出(模拟点击back键)

addOnBackStackChangeListener()注册一个监听器,用于监听后台栈的变化

你可能感兴趣的:(Android)