简单的fragment之间通信交流的实现

前言:本篇文章是利用fragment最基础的知识,仅供初学者观看,也为我以后要做相关知识做基础准备。如果是大神的话,浪费你几分钟看下也可以。

关于fragment那些api的基础知识,你可以点击这里查看Android Fragment 基本介绍 这篇文章将不会叙述。

首先,我们看下界面

左边的TextView会根据右边点击button的不同而改变。

下面开始介绍代码:

1.在layout里新建fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/fragment_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is fragment 1"
        android:textColor="#000000"
        android:textSize="25sp" />

</LinearLayout>

可以看出,这里就只有一个TextView

2.在layout里新建fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is fragment 2"
        android:textColor="#000000"
        android:textSize="25sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num 2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num 3" />

</LinearLayout>

这里是三个button

3.创建类Fragment1继承Fragment

package lgx.fram.framents;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment1, container, false);
	}
}

重写onCreateView()方法,这里return inflater.inflate(R.layout.fragment1, container, false);这句话是重点

4.创建类Fragment2继承Fragment

package lgx.fram.framents;

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.TextView;

public class Fragment2 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment2, container, false);
	}

	TextView textview;
	Button button, button2, button3;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		button = (Button) getActivity().findViewById(R.id.button);
		button2 = (Button) getActivity().findViewById(R.id.button2);
		button3 = (Button) getActivity().findViewById(R.id.button3);
		textview = (TextView) getActivity().findViewById(R.id.fragment_text);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				textview.setText(button.getText());
			}
		});
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				textview.setText(button2.getText());
			}
		});
		button3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				textview.setText(button3.getText());
			}
		});
	}
}

button = (Button) getActivity().findViewById(R.id.button);通过这种方法来得到fragment上面的控件

5.activity_fragment.xml里面的代码是这个样子的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment1"
        android:name="lgx.fram.framents.Fragment1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment2"
        android:name="lgx.fram.framents.Fragment2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

注意:控件fragment里的android:name=" "里面填写的是你的Fragment类的绝对路径(脑子突然短路,是这样说的吗??),id用来标示fragment。

6.FragmentActivity是最简单的,就只是setContentView,并没有进行其他改变。看下面

package lgx.fram.framents;

import android.app.Activity;
import android.os.Bundle;

public class FragmentActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment);
	}

}

在这里我的整个小应用就做完了。我这里的Fragment通过布局文件加入到Activity里的,还有另一种方式是通过编程的方式将Fragment加入Activity里。这里我简单叙述

上面的1,2,3,4都不需要动。

第5步骤,activity_fragment.xml里面的代码变成下面的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

</LinearLayout>

你会发现我知识去掉了两个Fragment,整个LinearLayout加进去了id

第6个步骤,里面的注释,已经写得很清楚了:

package lgx.fram.framents;

import android.os.Bundle;
import android.app.Activity;
import android.view.Display;
import android.view.Menu;

/**
 *
 * @author lenovo 动态添加Fragment主要分为4步:
 *         1.获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。
 *
 *         2.开启一个事务,通过调用beginTransaction方法开启。
 *
 *         3.向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。
 *
 *         4.提交事务,调用commit方法提交。
 */
public class FragmentActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment);
		Display display = getWindowManager().getDefaultDisplay();
		if (display.getWidth() > display.getHeight()) {
			Fragment1 fragment1 = new Fragment1();
			getFragmentManager().beginTransaction()
					.replace(R.id.main_layout, fragment1).commit();
		} else {
			Fragment2 fragment2 = new Fragment2();
			getFragmentManager().beginTransaction()
					.replace(R.id.main_layout, fragment2).commit();
		}
	}

}

这个代码的意思是,横竖屏显示不同的Fragment。如果是模拟机测试,请按Ctrl+F11。

我这里用的是replace方法,还有一个方法是add。。。这里不在赘述,请移步这里去看Android Fragment 基本介绍

完毕。。。

时间: 2024-08-10 01:21:11

简单的fragment之间通信交流的实现的相关文章

Activity Fragment 之间通信

用DialogFragment创建dialog  输入账号密码   Activity想获取到 dialog里面的值 重写onCreateDialog创建Dialog , 创建接口tabnameListener ,并在点击登陆的时候 把activity强转为自定义的接口 1 public class dialog extends DialogFragment { 2 3 private EditText tabname; 4 5 public interface tabnameListener{

Activity和Fragment之间通信

MainActivity4代码(Activity-Fragment通信) 1 package fragmentdemo.example.administrator.fragmentdemo; 2 3 import android.app.Activity; 4 import android.app.FragmentManager; 5 import android.app.FragmentTransaction; 6 import android.os.Bundle; 7 import andr

fragment的通信之bundle

fragment之间通信除了通过activity之外还可以通过bundle来实现 Bundle bundle = new Bundle(); bundle.putString("key","value"); MenuFragment menuFragment = new MenuFragment(); menuFragment.setArguments(bundle); 取数据时用 Bundle bundle = menuFragment.getArguments(

两个简单Fragment之间的通信

现在我要做个Fragment与Fragment之间的通信小demo. 建立两个Fragment,然后各添加1个按钮和1个TextView. 单击Fragment1的按钮修改Fragment2里的TextView文本. 相同的,单击Fragment2里面的按钮修改Fragment1的TextView文本. 前期准备:在Activity里面放进两个fragment: 1和2,再为其各绑定View. public class Fragment1 extends Fragment { public Vi

Activity与Fragment之间的通信

有时候我们需要在fragment与Activity之间通信,下面简单的总结下. 首先贴一个结果吧.在输入框中输入信息-->按钮-->弹出消息toast 分两部分: Activity——>Fragment 在Activity中创建Bundle数据包,并且调用Fragment的setArguments(Bundle bundle). FragmentActivity.java fragment.java  Fragment——>Activity 需要在Fragment中定义一个内部回调

Android - Fragment (三)不同Fragment之间的通信

在Fragment的java文件中,可以使用getActivity()来获得调用它的activity, 然后再找到另一个Fragment,进行通信 getActivity().getFragmentManager().findFragmentById(R.id.fragment_list); 但这样做耦合度太高,不方便后续的修改操作 Fragment与其附着的Activity之间的通信,都应该由Activity来完成 不能是多个Fragment之间直接通信 Fragment与其附着的Activi

搭建Activity与Fragment,Fragment与Fragment之间的通信架构

内心独白:  曾几何时但凡听到架构之两个字,总能联想到老子说的一句话:"玄之又玄,众妙之门".说不清,道不明.就像是看不见,摸不着,但又真实存在的东西给我们的那种感觉. 回顾人类的历史,繁重的劳动让我们意识到工具的必要性和重要性,并学会了去发明和使用工具.当我进行了大量的,甚至是繁重的编程之后,也开始重新意识到架构的必要性和重要性.当然软件工程发展了这么多年,构架与模式之类的东西前辈们早就说过并且践行与呼吁过,并且也留下了很多值得我们学习和研究的构架模式.但于我个人而言,在没有经历过痛

[转][译][Android基础]Android Fragment之间的通信

2014-2-14 本篇文章翻译自Android官方的培训教程,我也是初学者,觉得官方的Training才是最好的学习材料,所以边学边翻译,有翻译不好的地方,请大家指正. 如果我们在开发过程中为了重用Fragment这个UI组件,那么我们应该把Fragment设计成是“自包含”.“模块化”组件,这种组件定义自己的布局和行为.一旦我们成功定义了这样的可重用的Fragment,我们就可以将他们与Activity进行关联,然后与整个Application进行整体的UI组装. 我们经常需要一个Fragm

Fragment的生命周期&amp;同一Activity下不同Fragment之间的通信

Android开发:碎片Fragment完全解析(2) Fragment的生命周期 和Activity一样,Fragment也有自己的生命周期,理解Fragment的生命周期非常重要,我们通过代码的方式来瞧一瞧Fragment的生命周期是什么样的: 1 publicclass Fragment1 extends Fragment { 2 publicstaticfinal String TAG = "Fragment1"; 3 @Override 4 public View onCre