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
- activity向Fragment传值
- Activity向Fragment传值
- Activity向fragment传值
- Fragment向Activity传值
- Fragment向Activity传值
- Activity向Fragment传值
- Activity向Fragment传值
- Fragment向另一个Activity传值
- activity向另一个activity中的fragment传值
- 45 Android activity 向 fragment 传值
- 安卓中实现Activity向Fragment传值
- 安卓中回调实现Fragment向Activity传值
- 利用Bundle完成Activity向Fragment传值
- Activity向Fragment中传值
- Fragment向activity中传值
- Activity向Fragment中传值
- Activity向Fragment中传值
- Activity向Fragment中传值
- iOS多线程之NSThread
- zabbix 自定义 key
- WindowsServer2008 R2 Enterprise(ServerCore) + SqlServer2008 R2
- 使用DbVisualizer导出DB2创建序列SQL
- wordpress站点地址配置错误,无法访问网站
- Activity向Fragment传值
- 连续子数组的最大和
- 集合框架和泛型
- uCOS-II中Event阻塞、唤醒、超时机制(以Sem为例)
- FCM(Fuzzy C-Means)模糊C聚类
- BZoj 2190: [SDOI2008]仪仗队【欧拉函数】
- [Android]基于Android的Java学习路线图
- 如何在Linux上搭建VisualSVN Server(svn服务端)
- 机房重构—策略模式简单用