Android fragment入门二

来源:互联网 发布:软件测试项目 编辑:程序博客网 时间:2024/05/29 16:36

fragment不兼容3.0以下的版本,所有开发时需要使用Android扩展的兼容jar包,引入其中的android.support.v4.app.FragmentActivity;android.support.v4.app.Fragment;

下面做了一个fragment相互通讯的例子:



package com.example.fragment;import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

package com.example.fragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;public class Fragment1 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment1, null);Button bt = (Button)view.findViewById(R.id.bt);bt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//getSupportFragmentManager 是导入的 Android v4包兼容3.0以下版本//首先得到此Fragment的父容器activity,在父容器获取里面其他的子FragmentFragment2 fragment2 = (Fragment2) getActivity().getSupportFragmentManager().findFragmentById(R.id.f2);fragment2.setText("fragment2的文本被改变了");}});return view;}}

package com.example.fragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class Fragment2 extends Fragment{TextView tv;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//填充器,将fragment的xml文件,实例化成对象View view = inflater.inflate(R.layout.fragment2, null);tv = (TextView) view.findViewById(R.id.tv);return view;}public void setText(String s) {// TODO Auto-generated method stubtv.setText(s);}}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <fragment        android:id="@+id/f1"        android:name="com.example.fragment.Fragment1"        android:layout_width="0dip"        android:layout_height="fill_parent"        android:layout_weight="1" />    <fragment        android:id="@+id/f2"        android:name="com.example.fragment.Fragment2"        android:layout_width="0dip"        android:layout_height="fill_parent"        android:layout_weight="1" /></LinearLayout>
fragment1.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"    android:background="#ff0000" >    <Button        android:id="@+id/bt"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="改变fragment2" /></RelativeLayout>
fragment2.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"    android:background="#ffff00" >    <TextView        android:id="@+id/tv"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="fragment2" /></RelativeLayout>



0 0