Android fragment入门二

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>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值