Android实战积累——Fragment使用(一)

1、Fragment的产生与介绍

  Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,谷歌在3.0版本就退出了Fragment。简单来说就是Fragment就是Activity界面的一个部分,一个Activity可以有多个Fragment,并且每个Fragmen都有自己的生命周期,并和当前Activity绑定可以与其通信。

2、Fragment的生命周期

  直接贴谷歌官方给的图

  

  可以看出Fragment的生命周期依存与Activity。具体关系如下:

  onAttach(Activity)

  当Fragment与Activity发生关联时调用。
  onCreateView(LayoutInflater, ViewGroup,Bundle)
  创建该Fragment的视图
  onActivityCreated(Bundle)
  当Activity的onCreate方法返回时调用
  onDestoryView()
  与onCreateView想对应,当该Fragment的视图被移除时调用
  onDetach()
  与onAttach相对应,当Fragment与Activity关联被取消时调用
  注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现,

3、简单运用

  实际上我们可以把Fragment看成Activity上的一个控件,但在那之前你要先编写具体Fragment的布局。

  3.1、头部布局titlefragment_layout.xml,此布局讲铺满整个屏幕,在activity_main.xml中在指定相应大小。  

<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="这是个fragment"
        android:textSize="20dp"
        android:textStyle="bold"
        />
</LinearLayout>

  3.2、titlefragment事件处理代码

package hlf.com.fragmentdemo;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/4/8.
 */
public class TitleFragment extends Fragment {

    private ImageButton imageButton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.titlefragment_layout,container,false);
        imageButton = (ImageButton) view.findViewById(R.id.id_title_left_btn);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"点击了头像",Toast.LENGTH_LONG).show();

            }
        });
        return view;
    }
}

  此类必须继承Fragment,并重写public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {},获取对应布局id,再像Activity中一样控件操作。此方法必须放回一个View。

  3.3 中心布局文件contentfragment_layout.xml,很简单  

<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="这是个fragment"
        android:textSize="20dp"
        android:textStyle="bold"
        />
</LinearLayout>

   3.4 contentfragment布局处理代码

package hlf.com.fragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2016/4/8.
 */
public class ContentFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.contentfragment_layout,container,false);
    }
}

  3.5 activity_main.xml 在主布局文件中加入上面声明的两个Fragment布局文件,必须声明id否则在事件代码中无法找到。  

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="hlf.com.fragmentdemo.MainActivity">

   <fragment
       android:id="@+id/id_title_left_fragment"
       android:layout_width="match_parent"
       android:layout_height="45dp" //指定高度
        android:name="hlf.com.fragmentdemo.TitleFragment"
       />
    <fragment
        android:id="@+id/id_content_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="hlf.com.fragmentdemo.ContentFragment"
        android:layout_below="@+id/id_title_left_btn"
        />
</RelativeLayout>

  3.6 MainActivity中很简单无需加入代码,生成我们制定好的布局文件就行。

package hlf.com.fragmentdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

  Fragment基本的使用结束。但这只是开始,后面讲写到Fragment与Avtivity交互,及什么情况下使用,怎样更好的利用Fragment。

posted on 2016-04-08 11:22  Huang路飞  阅读(112)  评论(0)    收藏  举报

导航