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>