fragment学习(例子 选项卡页面切换)

来源:互联网 发布:淘宝过期订单怎么投诉 编辑:程序博客网 时间:2024/06/11 23:50

**

fragment介绍

转载来自:http://blog.csdn.net/lmj623565791/article/details/37970961

**

1、Fragment的产生与介绍

Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神马超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。

2、Fragment的生命周期

Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。官网这张图很好的说明了两者生命周期的关系:这里写图片描述

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

选项卡页面的实现
首先 创建一个activity实现整体页面的布局
其次 创建出fragment的xml文件和实现类

mainactivity.java类

package com.alleged.fragment;import android.support.v7.app.ActionBarActivity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends ActionBarActivity implements OnClickListener{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //找到所有的控件        Button index = (Button)findViewById(R.id.index);        Button discover = (Button)findViewById(R.id.discover);        Button contact = (Button)findViewById(R.id.contact);        Button me = (Button)findViewById(R.id.me);        //设置监听事件        index.setOnClickListener(this);        discover.setOnClickListener(this);        contact.setOnClickListener(this);        me.setOnClickListener(this);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    @Override    public void onClick(View v) {        //获取Fragment管理者        FragmentManager fragmentManager = getFragmentManager();        //开启fragmentmanager事务        FragmentTransaction transaction = fragmentManager.beginTransaction();        switch(v.getId()){        case R.id.index:            //替换掉以前的布局            transaction.replace(R.id.contentShow, new indexFragment());            break;        case R.id.discover:            //替换掉以前的布局            transaction.replace(R.id.contentShow, new discoverFragment());            break;        case R.id.contact:            //替换掉以前的布局            transaction.replace(R.id.contentShow, new contactFragment());            break;        case R.id.me:            //替换掉以前的布局            transaction.replace(R.id.contentShow, new meFragment());            break;        }        transaction.commit();       }}

mainactivity.java对应的布局文件

<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"    tools:context="com.alleged.fragment.MainActivity" >    <LinearLayout        android:id="@+id/contentShow"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        ></LinearLayout>    <LinearLayout         android:id="@+id/chooseTable"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_alignParentBottom="true">        <Button             android:id="@+id/index"            android:layout_width="0dp"            style="?android:attr/buttonBarButtonStyle"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="主页"/>        <Button             android:id="@+id/discover"            android:layout_width="0dp"            style="?android:attr/buttonBarButtonStyle"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="发现"/>        <Button             android:id="@+id/contact"            android:layout_width="0dp"            style="?android:attr/buttonBarButtonStyle"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="联系人"/>        <Button             android:id="@+id/me"            android:layout_width="0dp"            style="?android:attr/buttonBarButtonStyle"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="我"/>    </LinearLayout></RelativeLayout>

fragment类文件(例子 一共四个这种类更换名称就可以了)

package com.alleged.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;//Fragment类public class discoverFragment extends Fragment {    //复写oncreatview方法    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        // TODO Auto-generated method stub        //关联相应的布局  做成view类 第一个关联相应的xml布局文件        View view = inflater.inflate(R.layout.discover_fragment, null);        return view;    }}

fragment类的布局文件如下

<?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:gravity="center_vertical|center_horizontal"    android:orientation="vertical" >    <ImageView        android:id="@+id/index_icon"        android:layout_width="200dp"        android:layout_height="200dp"        android:src="@drawable/image_icon" />    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        style="@android:style/TextAppearance.Holo.Widget.TextView"        android:text="HELLO I AM discover"/></LinearLayout>
0 0
原创粉丝点击