Dynamic Fragment(一)

本文介绍如何在Android应用中使用Fragment进行布局切换,并通过自定义接口实现Fragment间的交互。包括竖屏与横屏布局的适配,以及通过MainActivity协调不同Fragment的数据交换。

Fragment从3.0开始支持,如果为3.0一下则需要引入support.v4.包,不过我在4.0上用的时如果不用这个附加包总是报错:Error inflating class fragment,google了很多方法都不行...

最后还是借的用了那个外加包,如果使用外加包的话MainActivity需要指明为FragmentActivity。

先看一下效果,竖屏时:

布局文件为:
<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="vertical" >

    <fragment
        android:id="@+id/fragementitle"
        android:name="com.example.fragement1_2_1.Fragementtitle"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </fragment>

    <fragment
        android:id="@+id/fragementdoc"
        android:name="com.example.fragement1_2_1.Fragementdoc"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </fragment>

</LinearLayout>
</pre><pre code_snippet_id="256492" snippet_file_name="blog_20140325_3_7346743" class="html" name="code">两个fragment,垂直布局
横屏:
<img alt="" src="https://img-blog.youkuaiyun.com/20140325203926359?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ2NoYW5nc2h1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />
布局文件:
<pre code_snippet_id="256492" snippet_file_name="blog_20140325_5_5459370" class="html" name="code"><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:name="com.example.fragement1_2_1.Fragementtitle"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </fragment>

    <fragment
        android:id="@+id/fragementdoc"
        android:name="com.example.fragement1_2_1.Fragementdoc"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </fragment>

</LinearLayout>

为了适应屏幕,水平布局
 
 
项目结构:
<img alt="" src="https://img-blog.youkuaiyun.com/20140325204307234?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ2NoYW5nc2h1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />
当屏幕转向时,手机会从layout-land文件夹下加载更合适的布局
类一:MainActivity:
<pre code_snippet_id="256492" snippet_file_name="blog_20140325_5_5459370" class="html" name="code">package com.example.fragement1_2_1;

import android.os.Bundle;


import android.annotation.SuppressLint;

import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements Framentinterface {
	android.support.v4.app.FragmentManager fragmentManager;
	Fragementdoc fragementdoc;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		fragmentManager = getSupportFragmentManager();
		fragementdoc = (Fragementdoc) fragmentManager
				.findFragmentById(R.id.fragementdoc);

	}
	@Override
	public void Onsetindex(int index) {
		// TODO Auto-generated method stub

		if (fragementdoc != null)
			fragementdoc.setdisplay(index);
		else
			System.out.println("fragemetdoc------------->null");

	}

}

 
 
继承FragmentActivity并且实现Framentinterface接口。
Framentinterface接口为一个自定义接口,其目的是为了实现fragment之间的互动。
其流程是Fragementtitle通过getActivity()获取MainActivity实例,调用Framentinterface里的Onsetindex()方法。将index参数传递给MainActivity,之后在MainActivity里获取并且实例化Fragementdoc,调用Fragementdoc里的setdisplay()方法进行更新textview。嗯,大致就是如此。
Fragementdoc:
<pre code_snippet_id="256492" snippet_file_name="blog_20140325_5_5459370" class="html" name="code">package com.example.fragement1_2_1;

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 Fragementdoc extends Fragment {
	String[] doc = new String[] { "It is the first doc!",
			"It is the second doc!", "It is the third doc!",
			"It is the fourth doc!", "It is the fifth doc!" };
	TextView textdoc;
	View docview;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		docview = inflater.inflate(R.layout.fragement_doc, container, false);

		return docview;
	}

	public void setdisplay(int i) {
		textdoc = (TextView) docview.findViewById(R.id.doc);
		textdoc.setText(doc[i]);
	}
}

 
 
Fragementtitle:
package com.example.fragement1_2_1;

import java.util.RandomAccess;

import android.annotation.SuppressLint;

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.RadioGroup;

public class Fragementtitle extends Fragment implements
		RadioGroup.OnCheckedChangeListener {

	View title;
	RadioGroup group;
	Framentinterface listener;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		title = inflater.inflate(R.layout.fragement_title, container, false);
		group = (RadioGroup) title.findViewById(R.id.nametitle);
		group.setOnCheckedChangeListener(this);
		listener = (Framentinterface) getActivity();
		return title;
		// TODO Auto-generated method stub
		// return super.onCreateView(inflater, container, savedInstanceState);
	}

	private int getindex(int id) {
		// TODO Auto-generated method stub
		int index = -1;
		switch (id) {
		case R.id.one:
			index = 0;
			// System.out.println("------------->0");
			break;
		case R.id.two:
			index = 1;
			break;
		case R.id.three:
			index = 2;
			break;
		case R.id.four:
			index = 3;
			break;
		case R.id.five:
			index = 4;
			break;

		}
		return index;
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		// System.out.println("------------->click");
		int id = getindex(checkedId);

		listener.Onsetindex(id);
	}

}
</pre><pre code_snippet_id="256492" snippet_file_name="blog_20140325_5_5459370" class="html" name="code">
Framentinterface:
package com.example.fragement1_2_1;

public interface Framentinterface {

	public void Onsetindex(int index);
}
 
 
 
demo:http://download.youkuaiyun.com/detail/yangchangshu/7098275

 
 
 
 
 

<pre code_snippet_id="256492" snippet_file_name="blog_20140325_5_5459370" class="html" name="code"><pre code_snippet_id="256492" snippet_file_name="blog_20140325_5_5459370" class="html" name="code"><pre>
 
 
 
 
 
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值