摘要
本文介绍了Android中一个Activity中有多个Fragment的情况下,Fragment之间如何通过Activity进行互操作。
源代码
源代码地址为:http://download.csdn.net/detail/logicteamleader/8931199
源代码使用ADT编写,ADT版本为2014,Android版本为android-22。
技术要点
1.在Activity中的多个Fragment之间要互操作,一定要通过此Activity,不能直接通信;
2.在Activity中添加Fragment可以使用id或者Tag,建议使用Tag,这样无论该Fragment有没有界面,都可以被Activity使用findFragmentByTag方法找到;
3.在Fragment中定义回调接口,让Activity实现此回调接口,这样就能让Activity响应Fragment中的消息;
4.不同的Fragment之间要互操作,还是通过Activity实现回调接口来实现,例如FragmentA中回调MainActivity中的OnBtnAClicked方法,而OnBtnAClicked方法中又调用了FragmentB的changeB方法,达到了两个Fragment之间的互操作。
FragmentA的源代码
package com.apkkids.fragmentinactivityexample;import android.app.Activity;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.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;public class FragmentA extends Fragment { private static int iChange = 1; private TextView tvA; //回调接口,此接口必须被MainActivity实现,并通过此接口回调来进行Fragment和Activity之间的互操作 public OnFragmentABtnClickedListener mCallback = null; public interface OnFragmentABtnClickedListener{ public void OnBtnAClicked(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try{ mCallback = (OnFragmentABtnClickedListener)activity; }catch(ClassCastException e){ throw new ClassCastException(activity.toString() + " must implement OnFragmentABtnClickedListener"); } } @Override @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragmenta_layout, container, false); tvA = (TextView) v.findViewById(R.id.tvA); Button btnA = (Button) v.findViewById(R.id.buttonA); //点击此按钮,通过回调接口,使得FragmentB中的textView改变 btnA.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { if(mCallback != null){ mCallback.OnBtnAClicked(); } } }); return v; } public void changeA() { if (tvA != null) { tvA.setText("TextView in FragmentA changed by FragmentB :" + iChange++); } }}
FragmentB的源代码
package com.apkkids.fragmentinactivityexample;import android.app.Activity;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.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;public class FragmentB extends Fragment { private static int iChange = 1; private TextView tvB; // 回调接口,此接口必须被MainActivity实现,并通过此接口回调来进行Fragment和Activity之间的互操作 public OnFragmentBBtnClickedListener mCallback = null; public interface OnFragmentBBtnClickedListener { public void OnBtnBClicked(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (OnFragmentBBtnClickedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentBBtnClickedListener"); } } @Override @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragmentb_layout, container, false); tvB = (TextView) v.findViewById(R.id.tvB); Button btnB = (Button) v.findViewById(R.id.buttonB); btnB.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mCallback != null) { mCallback.OnBtnBClicked(); } } }); return v; } /** * 改变Text的值 */ public void changeB() { if (tvB != null) { tvB.setText("TextView in FragmentB changed by FragmentA :" + iChange++); } }}
MainActivity的源代码
package com.apkkids.fragmentinactivityexample;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.Menu;import android.view.MenuItem;/** * @author wxb * Description:Activity中多个Fragment之间互操作的示例,主要使用回调接口方法实现。 * 注意:Fragment之间是不能直接通信的,一定要通过它们附着的Activity进行 * * 2015-7-24 * 下午10:02:45 */public class MainActivity extends FragmentActivity implements FragmentA.OnFragmentABtnClickedListener,FragmentB.OnFragmentBBtnClickedListener { private static final String FRAGMENT_A_TAG = "com.apkkids.fragmentA"; private static final String FRAGMENT_B_TAG = "com.apkkids.fragmentB"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentA fragmentA = new FragmentA(); FragmentB fragmentB = new FragmentB(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); //使用Tag方式添加Fragment,此外还可以用id方式添加 transaction.add(R.id.containerA, fragmentA, FRAGMENT_A_TAG); transaction.add(R.id.containerB, fragmentB, FRAGMENT_B_TAG); transaction.commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /* (non-Javadoc) * @see com.apkkids.fragmentinactivityexample.FragmentA.OnAClickedListener#OnAClicked() * FragmentA中定义的回调接口的方法,通过此方法修改FragmentB中的UI */ @Override public void OnBtnAClicked() { //得到FragmentB的实例 FragmentManager fragmentManager = getSupportFragmentManager(); FragmentB fragmentB = (FragmentB) fragmentManager.findFragmentByTag(FRAGMENT_B_TAG); if(fragmentB != null) fragmentB.changeB(); } @Override public void OnBtnBClicked() { //得到FragmentA的实例 FragmentManager fragmentManager = getSupportFragmentManager(); FragmentA fragmentA = (FragmentA) fragmentManager.findFragmentByTag(FRAGMENT_A_TAG); if(fragmentA != null) fragmentA.changeA(); }}
程序执行界面
小结
其他界面代码请从源代码包下载,都是不需要积分的。
版权声明:本文为博主原创文章,未经博主允许不得转载。