Fragment與Activity間的通信
Activity向Fragment傳值
Activity向引入的Fragment傳值步驟:
1.Activity中創建Fragment對象,調用setArguments(bundle)方法儲存值
2.Fragment中調用getArguments()獲取傳遞的bundle對象解析獲取具體值
示例代碼
MainActivity.java
package com.example.activitytofragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText et_content;
private FragmentManager manager;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_content = findViewById(R.id.et_content);
manager = getFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.ll_content,new ResultFragment());
transaction.commit();
}
/**
* 點擊按鈕將EditText中輸入的文本傳入ResultFragment中
*/
public void sendValue(View view){
// 獲取輸入的文本信息
String info = et_content.getText().toString().trim();
ResultFragment fragment = new ResultFragment();
// 創建Bundle對象,把需要傳遞的數據存到bundle裏,然後調用fragment的setArguments()方法傳遞bundle
Bundle bundle = new Bundle();
bundle.putString("to",info);
fragment.setArguments(bundle);
// 開啓事務
transaction = manager.beginTransaction();
// 替換之前沒有數據的Fragment
transaction.replace(R.id.ll_content,fragment);
// 提交事務
transaction.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.activitytofragment.MainActivity">
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/et_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點擊傳值"
android:layout_centerInParent="true"
android:gravity="center"
android:layout_below="@id/et_content"
android:onClick="sendValue"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@id/btn"
android:id="@+id/ll_content">
</LinearLayout>
</RelativeLayout>
ResultFragment.java
package com.example.activitytofragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ResultFragment extends Fragment {
private TextView tv_content;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, null);
tv_content = view.findViewById(R.id.tv_content);
// 先調用getArguments()方法獲取bundle bundle對象中根據key獲取傳遞的數據,再展示到TextView中
Bundle bundle = getArguments();
if (bundle != null) {
String info = bundle.getString("to");
tv_content.setText(info);
}
return view;
}
}
fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_content"
android:textSize="30sp"
android:gravity="center"/>
</android.support.constraint.ConstraintLayout>
效果圖
Fragment向Activity傳值
Fragment向所在Activity傳值步驟:
1.Fragment中定義傳值的回調接口,在生命週期onAttach()或者onCreate()方法中獲取接口的實現
2.Fragment需要傳值的位置調用接口回調方法傳值
3.Activity實現回調接口重寫回調方法獲取傳遞的值
代碼示例:
MainActivity.java
package com.example.fragmenttoactivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements ResourceFragment.MyListener{
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_show = findViewById(R.id.tv_show);
}
@Override
public void sendMessage(String str) {
// 判斷不等於空也不等於雙引號空格
if (str!=null&&!"".equals(str)){
// 回傳數據進行展示
tv_show.setText(str);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fragmenttoactivity.MainActivity">
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp" />
<fragment
android:id="@+id/fragment_resource"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tv_show"
android:name="com.example.fragmenttoactivity.ResourceFragment"/>
</RelativeLayout>
ResourceFragment.java
package com.example.fragmenttoactivity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class ResourceFragment extends Fragment {
private EditText et_content;
private Button button;
private MyListener listener;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 實例話MyListener,因爲Activity實現了MyListener,所以就是他的子類
listener = (MyListener) getActivity();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_resource,null);
et_content = view.findViewById(R.id.et_content);
button = view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 獲取EditText中的值
String info = et_content.getText().toString().trim();
// 接口回傳的形式回傳數據
listener.sendMessage(info);
}
});
return view;
}
// 定義接口 接口中定義回傳數據的方法
public interface MyListener{
public abstract void sendMessage(String str);
}
}
fragment_resource.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">
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/et_content" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/btn" />
</RelativeLayout>
效果圖
同一個Activity下的Fragment與Fragment之間的傳值
同一個Activity中不同Fragment之間的傳值有三種方法:
方法一:先得到FragmentManager,再調用findFragmentById()方法,根據id獲取到Fragment對象,調用fragment中的setTextView()方法賦值
方式二:先調用getFragmentManager()獲得FragmentManager對象,然後調用findFragmentById()方法獲得右側的Fragment對象,再調用getView()獲得右側的fragment的view對象,最後調用view的findViewById()方法獲得需要賦值的控件
方式三:先調用getActivity()獲取所屬Activity的對象,再通過activity的findViewById()獲取目標控件
代碼示例
MainActivity.java
package com.example.fragmenttofragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.fragmenttofragment.MainActivity">
<fragment
android:id="@+id/fragment_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.fragmenttofragment.LeftFragment"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.fragmenttofragment.RightFragment"
android:layout_weight="1" />
</LinearLayout>
LeftFragment.java
package com.example.fragmenttofragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LeftFragment extends Fragment {
private EditText et_content;
private Button btn_send;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left,null);
et_content = view.findViewById(R.id.et_content);
btn_send = view.findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String info = et_content.getText().toString().trim(); // 獲取輸入的內容
/* // 方式一:先得到FragmentManager,再調用findFragmentById()方法,根據id獲取到Fragment對象,調用fragment中的setTextView()方法賦值
RightFragment rightFragmenet = (RightFragment) getFragmentManager().findFragmentById(R.id.fragment_right);
rightFragmenet.setTextView(info);*/
/* // 方式二:先調用getFragmentManager()獲得FragmentManager對象,然後調用findFragmentById()方法獲得右側的Fragment對象,
// 再調用getView()獲得右側的fragment的view對象,最後調用view的findViewById()方法獲得需要賦值的控件
TextView tv_show = getFragmentManager().findFragmentById(R.id.fragment_right)
.getView().findViewById(R.id.tv_show);
tv_show.setText(info);*/
// 方式三:先調用getActivity()獲取所屬Activity的對象,再通過activity的findViewById()獲取目標控件
TextView tv_show = getActivity().findViewById(R.id.tv_show);
tv_show.setText(info);
}
});
return view;
}
}
fragment_left.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">
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="@string/et_content" />
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/btn_send" />
</RelativeLayout>
RightFragment.java
package com.example.fragmenttofragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RightFragment extends Fragment {
private TextView tv_show;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right,null);
tv_show = view.findViewById(R.id.tv_show);
return view;
}
// 定義一個方法給TextView賦值
public void setTextView(String text){
tv_show.setText(text);
}
}
fragment_right.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">
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp" />
</RelativeLayout>
效果圖