Activity向Fragment传值

来源:互联网 发布:电视机看电视软件 编辑:程序博客网 时间:2024/05/01 19:00

这里只介绍在创建Fragment对象的时候,怎么给Fragment传递值。因为最近比较忙,过一阵会发表具体介绍Fragment的使用文章
在原来看书学习的时候,书中介绍到很多传值得方法,但推荐使用Bundle对象传递

让Fragment显示有两种方式,但是<fragment>标签这样的方式是不推荐的,显得中规中矩,后期也不是很好操作和维护,这里利用FragmentManager的方式添加到布局中

代码如下:
布局:(简单到不行,就是一个RelativeLayout,一会儿用FragmentManager的方式将Fragment添加进去)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/relativeLayout_show"    android:layout_width="match_parent"    android:layout_height="match_parent" ></RelativeLayout>

MainActivity:

//创建Fragment对象的时候,使用Bundle对象传值给Fragment@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //创建Fragment对象,并通过Bundle对象传递值    MyFragment fragment = new MyFragment();    Bundle bundle = new Bundle();    bundle.putString("key", "value");    fragment.setArguments(bundle);    //利用FragmentManager对象和事物对象添加到布局中    FragmentManager fragmentManager = getFragmentManager();    FragmentTransaction transaction  = fragmentManager.beginTransaction();    transaction.add(R.id.relativeLayout_show, fragment;    transaction.commit();}

MyFragment:

//拿到Bundle对象,如果有参数传递,就拿到值并赋值给str@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {    Bundle bundle = this.getArguments();    if (bundle != null)    {        String str = bundle.getString("key");    }    TextView textView = new TextView(getActivity());    textView.setText("上上下下的享受");//是电梯,别误会    return textView;}

==========还有一种写法,也是用Bundle对象,是我看视频后封装的==========

个人感觉这种写法比较屌,因为考虑是面向对象的话,传递值这样的事情应该由Fragment来做,也就是你(Fragment)要什么值,我(Activity)就给你什么值,这样看上去很牛B,用的话也是一目了然,废话不多说上代码了,为了各位看着方便,小弟在这里直接写一个类里面了,方便观看。开发中千万不要这么写啊。。。

public class MainActivity extends Activity {    @SuppressLint("NewApi")    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        FragmentManager fragmentManager = getFragmentManager();        FragmentTransaction transaction = fragmentManager.beginTransaction();        MyFragment fragment = MyFragment.newMyFragment("给老子过去");//如果不想传值,给过去一个null就行        transaction.add(R.id.relativeLayout_show, fragment);        transaction.commit();    }}/*========================Fragment↓==========================*/class MyFragment extends Fragment {    String mValue;// 用于接收Activity传过来的数值    private static final String MARK = "mark";// 设置标记    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)     {        // 那到Bundle对象,如果有参数传递,就拿到值并赋值给mValue        Bundle bundle = this.getArguments();        if (bundle != null)        {            mValue = bundle.getString(MARK);        }        TextView textView = new TextView(getActivity());        textView.setText(mValue);        return textView;    }    /**     * 别通过new对象拿到Fragment了,直接调用我就行了     * @param value 想要拿到我,好啊!给我值(value)     * @return fragment     */    public static MyFragment newMyFragment(String value)    {        //将fragment绑定参数        Bundle bundle = new Bundle();        bundle.putString(MARK, value);        MyFragment fragment = new MyFragment();        fragment.setArguments(bundle);        return fragment;    }}

好!activity向fragment传值就分享到这里!

if(有疑问){    欢迎QQ骚扰(997797281);}
1 1
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机内存小了怎么办 华为平板停止运行怎么办 华为p6一直黑屏怎么办 华为手机root卡重启怎么办 华为手机变砖怎么办 华为手机丢了怎么办? 蓝牙耳机不闪烁怎么办 金立手机打不开怎么办 华为蓝牙不能用怎么办 iphone蓝牙坏了怎么办 苹果蓝牙坏了怎么办 苹果x蓝牙连不上怎么办 手机耳机槽松了怎么办 蓝牙连接不上怎么办 蓝牙开不了机怎么办 蓝牙耳机听不了怎么办 路虎车门打不开怎么办 蓝牙连接声音小怎么办 手机丢在高铁上怎么办 电信卡注销欠费怎么办 手机和卡都丢了怎么办 老干妈打不开盖子怎么办 苹果键盘电池仓打不开怎么办 苏泊尔电压力锅打不开盖子怎么办 honorv9声音太小怎么办 异地手机卡丢了怎么办 中国移动卡丢了怎么办 mate8麦克风坏了怎么办 qq空间被禁赞了怎么办 苹果老耳机模式怎么办 华为手机无声音怎么办 华为手机声音不正常怎么办 带耳机不能说话怎么办 耳机说话没声音怎么办 手机耳机说话没声音怎么办 不接爸爸电话怎么办 耳机自动调高怎么办 手机扩音器坏了怎么办 耳机有电流麦怎么办 耳机自动调音量怎么办 耳机接口总是坏怎么办