美文网首页
十五、Activity与Fragment通信

十五、Activity与Fragment通信

作者: 你的益达233 | 来源:发表于2020-01-28 15:45 被阅读0次

    1、fragment与fragment之间通信
    方法一:

    public class Fragment1 extends Fragment {
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment1,null);
        // 给fragment上的按钮添加点击事件  
        v.findViewById(R.id.btn_change).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    //拿到frgment---fragment之间的通信  
    Fragment2 f2= (Fragment2) getActivity().getFragmentManager().findFragmentByTag("two");//two是fragment2的tag,
     
        f2.changeStr("我要改变世界");
    
    }
      });
    
       return v;
     }
    }
    

    方法二:EventBus

    2、activity传值给fragment
    方法一:
    在activity中建一个bundle,把要传的值存入bundle,然后通过fragment的setArguments(bundle)传到fragment,在fragment中,用getArguments接收
    方法二:
    在onAttach()方法中获取Activity的值,在onCreateView中使用
    示例代码:

    public class FragmentOne extends Fragment {
    
    private Button btn;
    private TextView text;
    private String str;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragmentone,null);
        // 给fragment上的按钮添加点击事件
        text = v.findViewById(R.id.tv_fragmentone);
        btn = v.findViewById(R.id.getImgae);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
            }
        });
    
        text.setText(str);
    
    
    //return inflater.inflate(R.layout.fragmentone,container,false);
        return v;
    }
    
    
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    
        str = ((MainActivity)context).toFragmentone();
        Log.e("ss",str);
    
    }
    }
    

    3、fragment传值给Activity
    方法一:通过在fragment中实现接口的方式
    1.fragment中准备回调接口 接口中声明传值的回调方法

    2.在fragment中定义属性private MyListener myListener

    3.重写fragment中的onAttach()方法:listener = (MyLisener)getActivity();

    4.fragment触发事件时回传值

    5.Activity中实现回调接口 重写回调方法获取回传的值并显示

    示例代码:

    public class FragmentOne extends Fragment {
    
    private Button btn;
    private TextView text;
    private String str;
    
    private MyListener ac;
    
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragmentone,null);
        // 给fragment上的按钮添加点击事件
        text = v.findViewById(R.id.tv_fragmentone);
        btn = v.findViewById(R.id.getImgae);
    
    btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //通过调用在activity中实现的接口方法,吧数据传给Mainactivity
       ac.sendContent("dddd");
    
    }
    });
    
    
    //return inflater.inflate(R.layout.fragmentone,container,false);
        return v;
    }
    

    //activity和fragment联系时候调用,fragment必须依赖activty

    public void onAttach(Context context) {
        super.onAttach(context);
    
     ac = (MyListener) getActivity();//或者ac=(MainActivity) context;
    
    }
    
    
    //①定义回调接口
    public interface MyListener{
        public void sendContent(String info);
    }
    
    }
    

    在MainActivity中:

    import com.example.wofu.aichi010.Fragment.FragmentOne.MyListener;//fragmentOne中的接口
    
    public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener,MyListener{
    
    //fragmentOne中的接口,在这实现,以实现fragmentOne传值Mainactivity
    public void sendContent(String info) {
    if (info!=null && !"".equals(info)) {
    Log.e("sss",info);
    }else {
    
    }
    }
    
    
    }
    

    相关文章

      网友评论

          本文标题:十五、Activity与Fragment通信

          本文链接:https://www.haomeiwen.com/subject/smxmthtx.html