Activity向Fragment传值

这里只介绍在创建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
@Override
protected 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
@Override
public 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);
}
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值