1.在fragment声明一个接口,实现回调。
public class FragmentOne extends Fragment{
private static final String TAG = "FragmentOne";
public interface FOneBtnClickListener{
void onOneBtnClick();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button toTwoFragment = (Button) view.findViewById(R.id.button);
toTwoFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断Activity是否实现了FOneBtnClickListener
if(getActivity() instanceof FOneBtnClickListener){
((FOneBtnClickListener)getActivity()).onOneBtnClick();
}
}
});
return view;
}
2.
public class FragmentTwo extends Fragment{
//定义内部接口变量
private FTwoOnBtnClickListener fTwoOnBtnClickListener;
public interface FTwoOnBtnClickListener{
void onTwoBtnClick();
}
//为该变量赋值
public void setOnBtnClickListener(FTwoOnBtnClickListener fTwoOnBtnClickListener){
this.fTwoOnBtnClickListener = fTwoOnBtnClickListener;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button toThreeFragment = (Button) view.findViewById(R.id.button);
toThreeFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//该变量不为空,则说明Activity实现了该接口
if(fTwoOnBtnClickListener != null){
fTwoOnBtnClickListener.onTwoBtnClick();
}
}
});
return view;
}
}