14 fragment传值
两个fragment传值
方式一
布局文件代码:
<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:orientation="horizontal"
tools:context=".MainActivity" > <!-- 静态 必须要写ID--> <fragment
android:id="@+id/fragment1"
android:name="com.qf.day14_fragment_4.fragment.MyFragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#0f0" />
<!-- 动态 --> <LinearLayout
android:id="@+id/ll_fragment_id"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" /> </LinearLayout>布局文件逻辑代码:
package com.qf.day14_fragment_4; import com.qf.day14_fragment_4.fragment.MyFragment2; import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //管理者对象
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction(); MyFragment2 myFragment2 = new MyFragment2(); transaction.replace(R.id.ll_fragment_id,myFragment2); transaction.commit();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
其中一个fragment逻辑代码:
package com.qf.day14_fragment_4.fragment; import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button; import com.qf.day14_fragment_4.R; public class MyFragment1 extends Fragment{ private Button btn; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment1_layout, container, false); btn = (Button) v.findViewById(R.id.btn);
//点击按钮传值
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub //管理者对象
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction(); MyFragment2 myFragment2 = new MyFragment2(); Bundle bundle = new Bundle(); bundle.putString("msg", "左边Fragment向右边Fragment传值"); myFragment2.setArguments(bundle); transaction.replace(R.id.ll_fragment_id, myFragment2); transaction.commit();
}
});
return v;
}
}
方式二 前提:必须两个静态fragment
界面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:orientation="horizontal"
tools:context=".MainActivity" > <fragment
android:id="@+id/leftfragment"
android:name="com.qf.day14_fragment_demo5.fragment.Fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#00f" /> <fragment
android:id="@+id/rightfragment"
android:name="com.qf.day14_fragment_demo5.fragment.Fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>其中核心fragment代码
package com.qf.day14_fragment_demo5.fragment; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import com.qf.day14_fragment_demo5.R; public class Fragment1 extends Fragment{ private EditText etContent;
private Button btnSend; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub View view = inflater.inflate(R.layout.layout01, container, false);
etContent = (EditText) view.findViewById(R.id.et_content);
btnSend = (Button) view.findViewById(R.id.btn_send); btnSend.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub String msg = etContent.getText().toString().trim(); //通过Fragment的id 获取Fragment2的对象
// Fragment2 fragment2 = (Fragment2) getFragmentManager().findFragmentById(R.id.rightfragment);
// fragment2.setTextValues(msg); //TextView tv = (TextView) getFragmentManager().findFragmentById(R.id.rightfragment).getView().findViewById(R.id.tv_show);
TextView tv = (TextView) getActivity().findViewById(R.id.tv_show);
tv.setText(msg); } }); return view;
} }
fragment给界面传值(Activity) 接口回调
界面代码
package com.qf.day14_fragment_demo3; import com.qf.day14_fragment_demo3.callback1.CallBackValue; import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity implements CallBackValue{ private TextView tv; @SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.ll_fragment_id, new MyFragment());
transaction.commit();
} //回调接口的方法
@Override
public void sendMessage(String msg) {
// TODO Auto-generated method stub
tv.setText(msg);
} }
接口代码
package com.qf.day14_fragment_demo3.callback1; public interface CallBackValue { public void sendMessage(String msg); }
fragment代码
package com.qf.day14_fragment_demo3; import com.qf.day14_fragment_demo3.callback1.CallBackValue; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText; @SuppressLint("NewApi")
public class MyFragment extends Fragment{ CallBackValue callBackValue; private EditText et;
private Button btnFTA; /**
* 与Activity第一次链接是调用
*/
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity); //Fragment归属的Activity是getActivity
callBackValue = (CallBackValue) getActivity();
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_layout, container,false);
et = (EditText) v.findViewById(R.id.et);
btnFTA = (Button) v.findViewById(R.id.btn_FTA); btnFTA.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//将值传给Activity
String msg = et.getText().toString().trim(); callBackValue.sendMessage(msg);
}
}); return v;
} }
fragment 获取 界面数据(Activity)
- 可以fragment中直接getActivity 或者getContext 获取其对象
- 或者动态创建时用setAgunment 在fragment中getAgument
14 fragment传值的更多相关文章
- 14 fragment 创建
静态展示 注意 静态的开始进入界面的生命周期和动态的不同 详情:14 fragment注意点 步骤一:创建一个类继承 Fragment 代码类型一: package com.fmy.demo1; im ...
- 14 Fragment 碎片总结
Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...
- Android FragmentActivity 给Fragment传值
1.Fragment给Activity传值 定义一个在fragment 中 定义interface 监听器,让activity实现,并将activity的引用传递给fragment.即setListe ...
- 【android】activity、fragment传值例子
1:Activity篇 1.1向Activity传值 关键点在于putExtra.如果传递类的话,记得类实现Serializable接口 Intent intent = new Intent(Firs ...
- Activity向Fragment传值
发送数据 //Activity传值,通过Bundle Bundle bundle = new Bundle(); bundle.putString("MainActivity", ...
- 【Android】安卓开发之activity如何传值到fragment,activity与fragment传值
作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 大家知道,我们利用activity使 ...
- 两个activity或者activity和fragment传值
使用Fragment的时候可能需要在两个Fragment之间进行参数的传递,开始想着可以使用SharedPreferences进行处理,想想这些简单的参数没有必要使用这么麻烦的方式去实现,翻了一下Fr ...
- 14 Fragment的V4包的使用
activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- 14 Fragment 注意点
API 过时问题 API 23过时 public void onAttach(Activity activity)替换为public void onAttach(Context context) 注意 ...
随机推荐
- [CEOI2008]order
Description 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成. 现在给出这些参数,求最大利润 Solu ...
- 习题10-1 UVA 11040(无聊水一水)
题意: 给你一个残缺的塔,每个数字由他下面左右两个数相加得.给你其中一部分,要求输出全部的数字. #include <iostream> #include <cstdio> # ...
- [2017/5/28]FJ四校联考
来自FallDream的博客,未经允许,请勿转载,谢谢. 话说这一段时间算是过去了,好久好久之后终于又有联考了 没想到这次到我们学校出题,昨天才想起来,临时花一天赶了一套,我出了一个sbFFT,质量 ...
- Fabrik – 在浏览器中协作构建,可视化,设计神经网络
Fabrik是一个在线协作平台,通过简单的拖放界面来构建,可视化和训练深度学习模型. 它允许研究人员使用Web GUI协同开发和调试模型,该GUI支持导入,编辑和导出广泛流行的框架(如Caffe,Ke ...
- 阿里技术一面,Java研发岗
之前过了个简单的简历面,过了几天后没打来以为凉了,然后昨晚又接到了电话,括号内容是回答说的,理解有限,不一定都对,欢迎纠正-加油每一个牛友们! 阿里一面: 1.学过哪些技术知识呢? 2.说说接口和抽象 ...
- Servlet init()
有时候希望在servlet首次载入时,执行复杂的初始化任务,但并不想每个请求都重复这些任务的时候,用init()方法他在servlet初次创建时被调用,之后处理每个用户的请求时,则不在调用这个方法.因 ...
- struts2 Action获取表单传值(属性,类))
http://blog.csdn.net/sd0902/article/details/8393157 求大神告知两种方法的不同点 都是写个set方法就行了
- phpstorm查看类的继承关系
在看一些框架源码时,有些类有很多的继承或者接口,有一款神奇的帮助很重要 选中一个类文件,右键,选择diagrams->show diagrams 即可得到类的继承关系,如上右图 使用函数 fun ...
- AsyncLocal 与 async await
大家来看一张图 先猜猜看为什么会这样 关于async await的原理 建议查看 https://blog.csdn.net/brook_shi/article/details/50803957 这篇 ...
- pentaho cde 自定义复选下拉框 checkbox select
pentaho 自带的component 虽多,但是当用户需要在一个表格中查看多个组别的数据时,pentaho自带的单选框就不能实现了,所以复选下拉框势在必行,实现效果如下: 实现原理是借用了jqu ...