Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Fragment 與 Activity 間的資料傳遞(Fragment 取得資料)

編輯:關於 Android 程式設計

Fragment 與 Activity 之間的資料交換大致分為三種:一、Fragment 從 Activity 取得資料;二、Activity 從 Fragment 取得資料;三、Fragment 之間互相取得資料。本文聚焦第一種——Fragment 如何從 Activity 取得資料。

效果:Activity 將資料傳給兩個 Fragment,Fragment 取得後展示出來。

源碼:

activity_main 布局

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <LinearLayout android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="2"
        android:background="#CCCCCC">
    </LinearLayout>
    <LinearLayout android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="2">
    </LinearLayout>
</LinearLayout>

Fragment 布局 f1 與 f2

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity

關鍵在於透過 Bundle 搭配 setArguments() 傳遞資料:

public class MainActivity extends Activity {
    private FragmentManager manager;
    private FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        MyFragment1 fragment1 = new MyFragment1();
        Bundle bundle1 = new Bundle();
        bundle1.putString("id", "Activity發送給MyFragment1的資料");
        fragment1.setArguments(bundle1);
        transaction.replace(R.id.left, fragment1, "left");
        MyFragment2 fragment2 = new MyFragment2();
        Bundle bundle2 = new Bundle();
        bundle2.putString("id", "Activity發送給MyFragment2的資料");
        fragment2.setArguments(bundle2);
        transaction.replace(R.id.right, fragment2, "right");
        transaction.commit();
    }
}

MyFragment1(MyFragment2 類似)

public class MyFragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.f1, null);
        TextView textView = (TextView) view.findViewById(R.id.textView);
        Bundle bundle1 = getArguments();
        textView.setText(bundle1.getString("id"));
        return view;
    }
}

核心機制:Activity 透過 setArguments() 將資料放入 Bundle,Fragment 則在 onCreateView() 中以 getArguments() 取出,即可完成資料傳遞。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved