3-AVI–Activity與Fragment的資料傳遞
零、前言
[1].Activity向Fragment傳資料
[2].Fragment向Activity傳資料
[3].Fragment向Fragment傳資料
一、Activity向Fragment傳資料效果:
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>
二、Fragment向Activity傳資料效果:
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向Fragment傳資料據效果:
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.歡迎關注我的微信公眾號,最新精彩文章,及時送達:
相關文章
- Fragment傳值到ActivityFragment
- Activity跳轉時傳遞資料的騷操作
- bundle實現Activity之間的資料傳遞
- 將一個Activity中的資料傳到另一個Activity的Fragment中的方法Fragment
- Service實時向Activity傳遞資料案例
- 簡單的在兩個activity中傳遞資料
- Fragment與Activity通訊Fragment
- activity_main與fragment_mainAIFragment
- Android Fragment 間物件傳遞AndroidFragment物件
- android Fragment與Activity互動,互相發資料(附圖詳解)AndroidFragment
- android 中Service 和activity之間的資料傳遞的幾種方法Android
- Activity、Fragment和IntentFragmentIntent
- fragment之間相互傳資料、共享資料Fragment
- react元件與元件之間的資料傳遞React元件
- chan中傳遞map資料,傳遞的是引用
- Activity跳到指定的Fragment的方法Fragment
- 監聽activity、fragment生命Fragment
- VUE 傳遞資料Vue
- Activity 與 Fragment 通訊(99%)完美解決方案Fragment
- Flutter 中的資料傳遞Flutter
- 父子元件的資料傳遞元件
- RN與原生互動(二)——資料傳遞
- iOS應用之間的跳轉與資料傳遞iOS
- 值傳遞與引用傳遞
- Activity回傳資料方法startActivityForResult onActivityResult
- Android知識點複習1(Activity與Fragment)AndroidFragment
- 淺談Vue元件傳遞資料與通訊Vue元件
- Activity和fragment是如何互動的Fragment
- 向上向下傳遞資料
- Android--單Activity+多Fragment,玩轉FragmentAndroidFragment
- 【Android】在Activity頁面中如何實現Fragment資料的緩載入AndroidFragment
- 關於Activity之間傳送資料
- 【開發經驗】Flutter元件的事件傳遞與資料控制Flutter元件事件
- Vue 子元件和父元件傳遞資料與方法的例子Vue元件
- 淺入深出Vue:子元件與資料傳遞Vue元件
- Activity的跳轉與傳值薦
- 不同資料庫間傳遞資料的問題資料庫
- activity和fragment中startactivityforresult方法的區別Fragment