Fragment之间的通信
来源:互联网 发布:阿里云华北1 编辑:程序博客网 时间:2024/05/05 23:54
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中的fragment2的id,然后在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布局中的TtexView的id,同时在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);
}
}
- Fragment之间的通信
- Fragment之间的通信
- Fragment之间的通信
- Fragment之间的通信
- Fragment 之间的通信
- Fragment之间的通信
- Fragment之间的通信
- Fragment之间的通信
- Fragment之间的通信
- Fragment之间的通信
- Fragment之间的通信
- Fragment之间的通信
- android-----Fragment之间的通信
- Fragment之间的通信1
- Android Fragment之间的通信
- Android Fragment之间的通信
- Fragment与Fragment之间的通信
- Fragment 与Fragment之间的通信
- poj 3617 Best Cow Line(贪心)
- vSphere高级功能(四)—— vSphere容错FT功能实践(一览)
- 解决Fragment向下兼容问题
- 用《三体》思想看真正的O2O
- 数组,vector的sort快速降序方法
- Fragment之间的通信
- ffmpeg命令简例
- ADF11g-044:Favicon in ADF
- android按钮被点击文字颜色变化效果
- 几个关于activeMQ的配置记录
- Codeforces 425A Sereja and Swaps(暴力枚举)
- 第一篇CSDN博客,顶顶
- 深入浅出分析C#接口的作用
- 枚举