Android中的Fragment
Frament是什么?
1.什么是Fragment?
fragment它自己的中文意思:碎片;
一个可以将activity拆分成几个完全独立封装的可重用的组件,每个组件有自己的生命周期和ui布局。
Fragment的生命周期
onAttach()
这个是 回调函数
onCreate()
系统创建fragment的时候回调他,在他里面实例化一些变量
这些个变量主要是:当你 暂停 停止的时候 你想保持的数据
onCreateView()
第一次使用的时候 fragment会在这上面画一个layout出来, 为了可以画控件 要返回一个 布局的view,也可以返回null。
onActivityCreated()
当Activity中的onCreate方法执行完后调用。
onStart()
和activity一致,启动Fragement 启动时回调,,此时Fragement可见。
onResume()
和activity一致 在activity中运行是可见的。激活, Fragement 进入前台, 可获取焦点时激活。
onPause()
和activity一致 其他的activity获得焦点,这个仍然可见
第一次调用的时候,指的是 用户 离开这个fragment(并不是被销毁)
通常用于 用户的提交(可能用户离开后不会回来了)
onStop()
和activity一致, fragment不可见的, 可能情况:activity被stopped了OR fragment被移除但被,加入到回退栈中,一个stopped的fragment仍然是活着的如果长时间不用也会被移除。
onDestroyView()
Fragment中的布局被移除时调用。
onDestroy()
销毁fragment对象, 跟activity类似了。
onDetach()
Fragment和Activity解除关联的时候调用。 脱离activity。
2.fragment静态加载方法
fragment静态加载所用的布局,android:name属性中是MyFragment.java的全名,android:id中是Fragment的唯一标识(这个必须得加,否则报错,也可用android:tag属性来作唯一标识)。
代码块
<fragment
android:name="com.example.myfragment.MyFragment"
android:id="@+id/myfragment_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
3.fragment动态加载方法
1.创建一个类继承Fragment,复写onCreateView方法。
例如:
public class AnotherRightFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
//传进fragment布局文件创建一个view对象
View view =inflater.inflate(R.layout_another_right_fragment,container,flase);
return view;
}
}
2.在MainActivity中创建待添加的fragment实例。
AnotherRightFragment fragment = new AnotherRightFragment();
3.在Activity中通过调用个体FragmentManager()方法获取到FragmentManager。
FragmentManager fragmentManager = getFragmentManager();
4.开启一个事物,通过调用beginTransaction()方法开启。
FragmentTransaction transaction = fragmentManager.beginTransaction();
5.向容器内加入Fragment,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。
transaction.replace(R.id.right_layout,fragment);
6.提交事务,调用commit()方法来完成。
transaction.commit();
5.viewpager+fragment实现页卡滑动切换
布局文件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">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="@dimen/top_tab_height"
android:background="@color/main_top_color" >
<TextView
android:id="@+id/picture_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/picture"
android:textStyle="bold"
android:textColor="@color/main_top_tab_color"
android:textSize="@dimen/main_top_tab_text_size" />
<TextView
android:id="@+id/movie_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/movie"
android:textStyle="bold"
android:textColor="@color/main_top_tab_color"
android:textSize="@dimen/main_top_tab_text_size" />
<TextView
android:id="@+id/music_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/music"
android:textStyle="bold"
android:textColor="@color/main_top_tab_color"
android:textSize="@dimen/main_top_tab_text_size" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/main_line_height"
android:layout_gravity="bottom"
android:orientation="vertical"
android:background="@color/main_top_color"
>
<ImageView
android:id="@+id/cursor"
android:layout_width="@dimen/main_matrix_width"
android:layout_height="@dimen/main_line_height"
android:scaleType="matrix"
android:src="@color/matrix_color" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:background="@color/main_top_color"/>
<android.support.v4.view.ViewPager
android:id="@+id/vPager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="@color/white"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
</LinearLayout>
代码块
package net.horsttnann.Fragments;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("Fragment 1", "onCreateView");
// ---Inflate the layout for this fragment---
return inflater.inflate(R.layout.fragment1, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d("Fragment 1", "onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Fragment 1", "onCreate");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("Fragment 1", "onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.d("Fragment 1", "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.d("Fragment 1", "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.d("Fragment 1", "onPause");
}
@Override
public void onStop() {
super.onStop();
Log.d("Fragment 1", "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d("Fragment 1", "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("Fragment 1", "onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.d("Fragment 1", "onDetach");
}
}