您的位置:首页 > 移动开发 > Android开发

android Fragment 简单的使用

2016-06-20 17:50 686 查看
本文是根据”鸿洋_”一篇博客来写的

这里只是自己写了一份 基本都是copy的

贴一份fragment的生命周期


代码运行效图 :


最上面是一个Fragment ,是在xml 中添加的,中间那部分就是要切换的Fragment

Activity XML 文件

第一个fragment 是直接在xml 布局里面就加载Fragment 也就是 android:name=”com.example.animate.fragment.fragment”这句话,在xml 加载Fragment 一般都是不要变动的fragment 比如标题等等

第二个Fragment 是在代码中加载的 一般都是经常会变动的

<?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"
tools:context=".MainActivity">

<fragment
android:id="@+id/icon"
android:name="com.example.animate.fragment.fragment"
android:layout_width="fill_parent"
android:layout_height="45dp" />

<FrameLayout
android:id="@+id/id_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/id_ly_bottombar"
android:layout_below="@id/icon" />

<include
android:id="@+id/id_ly_bottombar"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
layout="@layout/fragmentcenter" />

</RelativeLayout>


Activity java 代码

package com.example.animate.fragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements View.OnClickListener {
private LinearLayout mTabWeixin;
private LinearLayout mTabFriend;

private fragment2 fragment2;
private fragment3 fragment3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mTabWeixin = (LinearLayout) findViewById(R.id.radio_community);
mTabFriend = (LinearLayout) findViewById(R.id.radio_service);
mTabWeixin.setOnClickListener(this);
mTabFriend.setOnClickListener(this);
setDefaultFragment();

}

private void setDefaultFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
fragment2 = new fragment2();
transaction.replace(R.id.id_content, fragment2);
transaction.commit();
}

@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
// 开启Fragment事务
FragmentTransaction transaction = fm.beginTransaction();

switch (v.getId()) {
case R.id.radio_community:
if (fragment2 == null) {
fragment2 = new fragment2();
}
// 使用当前Fragment的布局替代id_content的控件
transaction.replace(R.id.id_content, fragment2);
break;
case R.id.radio_service:
if (fragment3 == null) {
fragment3 = new fragment3();
}
transaction.replace(R.id.id_content, fragment3);
break;
}
// transaction.addToBackStack();
// 事务提交
transaction.commit();

}
}


fragment java 代码

package com.example.animate.fragment;

import android.app.Fragment;
import android.os.Bundle;
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/6/20 0020.
*/
public class fragment extends Fragment {
private ImageButton mLeftMenu;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.staitcfragment1, container, false);
mLeftMenu = (ImageButton) view.findViewById(R.id.id_title_left_btn);
mLeftMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),
"么么哒 ",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
}


fragment xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#2D9FF0" >

<ImageButton
android:id="@+id/id_title_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/home_4" />

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="fragment1"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />

</RelativeLayout>


fragment2 和fragment3 基本一样布局也就是一个textview

package com.example.animate.fragment;

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/6/20 0020.
*/
public class fragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return  inflater.inflate(R.layout.staitcfragment2,container,false);
}
}


如果Fragment在加载布局文件的是时候用 inflater.inflate(R.layout.staitcfragment2,container);时可能会报java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.错误 ,所以还是选择 inflater.inflate(R.layout.staitcfragment2,container,false);

Fragment家族常用的API

Fragment常用的三个类:

android.app.Fragment 主要用于定义Fragment

android.app.FragmentManager 主要用于在Activity中操作Fragment

android.app.FragmentTransaction
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: