Fragment间的通信
Fragment通信
主要说的是activity与fragment的数据传递、fragment与activity的数据传递和fragment与fragment之前的数据传递
一、activity与fragment之间的通信
采用 Bundle方式。具体Demo步骤如下:
步骤1:Activity的布局文件act_to_fragment.xml
步骤2:设置 Fragment的布局文件fragment_com.xml
步骤3:设置Activity的类文件ActToFragment.java
步骤4:设置Fragment的类文件fragment_com.java
1.activity页面文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <? xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ActToFragment" android:orientation="vertical"> < TextView android:id="@+id/text" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="我是Activity" /> < FrameLayout android:layout_below="@+id/button" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="500dp"/> </ LinearLayout > |
2.设置 Fragment的布局文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent" > < TextView android:id="@+id/com_vfa" android:text="我是fragment" android:layout_gravity="center" android:textSize="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> < TextView android:id="@+id/text" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="等待Activity发送消息" /> < Button android:id="@+id/button" android:layout_gravity="center" android:text="点击接收Activity消息" android:layout_centerInParent="true" android:textSize="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </ LinearLayout > |
3.设置activity的类文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class ActToFragment extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.act_to_fragment); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //创建需要添加的fragment; FragmentCom fragmentCom = new FragmentCom(); //创建bundle对象并将数据传递到fragment中去 Bundle bundle = new Bundle(); bundle.putString( "message" , "I love Google" ); fragmentCom.setArguments(bundle); transaction.add(R.id.fragment_container,fragmentCom); transaction.commit(); } } |
4. fragment的类文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_com, container, false ); TextView textView = (TextView) view.findViewById(R.id.text); Button button = (Button) view.findViewById(R.id.button); //通过getArguments获取bundle对象 Bundle bundle = this .getArguments(); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(bundle.getString( "message" )); } }); return view; } |
二、fragment与activity的通信
fragment传递消息到activity
1、定义接口
2、实现接口
3、消息传递
1.设置activity布局文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <? xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".FragmentToActActivity" android:orientation="vertical" > < TextView android:id="@+id/text_second" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="等待Fragment发送消息" /> < Button android:id="@+id/button_second" android:layout_below="@+id/text" android:text="点击接收Fragment消息" android:layout_centerInParent="true" android:textSize="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> < FrameLayout android:layout_below="@+id/selected" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="500dp"/> </ LinearLayout > |
2.设置Fragment的布局文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <? 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/fragment" android:text="我是fragment" android:gravity="center" android:textSize="30dp" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorAccent"/> </ LinearLayout > |
3.设置回调接口:ICallBack.java
1 2 3 4 | public interface ICallBack { void getMessageFromFragment(String message); } |
4.设置fragment的类文件
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_com_sencond, container, false ); } //实现定义的接口 public void sendMessage(ICallBack iCallBack){ iCallBack.getMessageFromFragment( "我是来自fragment的消息" ); } |
5.设置activity的类文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class FragmentToActActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_fragment_to_act); TextView textView = (TextView) findViewById(R.id.text_second); Button button = (Button) findViewById(R.id.button_second); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); ComSencondFragment comSencondFragment = new ComSencondFragment(); transaction.add(R.id.fragment_container, comSencondFragment).commit(); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { comSencondFragment.sendMessage( new ICallBack() { @Override public void getMessageFromFragment(String message) { textView.setText(message); } }); } }); } } |
小蘑菇
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix