如图所示:一个activity中,放了两个fragment,现在要把fragmentA中的数据,在fragmentB中显示出来,怎么做呢?
思路:首先想到的是利用接口回调,因为接口确实是一个很好的俄东西,不用不知道它的好处。。。。
上代码:看如何巧用接口进行的通讯
第一步:定义一个接口:
/**
* @author 作者名 易皇星
* @email 邮箱名 [email protected]
* @time 2016-5-9 2016-05
* @TODO 缘分是本书,翻得不经意会错过,读得太认真会流泪!
*
*
* 接口是史上最好的回调
*/
public interface OnArticleSelectedListener {
public void onArticleSelected(String msg);
}
第二步:在MainActivity实现这个接口
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnArticleSelectedListener {
private TextView tv_show;
private String msg;
private LeftFragment mlFragment=new LeftFragment();
private RightFragment mrFragment=new RightFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction beginTransaction = getSupportFragmentManager().beginTransaction();
beginTransaction.replace(R.id.fl_fragmentA, mlFragment);
beginTransaction.commit();
FragmentTransaction beginTransaction1 = getSupportFragmentManager().beginTransaction();
beginTransaction1.replace(R.id.fl_fragmentB, mrFragment);
beginTransaction1.commit();
}
@Override
public void onArticleSelected(String msg) {
mrFragment.update(msg);
}
第三步:看看fragment 怎么利用接口的吧:
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* * 当fragment添加到activity中时,会调用fragment的方法onAttach(),
* 这个方法中适合检查activity是否实现了OnArticleSelectedListener接口,
* 检查方法就是对传入的activity的实例进行类型转换
*
*
*/
public class LeftFragment extends Fragment {
private OnArticleSelectedListener mListener;
private TextView tv_text_left;
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener =(OnArticleSelectedListener)context;
}catch (Exception ex){
throw new ClassCastException(context.toString()+"must implement OnArticleSelectedListener");
}
}
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view=inflater.inflate(R.layout.fragment_left, container, false);
tv_text_left= (TextView) view.findViewById(R.id.tv_left_text);
tv_text_left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(null!=mListener){
mListener.onArticleSelected(tv_text_left.getText().toString());
}
}
});
return view;
}
}
RigntFragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RightFragment extends Fragment {
private TextView tv_text_right;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view=inflater.inflate(R.layout.fragment_right, container, false);
tv_text_right= (TextView) view.findViewById(R.id.tv_text_right);
return view;
}
//定义一个方法
public void update(String msg){
tv_text_right.setText(msg);
}
}
效果图:
猛戳这里:studio点击下载