Fragment以及关于Activity和Fragment之间的通信

image.png

image.png

image.png

加载方式有俩种

  • 静态加载


    动态加载
  • dongtaij
  • 动态加载


    image.png

    使用事物

与Activity的通信

Activity给Fragment传递数据值

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/layout"
    android:layout_height="match_parent"
   >
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"/>

</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private EditText edit_text;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit_text=(EditText) findViewById(R.id.edit_text);
        button=(Button)findViewById(R.id.send);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
                String text=edit_text.getText().toString();
//                Intent i=new Intent(this,SecondActivity.class);
//                startActivity(i);
        MyFragment myFragment=new MyFragment();
        Bundle bundle=new Bundle();
        //绑定数据
        bundle.putString("name",text);
        myFragment.setArguments(bundle);
//        使用动态加载
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.layout,myFragment,"fragment_first");
        fragmentTransaction.commit();
        Toast.makeText(MainActivity.this,"向Frangment发送数据",Toast.LENGTH_SHORT).show();

    }
}
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
MyFragment

public class MyFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_first,container,false);
        TextView textView=(TextView)view.findViewById(R.id.text);
//        textView.setText("第一个fragment");
        //接受传递的值
        String text=getArguments().getString("name");
        textView.setText(text);
        Toast.makeText(getActivity(),"已成功接受text内容"+text,Toast.LENGTH_SHORT).show();
        return view;
    }
}

Fragment给Activity传递数据值

再MyFragment里添加一个接口方法,并在MainActivity里实现接口方法

MyFragment
public class MyFragment extends Fragment {
    private  String code="好好啊哦哦啊哦好好";
    public MyListener listener;
    public interface MyListener{
        public void thank(String code);
    }
//当fragment被添加进来调用
    @Override
    public void onAttach(Activity activity) {
     listener=(MyListener)activity;
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_first,container,false);
        TextView textView=(TextView)view.findViewById(R.id.text);
//        textView.setText("第一个fragment");
        //接受传递的值
        String text=getArguments().getString("name");
        textView.setText(text);
        Toast.makeText(getActivity(),"已成功接受text内容"+text,Toast.LENGTH_SHORT).show();
        Toast.makeText(getActivity(),"向activity发送消息"+text,Toast.LENGTH_SHORT).show();
        listener.thank(code);
        return view;
    }

}
MainActivity
  @Override
    public void thank(String code) {
         Toast.makeText(this,"成功接收到标语"+code,Toast.LENGTH_SHORT).show();
        edit_text.setText(code);
    }
image.png

Fragment和Activity的交互:

1、在Fragment中调用Activity中的方法:

Fragment可以通过getActivity()方法来获得Activity的实例,然后就可以调用一些例如findViewById()之类的方法。例如:

View listView = getActivity().findViewById(R.id.list);
但是注意调用getActivity()时,fragment必须和activity关联(attached to an activity),否则将会返回一个null。

另外,当碎片中需要使用Context对象时,也可以使用getActivity()方法,因此获取到的活动本身就是一个Context对象。

在Activity中调用Fragment中的方法:(要用到接口回调)

activity也可以获得一个fragment的引用,从而调用fragment中的方法。获得fragment的引用要用FragmentManager,之后可以调用findFragmentById() 或者 findFragmentByTag()。例如:

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

也可以通过接口回调的方式来进行fragment给activity传值

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决...
    闲庭阅读 3,312评论 0赞 10
  • 下面是我对关于fragment官方文档的翻译,加了些自己的理解。 前言 Fragment 表示 Activity ...
    走在路上的日子阅读 2,586评论 0赞 22
  • 一 Activity 1 Activity 生命周期 1.1 Activity 的四种状态 running 当前...
    _执_念__阅读 10,900评论 0赞 91
  • 本节例程下载地址:WillFlowFragment 究竟要如何使用碎片才能充分地利用平板屏幕的空间呢?假如我们正在...
    常思行阅读 669评论 0赞 3
  • 《Android Fragment完全解析,关于碎片你所需知道的一切》 我们都知道,Android上的界面展示都是...
    cxm11阅读 2,430评论 2赞 19

友情链接更多精彩内容