现在的位置: 首页 > 综合 > 正文

Fragment之间的通信

2018年06月09日 ⁄ 综合 ⁄ 共 3750字 ⁄ 字号 评论关闭

Fragment之间的通信

实现如下功能:

 

当点击左边的修改fragment2的内容的按钮时,修改右边的fargment2的内容,如下:

 

具体实现步骤如下:

1)首先在生成的Activity_main.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">

 

<fragment 

    android:id="@+id/fragment1"

    android:layout_width="0dip"

    android:layout_height="match_parent"

    android:layout_weight="1"

    android:name="zjh.android.fragment.Fragment1"/>

<fragment 

    android:id="@+id/fragment2"

    android:layout_width="0dip"

    android:layout_weight="1"

    android:layout_height="match_parent"

    android:name="zjh.android.fragment.Fragment2"/> 

 

</LinearLayout>

 

 

2)分别建立两个fragment的布局文件,具体代码实现代码如下:

fragment1.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"

    android:background="#ff00ff">

<Button

    android:id="@+id/button"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="修改fragment2的内容"/>

</LinearLayout>

 

fragment2.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"

    android:background="#00ffff">

<TextView 

    android:id="@+id/info"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="内容是..."/>

</LinearLayout>

 

3)分别建立两个用来加载布局文件的Fragment类,在Fragment1.java中可以通过getActivity()方法来获得getFragmentManager(),再通过其的findViewById()获得Activity_main.xml中的fragment2id,然后在fragment1中当点击修改按钮时就可以对fragment2中的内容进行修改了,Fragment1.java的具体实现代码分别如下:

package zjh.android.fragment;

 

import android.annotation.SuppressLint;

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;

 

@SuppressLint("NewApi")

public class Fragment1 extends Fragment {

 

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment1,null);

Button button = (Button) view.findViewById(R.id.button);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Fragment2 fragment2 = (Fragment2) getActivity()

.getFragmentManager().findFragmentById(R.id.fragment2);

fragment2.setText("内容改变了...");

}

});

return view ;

}

 

}

 

 

4)在Fragment2.java中需要获得fragment2.xml布局中的TtexViewid,同时在Fragment2.java中建立一个用于设置内容的setText()方法,使得其可以在Fragment1.java中被调用来给Fragment2.java设置内容,具体代码如下:

package zjh.android.fragment;

 

import android.annotation.SuppressLint;

import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

 

@SuppressLint("NewApi")

public class Fragment2 extends Fragment {

private TextView info ;

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment2,null);

info = (TextView)view.findViewById(R.id.info);

return view;

}

public void setText(String text){

info.setText(text);

}

}

 

5)在生成的MainActivity.java的代码如下:

package zjh.android.fragment;

 

import android.os.Bundle;

import android.app.Activity;

 

public class MainActivity extends Activity {

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.activity_main);

}

 

}

【上篇】
【下篇】

抱歉!评论已关闭.