您可以使用Interface在片段之間傳遞數據。 聲明一個接口:上LayoutOne.java片段
public interface OnButtonPressListener {
public void onButtonPressed(String msg,int value,ArrayList<Object> arrData);
}
:
public class LayOutOne extends Fragment {
OnButtonPressListener buttonListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
Button but=(Button)root.findViewById(R.id.button1);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonListener.onButtonPressed("Message From First Fragment",1,ArrayList objct);
}
});
return root;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
buttonListener = (OnButtonPressListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onButtonPressed");
}
}
上片段2
public class LayOutTwo extends Fragment implements OnButtonPressListener{
ViewGroup root;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
root = (ViewGroup) inflater.inflate(R.layout.layout_two, null);
return root;
}
@Override
public void onButtonPressed(String msg,int i,ArrayList<object> obj) {
// TODO Auto-generated method stub
//print the values or show them
}
您必須通過接口調用此片段替換方法。 onClick方法必須位於活動類中。要了解如何執行此操作,請參閱http://stackoverflow.com/a/12683615/5819589關於如何使用捆綁檢查將數據發送到片段http://stackoverflow.com/a/12739968/5819589 –
什麼類型的數據你想傳遞第二個片段 –
我想傳遞字符串,int值..和arralist –