Fragment 案例设置界面
1.创建工程
在activity_main.xml布局中添加LinearLayout,下面放上三个按钮,上面会被相应的fragment代替掉。具体代码如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="消息" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="联系人" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="动态" />
</LinearLayout>
</LinearLayout>
若想使三个按钮水平等大小排放,只需把宽度设为0,weight设为1。
2.创建三个fragment布局文件
代码如下所示: fragment1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是第一个碎片"
android:textSize="22sp"
android:textColor="#ff0000"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="350dp"
android:src="@drawable/a01" />
</LinearLayout>
代码如下所示: fragment2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个片段"
android:textColor="#00ff00"
android:textSize="22sp"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="350dp"
android:src="@drawable/a04" />
</LinearLayout>
代码如下所示: fragment3
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="350dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="@drawable/a09" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:text="第三个片段"
android:textColor="#0000ff"
android:textSize="22dp"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="返回" />
</RelativeLayout>
3.创建三个Fragment
由于需要在MainActivity中添加fragment,因此需要创建相应的fragment类,分别创建三个类F1,F2,F3继承fragment,并在该类中编写相应的逻辑代码,F1代码如下:
package com.example.androidstudy1002;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class F1 extends Fragment {
static TextView tv; //静态方法只能访问静态变量
public static Handler handler=new Handler(){
public void handleMessage(android.os.Message msg){
tv.setText(msg.obj.toString());
};
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1, null);
tv=(TextView) view.findViewById(R.id.textView1);
return view;
}
}
F2代码如下所示:
package com.example.androidstudy1002;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class F2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment2, null);
return view;
}
}
F3代码如下所示:
package com.example.androidstudy1002;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class F3 extends Fragment {
Button bt;
FragmentManager fm;
FragmentTransaction ft;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment3, null);
return view;
}
}
4.编写MainActivity中的代码
编写好了Fragment的代码之后需要在MainActivity中添加Fragment,具体代码如下:
package com.example.androidstudy1002;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements
OnClickListener{
Button b1,b2,b3;
FragmentManager manager; //碎片的管理器
FragmentTransaction transaction; //碎片的事物
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
b2=(Button) findViewById(R.id.button2);
b3=(Button) findViewById(R.id.button3);
manager=getFragmentManager();
b1.setOnClickListener(this);// this 自己做监视器
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
transaction=manager.beginTransaction();//单击一下就开始一个事物
switch (arg0.getId()) {
case R.id.button1:
transaction.replace(R.id.ll, new F1());//把ll布局换成第一个片段
break;
case R.id.button2:
transaction.replace(R.id.ll, new F2());
break;
case R.id.button3:
transaction.replace(R.id.ll, new F3());
break;
default:
break;
}
transaction.commit(); //提交
}
}
5.运行效果如下:
![](https://img.haomeiwen.com/i6543399/eb477346ce120b6c.jpg)
![](https://img.haomeiwen.com/i6543399/63f149fd5200b148.jpg)
![](https://img.haomeiwen.com/i6543399/a788785e18b588cb.jpg)
6.接着实现点击第三个片段中的返回,跳转到fragment1中,并替换掉里面的内容,更改为 “您浏览完成”
(1)在F3中增加对按钮的监听,代码如下:
bt=(Button) view.findViewById(R.id.button4);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
fm=getFragmentManager();
ft=fm.beginTransaction();
ft.replace(R.id.ll, new F1());
ft.commit();
//利用massage向F1传递信息
Message msg=Message.obtain();
//产生一个message对象,obtain得到、获得 这样一个消息
msg.obj="您浏览完成!" ;
F1.handler.sendMessage(msg);
}
});
(2)在F1中接收消息,代码如下:
public class F1 extends Fragment {
static TextView tv; //静态方法只能访问静态变量
public static Handler handler=new Handler(){
public void handleMessage(android.os.Message msg){
tv.setText(msg.obj.toString());
};
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1, null);
tv=(TextView) view.findViewById(R.id.textView1);
return view;
}
}
7.点击返回按钮,运行效果如图所示:
![](https://img.haomeiwen.com/i6543399/55572ed9e6b7c6d2.jpg)
8.接下来实现为页面跳转添加动画效果的功能。
在MainActivity中添加如下代码:
public class MainActivity extends Activity implements
OnClickListener{
Button b1,b2,b3;
F1 f1;
F2 f2;
F3 f3;
FragmentManager manager; //碎片的管理器
FragmentTransaction transaction; //碎片的事物
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
b2=(Button) findViewById(R.id.button2);
b3=(Button) findViewById(R.id.button3);
manager=getFragmentManager();
f1=new F1();
f2=new F2();
f3=new F3();
changeFragment(f1,R.animator.slide_left_in,R.animator.slide_right_out);
b1.setOnClickListener(this);// this 自己做监视器
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
private void changeFragment(Fragment fragment,int enterAnim,int exitAnim) {
if(fragment != null){
FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();
beginTransaction.setCustomAnimations(enterAnim, exitAnim);
beginTransaction.replace(R.id.ll, fragment);
beginTransaction.commit();
}
}
@Override
public void onClick(View arg0) {
transaction=manager.beginTransaction();//单击一下就开始一个事物
switch (arg0.getId()) {
case R.id.button1:
changeFragment(f1,R.animator.slide_left_in,R.animator.slide_right_out);
break;
case R.id.button2:
changeFragment(f2,R.animator.slide_left_in,R.animator.slide_right_out);
break;
case R.id.button3:
changeFragment(f3,R.animator.slide_left_in,R.animator.slide_right_out);
break;
default:
break;
}
transaction.commit(); //提交
}
}
9.在res下创建一个animator文件夹,在文件夹下写实现动画特效的两个xml文件。
slide_left_in.xml(从左边进去)代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator
android:valueFrom="200dp" android:valueTo="0"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="3000" />
<objectAnimator
android:valueFrom="0.0" android:valueTo="1.0"
android:valueType="floatType"
android:propertyName="alpha"
android:duration="3000" />
</set>
slide_right_out.xml(从右边出来)代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:valueFrom="0dp" android:valueTo="-350dp"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="3000" />
<objectAnimator
android:valueFrom="1.0" android:valueTo="0.0"
android:valueType="floatType"
android:propertyName="alpha"
android:duration="3000" />
</set>
10.运行结果如图所示:
![](https://img.haomeiwen.com/i6543399/9db8fa71aac5becc.jpg)
本节知识点:
FragmentManager manager; //碎片的管理器
FragmentTransaction transaction; //碎片的事物
transaction=manager.beginTransaction();//单击一下就开始一个事物
changeFragment()增加动画特效的方法
网友评论