当Activity类动态加载fragment时可以通过fragment的setArguments()传入值,并在fragment类中通过fragment的getArguments()方法获得传入的值;
下面演示一下代码:
MainActivity.java:
主要步骤(该步骤必须在fragmentTransaction.commit()之前完成):
1.创建一个fragment实例
2.创建一个Bundle 对象(其为键值对存储,可以理解为一个Map<K,V>): Bundle bundle = new Bundle();
3.向bundle中传值:bundle.putString("str","这是MainActivity传来的值~");
4.将bundle通过setArguments(bundle)方法设置进fragment01:fragment01.setArguments(bundle);
- 1
- 2
- 3
- 4
- 5
package activity.wyc.com.fragmentdemo0305;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import com.vrinux.fragment.Fragment01;
public class MainActivity extends ActionBarActivity {
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment01 fragment01 = new Fragment01();
Bundle bundle = new Bundle();
bundle.putString("str","这是MainActivity传来的值~");
fragment01.setArguments(bundle);
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fm01id,fragment01);
fragmentTransaction.commit();
}
}
- 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
Fragment01.java:
获取传来的值:
String str = (String)getArguments().get(“str”);
package com.vrinux.fragment;
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;
import activity.wyc.com.fragmentdemo0305.R;
/**
* Created by yc on 2015/3/5.
*/
public class Fragment01 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment01layout, null);
TextView tvObj = (TextView)view.findViewById(R.id.fm01tvid);
String str = (String)getArguments().get("str");
tvObj.setText(str);
return view;
}
}
- 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
activity_main.xml:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fm01id"></FrameLayout>
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
fragment01layout.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:id="@+id/fm01tvid"/>
</LinearLayout>