参考:Fragment间传值三种方式
1、在【接收方】的fragment中设置setData方法
public void setData(String string) {
bt_main.setText(string);
}
2、在【发送方】获取【接收方】的fragment,并调用setData方法
// 方法一:在MainFragment中设置一个setData()方法,在方法中设置更改按钮名称
MainFragment mainFragment = (MainFragment)getActivity().getSupportFragmentManager().findFragmentByTag("mainFragment");
mainFragment.setData(mDatas.get(position));
1、【发送方】的fragment中设置接口
private OnDataTransmissionListener mListener;
//接口回调的方法
public interface OnDataTransmissionListener {
void dataTransmission(String data);
}
public void setOnDataTransmissionListener(OnDataTransmissionListener mListener) {
this.mListener = mListener;
}
2、【发送方】的fragment中接口设置数据
//方法二:采取接口回调的方式进行数据传递
if (mListener != null) {
mListener.dataTransmission(mDatas.get(position));
}
3、在activity中实现接口,并将数据传给【接收方】的fragment
MenuFragment menuFragment = new MenuFragment();
final MainFragment mainFragment = new MainFragment();
menuFragment.setOnDataTransmissionListener(new MenuFragment.OnDataTransmissionListener() {
@Override
public void dataTransmission(String data) {
mainFragment.setData(data);
}
});
1、添加eventbus依赖:
compile 'org.greenrobot:eventbus:3.0.0'
2、【发送方】的fragment传递数据
//方法三:使用第三方开源框架EventBus
EventBus.getDefault().post(mDatas.get(position));//传递数据
3、【接收方】的fragment接收数据
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = View.inflate(getActivity(), R.layout.main_fragemnt, null);
bt_main = (Button) mainView.findViewById(R.id.bt_main);
EventBus.getDefault().register(this);//注册
return mainView;
}
@Subscribe
public void onEvent(String data) {//接受数据
bt_main.setText(data);
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);//取消注册
}
public class MainActivityIII extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainiii);
//实例化两个Fragment
MenuFragment menuFragment = new MenuFragment();
final MainFragment mainFragment = new MainFragment();
//将上面的两个Fragment添加进来
getSupportFragmentManager().beginTransaction().replace(R.id.fl_menu, menuFragment, "menuFragment").commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, mainFragment, "mainFragment").commit();
menuFragment.setOnDataTransmissionListener(new MenuFragment.OnDataTransmissionListener() {
@Override
public void dataTransmission(String data) {
mainFragment.setData(data);
}
});
}
}
MainActivityIII 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fl_menu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#20ff0000" />
<FrameLayout
android:id="@+id/fl_main"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#2500ff00" />
LinearLayout>
public class MenuFragment extends Fragment {
private String[] ways = {"通过标签获取", "采取接口回调", "开源框架:EventBus"};
private List mDatas = new ArrayList<>();
private OnDataTransmissionListener mListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View menuView = View.inflate(getActivity(), R.layout.menu_fragemnt, null);
for (int i = 0; i < ways.length; i++) {
mDatas.add(ways[i]);
}
final ListView lv = (ListView) menuView.findViewById(R.id.lv_menu);
lv.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.menu_fragemnt_item, parent, false);
TextView tv = (TextView) contentView.findViewById(R.id.tv_menu_item);
tv.setText(mDatas.get(position));
/**
* 需求:点击对应的条目,将条目的内容发送到MainFragment中的Button上,更改Button名称(即进行一个数据传递)
*/
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
switch (position) {
case 0:
/**
* 方法一:在MainFragment中设置一个setData()方法,在方法中设置更改按钮名称
*/
MainFragment mainFragment = (MainFragment) getActivity()
.getSupportFragmentManager().findFragmentByTag("mainFragment");
mainFragment.setData(mDatas.get(position));
break;
case 1:
/**
* 方法二:采取接口回调的方式进行数据传递
*/
if (mListener != null)
mListener.dataTransmission(mDatas.get(position));
break;
case 2:
/**
* 方法三:使用第三方开源框架EventBus
*/
EventBus.getDefault().post(mDatas.get(position));
break;
default:
break;
}
}
});
return contentView;
}
});
return menuView;
}
//接口回调的方法
public interface OnDataTransmissionListener {
void dataTransmission(String data);
}
public void setOnDataTransmissionListener(OnDataTransmissionListener mListener) {
this.mListener = mListener;
}
}
MenuFragment布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
RelativeLayout>
listview的item布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_menu_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="item"
android:textColor="@color/colorAccent"
android:textSize="25sp" />
LinearLayout>
public class MainFragment extends Fragment {
private Button bt_main;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = View.inflate(getActivity(), R.layout.main_fragemnt, null);
bt_main = (Button) mainView.findViewById(R.id.bt_main);
EventBus.getDefault().register(this);
return mainView;
}
@Subscribe
public void onEvent(String data) {
bt_main.setText(data);
}
public void setData(String string) {
bt_main.setText(string);
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
MainFragment布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bt_main"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="#ff0"
android:text="根据条目改变内容" />
RelativeLayout>