3-AVI--Activity与Fragment的数据传递

简介: 零、前言[1].Activity向Fragment传数据[2].Fragment向Activity传数据[3].Fragment向Fragment传数据一、Activity向Fragment传数据效果:ac2fg.

零、前言

[1].Activity向Fragment传数据
[2].Fragment向Activity传数据
[3].Fragment向Fragment传数据

一、Activity向Fragment传数据效果:

ac2fg.gif
1.Ac2FgActivity.java
public class Ac2FgActivity extends AppCompatActivity {

    @BindView(R.id.tv_data_content)
    EditText mTvDataContent;
    @BindView(R.id.btn_send_data)
    Button mBtnSendData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ac2fg);
        ButterKnife.bind(this);
        initFragment();

    }

    private void initFragment() {
        FragmentManager fm = getFragmentManager();//1.获取FragmentManager
        FragmentTransaction ft = fm.beginTransaction();//2.fm开启事务
        //3.动态添加 (控件id,fragment对象,tag)
        ft.add(R.id.fl_fragmemt_content, new ResultFragment());
        ft.commit();//4.提交事务
    }

    @OnClick(R.id.btn_send_data)
    public void onViewClicked() {

        String result = mTvDataContent.getText().toString().trim();//获取输入结果
        ResultFragment rfg = new ResultFragment();//创建ResultFragment对象
        Bundle bundle = new Bundle();//创建Bundle对象
        bundle.putString("data", result);//为bundle赋值
        rfg.setArguments(bundle);//为ResultFragment设置Arguments
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fl_fragmemt_content, rfg);//替换之前的
        ft.commit();
    }
}
2.activity_ac2fg.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/tv_data_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="I'm Activity:请输入传入fragment的信息"/>

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_data_content"
        android:layout_centerHorizontal="true"
        android:text="点击传值"/>

    <FrameLayout
        android:id="@+id/fl_fragmemt_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/btn_send_data">
    </FrameLayout>
</RelativeLayout>
3.ResultFragment.java
public class ResultFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content, container, false);
        TextView f_tv_content = view.findViewById(R.id.f_tv_content);
        Bundle bundle = getArguments();
        if (bundle != null) {
            String reslut = bundle.getString("data");
            f_tv_content.setText(reslut);
        }
        return view;
    }
}

4.fragment_content.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <TextView
        android:id="@+id/f_tv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="#D5DBDD"
        android:text="I'm Fragment"
        android:textSize="24dp"/>

</RelativeLayout>

Activity向Fragment传数据.png


二、Fragment向Activity传数据效果:

fg2ac.gif
1.Fg2AcActivity.java
public class Fg2AcActivity extends AppCompatActivity{


    @BindView(R.id.activity_tv_content)
    TextView mActivityTvContent;
    @BindView(R.id.fl_fragmemt_send)
    FrameLayout mFlFragmemtSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fg2ac);
        ButterKnife.bind(this);
        initFragment();

    }

    private void initFragment() {
        FragmentManager fm = getFragmentManager();//1.获取FragmentManager
        FragmentTransaction ft = fm.beginTransaction();//2.fm开启事务
        //3.动态添加 (控件id,fragment对象,tag)
        ft.add(R.id.fl_fragmemt_send, new SendFragment());
        ft.commit();//4.提交事务
    }

    /**
     * 设置数据方法
     * @param str
     */
    public void setDate(String str) {
        if (str != null && !"".equals(str)) {
            mActivityTvContent.setText(str);
        }
    }
}
2.activity_fg2ac.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/activity_tv_content"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="I'm Activity:"
        android:textSize="24dp"/>

    <FrameLayout
        android:id="@+id/fl_fragmemt_send"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/activity_tv_content">
    </FrameLayout>
</RelativeLayout>

3.SendFragment.java
public class SendFragment extends Fragment {

    private EditText tv_data_content;
    private Button btn_send_data;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_send, container, false);
        //可实现控件事件
        tv_data_content = view.findViewById(R.id.tv_data_content);
        btn_send_data = view.findViewById(R.id.btn_send_data);
        btn_send_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = tv_data_content.getText().toString().trim();
                ((Fg2AcActivity)getActivity()).setDate(result);
            }
        });

        return view;
    }
}

4.fragment_send

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:background="@color/qq_line"
                android:layout_height="match_parent">

    <EditText
        android:id="@+id/tv_data_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="I'm Fragment:请输入传入Activity的信息"/>

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_data_content"
        android:layout_centerHorizontal="true"
        android:text="点击传值"/>

</RelativeLayout>
Fragment向Activity传数据.png


三、Fragment向Fragment传数据据效果:

fg2fg.gif
1.Fg2FgActivity.java
public class Fg2FgActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fg2fg);
        initFragment();

    }
    private void initFragment() {
        FragmentManager fm = getFragmentManager();//1.获取FragmentManager
        FragmentTransaction ft = fm.beginTransaction();//2.fm开启事务
        //3.动态添加 (控件id,fragment对象,tag)
        ft.add(R.id.f_left, new LeftFragment());
        ft.add(R.id.f_right, new RightFragment());
        ft.commit();//4.提交事务
    }
}
2.activity_fg2fg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <FrameLayout
        android:id="@+id/f_left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/f_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>

</LinearLayout>
3.LeftFragment.java
public class LeftFragment extends Fragment {
    EditText mTvDataContent;
    Button mBtnSendData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_left, container, false);
        mTvDataContent = view.findViewById(R.id.tv_data_content);
        mBtnSendData = view.findViewById(R.id.btn_send_data);

        mBtnSendData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = mTvDataContent.getText().toString().trim();
                TextView mtv = getActivity().findViewById(R.id.right_content);
                mtv.setText(str);
            }
        });
        return view;
    }
}

4.fragment_left.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <EditText
        android:id="@+id/tv_data_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="I'm left fragment"/>

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_data_content"
        android:layout_centerHorizontal="true"
        android:text="点击传值"/>
</LinearLayout>
5.RightFragment.java
public class RightFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_right, container, false);
        return view;
    }
}
6.fragment_right.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#eee"
              android:orientation="vertical">

<TextView
    android:id="@+id/right_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="I'm right fragment"/>

</LinearLayout>


后记、

1.声明:

[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力

2.连接传送门:

更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

3.联系我

QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328

4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
公众号.jpg
相关文章
|
27天前
activity中加载fragment的控件 在fragment 中调用activity中的控件
activity中加载fragment的控件 在fragment 中调用activity中的控件
10 0
|
XML 移动开发 Android开发
activity中加载fragment的控件]
fragment它自己的中文意思:碎片; 一个可以将activity拆分成几个完全独立封装的可重用的组件,每个组件有自己的生命周期和ui布局。
247 1
activity中加载fragment的控件]
|
Android开发
Android--fragment与activity及两个fragment之间的跳转实现
在应用的交互中,我可能需要实现: 从当前的fragment跳转到另一个fragment 从当前的fragment跳转到一个activity中 从当前的activity跳转到一个fragment中 网上提供的思路较多,这里总结了一套自己的方法。
1847 0
|
Android开发
5-AVI--Fragment简单封装
零、前言 [1].每次写Fragment要加载布局,为布局设置内容,挺麻烦的,搞个基类简单封装一下吧 [2].一般封装基类使用模板方法设计模式,基类中做一些常用的不变东西,需要拐点弯的逻辑就弄个抽象方法延迟到子类 [3].
1002 0
|
Android开发 容器
1-AVI--Fragment基础使用
零、前言 [1].Fragment静态使用 [2].Fragment动态使用 一、Fragment静态使用 静态fragment.jpg 1.
1170 0
|
Android开发 容器
4-AVI--Fragment与ViewPager结合
零、前言 [1].认真看下图Fragment在ViewPager里的默认生命周期(可在浏览器中单独打开网页查看动图,清晰很多) [2].名字数据随机获取见:随机数据生成 [3].
1094 0
|
测试技术 容器
2-AVI--Fragment生命周期测试
零、前言 [1].两个Fragment,点击左右按钮切换相应的Fragment [2].观察Activity 和两个Fragment生命周期变化 [3].
1091 0