2018-04-17

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.运行效果如下:


图1-5.jpg
图1-7.jpg
图1-8.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.点击返回按钮,运行效果如图所示:


图1-9.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.运行结果如图所示:


图1-6.jpg

本节知识点:

 FragmentManager manager;     //碎片的管理器
 FragmentTransaction transaction; //碎片的事物
 transaction=manager.beginTransaction();//单击一下就开始一个事物
 changeFragment()增加动画特效的方法
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356

推荐阅读更多精彩内容

  • 本篇是合集文章,主要供各位童鞋系统学习和日后复习使用。本篇是旧篇中的“如何直观地理解过拟合与欠拟合的那些事”,“分...
    浩彬老撕阅读 791评论 0 0
  • 高级编程语言与S7(3) 2017-09-21HwLib(慧兰博)HwLib慧兰博 功能测试 代码完成后,编译程序...
    慧兰博阅读 128评论 0 0
  • 读完“颠覆意志力的改变方法,让你变成更好的自己”一文,精神为之一振,文中的三大点,无不是自己今年的写照,而关于改变...
    Zero_Lily阅读 140评论 2 1
  • 你的教室 到我的教室 第一次走的时候一共是89步 我走了很多次 也数了很多次 每次都不会差1、2步 从我的家 到你...
    先80阅读 453评论 2 2