MainActivity

package com.example.zhangyu.myapplication;import android.os.Bundle;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements BlankFragment2.OnFragmentInteractionListener {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //1.创建Fragment的管理对象        FragmentManager fragmentManager = getSupportFragmentManager();        //2.获取Fragment的事务对象并且开启事务        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();        //3.调用事务中动态操作fragment的方法执行 add(添加到哪里,需要添加的fragment对象);        fragmentTransaction.add(R.id.fl_fragment, BlankFragment2.newInstance("参数1:hello1", "参数2:hello2"));        //4.提交事务        fragmentTransaction.commit();    }    @Override    public void onFragmentInteraction(String s) {        TextView tvMain =  findViewById(R.id.tv_main);        tvMain.setText(s);    }}

BlankFragment2

package com.example.zhangyu.myapplication;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;/** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link OnFragmentInteractionListener} interface * to handle interaction events. * Use the {@link BlankFragment2#newInstance} factory method to * create an instance of this fragment. */public class BlankFragment2 extends Fragment {    // TODO: Rename parameter arguments, choose names that match    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER    private static final String ARG_PARAM1 = "param1";    private static final String ARG_PARAM2 = "param2";    // TODO: Rename and change types of parameters    private String mParam1;    private String mParam2;    private OnFragmentInteractionListener mListener;    private TextView tvTest2;    public BlankFragment2() {        // Required empty public constructor    }    /**     * Use this factory method to create a new instance of     * this fragment using the provided parameters.     *     * @param param1 Parameter 1.     * @param param2 Parameter 2.     * @return A new instance of fragment BlankFragment2.     */    // TODO: Rename and change types and number of parameters    public static BlankFragment2 newInstance(String param1, String param2) {        BlankFragment2 fragment = new BlankFragment2();        Bundle args = new Bundle();        args.putString(ARG_PARAM1, param1);        args.putString(ARG_PARAM2, param2);        fragment.setArguments(args);        return fragment;    }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        if (getArguments() != null) {            mParam1 = getArguments().getString(ARG_PARAM1);            mParam2 = getArguments().getString(ARG_PARAM2);        }    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        View view = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);        tvTest2 = view.findViewById(R.id.tv_test2);        tvTest2.append("\n" + mParam1);        tvTest2.append("\n" + mParam2);        tvTest2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                onButtonPressed();            }        });        return view;    }    // TODO: Rename method, update argument and hook method into UI event    public void onButtonPressed() {        if (mListener != null) {            mListener.onFragmentInteraction("接口回调,Fragment传递数据到Activity");        }    }    @Override    public void onAttach(Context context) {        super.onAttach(context);        if (context instanceof OnFragmentInteractionListener) {            mListener = (OnFragmentInteractionListener) context;        } else {            throw new RuntimeException(context.toString()                    + " must implement OnFragmentInteractionListener");        }    }    @Override    public void onDetach() {        super.onDetach();        mListener = null;    }    /**     * This interface must be implemented by activities that contain this     * fragment to allow an interaction in this fragment to be communicated     * to the activity and potentially other fragments contained in that     * activity.     * 

* See the Android Training lesson Communicating with Other Fragments for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(String s); }}

更多相关文章

  1. android 实现Listview 动态删除
  2. Glide回调设置Bitmap对象
  3. 使用Toast显示提示信息框
  4. 安装时提示:INSTALL_FAILED_INSUFFICIENT_STORAGE
  5. Android(安卓)代码监控apk安装,卸载,替换
  6. Android(安卓)SQLite数据库学习笔记
  7. Android打包常见问题的解决办法
  8. 使用SmartRefreshLayout 给RecyclerView 添加反弹效果示例
  9. 使用Shader渲染图形

随机推荐

  1. Android(安卓)监听短信2种方式:Broadcast
  2. -----------Android(安卓)Support v4、v7
  3. 热修复AndFix详解
  4. Android趣事五则
  5. VLC Android播放器介绍
  6. Linux Kernel and Android(安卓)休眠与唤
  7. android 命令介绍
  8. textview中加链接
  9. android 静音与振动
  10. ImageView androidscaleType属性详解