Fragment和Activity通信以及Fragment之间通信的两种方式
Fragment之间通信很简单……这里只是提供思路,无论是Fragment和Activity还是Fragment之间,通信的原理都是如此,一通百通,后面会附上DEMO
Activity。
FragmentManager的findFragmentByTag或者findFragmentById拿到我们需要通信的FragmentTabHost,所以就使用拿到Fragment是直接在XML中定义的,那么就使用Fragment),然后就可以对拿到的方式2
如果直接在Fragment里面操作其他的Fragment之间通信。
因为太简单了,所以直接帖代码了
Demo1
1 重写fragment_home.xml和fragment_message.xml(只在RelativeLayout 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"
- >
- <TextView
- android:id="@+id/tv"
- android:layout_centerInParent="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="25sp"
- android:text="Fragment Home" />
- <Button
- android:id="@+id/btn_home"
- android:layout_below="@id/tv"
- android:layout_centerHorizontal="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="获取来自Fragment Message的数据"/>
- </RelativeLayout>
RelativeLayout 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" >
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="Fragment Message"
- android:textSize="25sp" />
- <Button
- android:id="@+id/btn_message"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/tv"
- android:layout_centerHorizontal="true"
- android:text="获取来自Fragment Home的数据" />
- </RelativeLayout>
再来看看FragmentHome的代码,很简单……
class FragmentHome extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_home, null);
- setupView(view);
- return view;
- }
- private void setupView(View view) {
- Button Btn1 = (Button) view.findViewById(R.id.btn_home);// 获取按钮资源
- Btn1.setOnClickListener(new Button.OnClickListener() {// 创建监听
- public void onClick(View v) {
- MainTab activity = ((MainTab) getActivity());
- FragmentManager manager = activity.getSupportFragmentManager();
- FragmentMessage fragment = (FragmentMessage) manager
- .findFragmentByTag("tab2");
- if (null != fragment) {
- View vw = fragment.getView();
- if (null != vw) {
- TextView txt = (TextView) vw.findViewById(R.id.tv);
- T.showShort(getActivity(), txt.getText().toString());
- }
- } else {
- T.showShort(getActivity(), "FragmentMessage还未创建");
- }
- }
- });
- }
- }
这里做一下简单说明:
MainTab
activity = ((MainTab) getActivity());得到当前绑定的Activity实例对象
FragmentManager
FragmentMessage
fragment = (FragmentMessage) manager.findFragmentByTag("tab2");
拿到需要操作的FragmentMessage代码类似
- public class FragmentMessage extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_message, null);
- setupView(view);
- return view;
- }
- private void setupView(View view) {
- Button Btn1 = (Button)view.findViewById(R.id.btn_message);//获取按钮资源
- Btn1.setOnClickListener(new Button.OnClickListener(){//创建监听
- public void onClick(View v) {
- MainTab activity = ((MainTab) getActivity());
- FragmentManager manager = activity.getSupportFragmentManager();
- FragmentHome fragmentHome = (FragmentHome) manager.findFragmentByTag("tab1");
- if (null != fragmentHome) {
- View vw = fragmentHome.getView();
- if (null != vw) {
- TextView txt = (TextView) vw
- .findViewById(R.id.tv);
- T.showShort(getActivity(), txt.getText().toString());
- }
- } else {
- T.showShort(getActivity(), "FragmentHome还未创建");
- }
- }
- });
- }
- }
效果图1(首次打开时点击,FragmentMessage还未创建)
效果图2(切换到消息Tab,获取到了)
效果图3(再次切换到首页Tab,获取到了FragmentMessage中TextView的值
fragment_home.xml和(只在)
RelativeLayout 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"
- >
- <TextView
- android:id="@+id/tv"
- android:layout_centerInParent="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="25sp"
- android:text="Fragment Home" />
- <Button
- android:id="@+id/btn_home"
- android:layout_below="@id/tv"
- android:layout_centerHorizontal="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="点击把值传给Fragment Message"/>
- </RelativeLayout>
- <RelativeLayout 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" >
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="Fragment Message"
- android:textSize="25sp" />
- </RelativeLayout>
FragmentHome
- <pre name="code" class="java">public class FragmentHome extends Fragment {
- OnButtonclickListener mCallback;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_home, null );
- setupView(view);
- return view;
- }
- private void setupView(View view) {
- Button Btn1 = (Button)view.findViewById(R.id.btn_home);//获取按钮资源
- final TextView tv = (TextView) view.findViewById(R.id.tv);
- Btn1.setOnClickListener(new Button.OnClickListener(){//创建监听
- public void onClick(View v) {
- mCallback.OnButtonclicked(tv.getText().toString());
- }
- });
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- mCallback = (OnButtonclickListener) activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString()
- + " must implement OnButtonclickListener");
- }
- }
- public interface OnButtonclickListener {
- public void OnButtonclicked(Object object);
- }
- }
说明:首先定义接口,然后在Button的点击事件中回调
再来看看MainTab,实现 OnButtonclickListener接口,然后重写OnButtonclicked方法就可以了
- @Override
- public void OnButtonclicked(Object object) {
- FragmentMessage fMessage = (FragmentMessage) getSupportFragmentManager()
- .findFragmentByTag("tab2");
- if (fMessage != null) {
- View vw = fMessage.getView();
- if (null != vw) {
- TextView txt = (TextView) vw.findViewById(R.id.tv);
- txt.setText(object.toString());
- }
- }else {
- T.showShort(MainTab.this, "FragmentMessage还未创建");
- }
- }
最后看看效果
FragmentHome中FragmentMessage)
效果图2(切换到消息Tab,Fragment
Message)
效果图3(再次切换到首页Tab,点击,再FragmentHome中FragmentMessage)
Demo1下载地址:http://download.csdn.net/detail/yalinfendou/8539469
Demo2下载地址:http://download.csdn.net/detail/yalinfendou/8539655

浙公网安备 33010602011771号