多个Fragment在同一个Activity中时,在信息交互时,由于Fragment是独立的,且不产生新的Activity可以使用startActivityForResult获取返回参数,同时也无法使用setArgment等函数多次向其他Fragment设置参数,这种情况下只能使用BroadcastReceiver来及时通知其他的子Fragment以及更新子Fragment。
由于broadcastReciver是对整个系统内的应用程序进行广播通知的,为了安全起见可使用LocalBroadCastReceiver来进行应用程序内的广播发送与接收。
下面是一个同在一个Activity中的两个Fragment,TitleFragment是一个列表Fragment,点击不同的列表时,可更新右边DetailFragment中的内容。他们之间的通信使用的时LocalBraodCastReceiver来进行的。
private MainCallBackLocalBroadCastReceiver mainCallBackLocalBroadCastReceiver;
private LocalBroadcastManager lBM;
/**
* 初始化广播器
*/
private void InitMainLocalBroadcastReceiver() {
lBM = LocalBroadcastManager.getInstance(MainActivity.this);
mainCallBackLocalBroadCastReceiver = new MainCallBackLocalBroadCastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("TitleFragment"); //添加过滤关键词
intentFilter.addAction("DetailFragment");//添加过滤关键词
lBM.registerReceiver(mainCallBackLocalBroadCastReceiver, intentFilter);
}
/**
* 广播接收处理函数
* @author admin
*
*/
private class MainCallBackLocalBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("TitleFragment")) {
//得到Title中传来的数据
System.out.println("TitleFragment");
String str = (String)intent.getExtras().get("arraylist");
System.out.println(str);
Intent intent2 = new Intent();
Bundle bundle = new Bundle();
//将数据放入到Bundle中,并使用Intent发送数据
bundle.putString("detail_list", str);
intent2.putExtras(bundle);
intent2.setAction("Detail_ReceiveMainFragment");
lBM.sendBroadcast(intent2);
} else if (intent.getAction().equals("DetailFragment")) {
}
}
}
if (mIntent == null) {
mIntent = new Intent();
}
mIntent.setAction("TitleFragment");
Bundle bundle = new Bundle();
bundle.putString("arraylist", arraylist.get(position));
mIntent.putExtras(bundle);
mLBM.sendBroadcast(mIntent);
TitleFragment:
package com.example.listfragment2;
import java.util.ArrayList;
import java.util.List;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class TitleFragment extends ListFragment {
private ArrayList arraylist;
private LocalBroadcastManager mLBM;
private Intent mIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
arraylist = new ArrayList();
arraylist.add("123");
arraylist.add("234");
arraylist.add("345");
arraylist.add("123");
arraylist.add("234");
arraylist.add("345");
arraylist.add("123");
arraylist.add("234");
arraylist.add("345");
arraylist.add("123");
arraylist.add("234");
arraylist.add("345");
UserAdapter userAdapter = new UserAdapter(arraylist);
setListAdapter(userAdapter);
;
if (getActivity() != null) {
mLBM = LocalBroadcastManager.getInstance(getActivity());
}
}
public class UserAdapter extends ArrayAdapter {
public UserAdapter(ArrayList arraylist) {
super(getActivity(), 0, arraylist);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.titlefragment_item, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tv_item);
tvItem.setText(arraylist.get(position));
return convertView;
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if (mIntent == null) {
mIntent = new Intent();
}
mIntent.setAction("TitleFragment");
Bundle bundle = new Bundle();
bundle.putString("arraylist", arraylist.get(position));
mIntent.putExtras(bundle);
mLBM.sendBroadcast(mIntent);
}
}
package com.example.listfragment2;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
private LocalBroadcastManager mLBM;
private DetailBroadCastReceiver detailBroadCastReceiver;
private TextView mtvtext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detailframent, null);
mtvtext = (TextView)view.findViewById(R.id.tvdetail);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
InitDetailBroadCastReceiver();
}
/**
* 初始化Detail广播LocalBroadcastReceiver
*/
private void InitDetailBroadCastReceiver() {
mLBM = LocalBroadcastManager.getInstance(getActivity());
detailBroadCastReceiver = new DetailBroadCastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Detail_ReceiveMainFragment");
mLBM.registerReceiver(detailBroadCastReceiver, intentFilter);
}
/**
* 广播接收处理函数
* @author admin
*
*/
public class DetailBroadCastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("detailFragment!!!!!!!!!!!!!!!");
String str = (String)intent.getExtras().get("detail_list");
mtvtext.setText(str);
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mLBM.unregisterReceiver(detailBroadCastReceiver);
}
}
package com.example.listfragment2;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
/**
* 两个Fragment : 1 ListFragment :显示信息列表 ...... 2 Fragment :显示详细信息
* 采用LocalBroadCastReceiver来实现Activity与Fragment之间的交互
* 可以实现动态数据拉取功能
* @author admin
*
*/
public class MainActivity extends Activity {
private MainCallBackLocalBroadCastReceiver mainCallBackLocalBroadCastReceiver;
private LocalBroadcastManager lBM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitTitleFragment();
InitDetailFragment();
InitMainLocalBroadcastReceiver();
}
/**
* 初始化广播器
*/
private void InitMainLocalBroadcastReceiver() {
lBM = LocalBroadcastManager.getInstance(MainActivity.this);
mainCallBackLocalBroadCastReceiver = new MainCallBackLocalBroadCastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("TitleFragment"); //添加过滤关键词
intentFilter.addAction("DetailFragment");//添加过滤关键词
lBM.registerReceiver(mainCallBackLocalBroadCastReceiver, intentFilter);
}
@Override
protected void onDestroy() {
lBM.unregisterReceiver(mainCallBackLocalBroadCastReceiver);
super.onDestroy();
}
/**
* 广播接收处理函数
* @author admin
*
*/
private class MainCallBackLocalBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("TitleFragment")) {
//得到Title中传来的数据
System.out.println("TitleFragment");
String str = (String)intent.getExtras().get("arraylist");
System.out.println(str);
Intent intent2 = new Intent();
Bundle bundle = new Bundle();
//将数据放入到Bundle中,并使用Intent发送数据
bundle.putString("detail_list", str);
intent2.putExtras(bundle);
intent2.setAction("Detail_ReceiveMainFragment");
lBM.sendBroadcast(intent2);
} else if (intent.getAction().equals("DetailFragment")) {
}
}
}
/**
* 初始化Fragment
*/
public void InitTitleFragment() {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
TitleFragment titleFragment = new TitleFragment();
transaction.replace(R.id.fm_content, new TitleFragment());
transaction.commit();
}
/**
* 初始化Fragment
*/
public void InitDetailFragment() {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
DetailFragment detailFragment = new DetailFragment();
transaction.replace(R.id.fl_details, detailFragment);
transaction.commit();
}
}