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页面文件
<?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的布局文件
<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的类文件
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的类文件
@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布局文件
<?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的布局文件
<?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
public interface ICallBack {
void getMessageFromFragment(String message);
}
4.设置fragment的类文件
@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的类文件
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);
}
});
}
});
}
}
小蘑菇

浙公网安备 33010602011771号