您的位置:首页 > 移动开发 > Android开发

Android fragment入门二

2014-05-08 13:30 411 查看
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 {

@Override
protected 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{
@Override
public 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() {
@Override
public void onClick(View v) {
//getSupportFragmentManager 是导入的 Android v4包兼容3.0以下版本
//首先得到此Fragment的父容器activity,在父容器获取里面其他的子Fragment
Fragment2 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;
@Override
public 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 stub
tv.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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android