1.什么是Fragment?
fragment它自己的中文意思:碎片;
一个可以将activity拆分成几个完全独立封装的可重用的组件,每个组件有自己的生命周期和ui布局。
2.用fragment能解决什么问题?
说明:总的来说,Fragment和Activity的生命周期类似。需要注意的是,它相比于Activity,多了onAttach(), onDetch(), onCreateView()和onDestroyView()这几个回调函数;但是,却少了onRestart()。
Fragment的生命周期非常复杂,分为以下几种情况:
- 如果是通过XML中的<fragment/>标签实例化的,那么第一个收到的回调将是onInflate
- 如果setRetainInstance(true),那么当Activity重建时,Fragment的onDestroy以及Activity重建后Fragment的onCreate回调不会被调用.(无论是否将其添加到了返回栈)
- 如果当前显示的是Fragment A,然后执行FragmentTransaction.replace(),那么Fragment A会执行onPause()->onStop()->onDestroyView()->onDestroy()->onDetach(),如果执行FragmentTransaction.replace().addToBackStack(),那么Fragment A会执行onPause()->onStop()->onDestroyView()
- FragmentTransaction.hide(),将不会导致onPause(),而是会触发onHiddenChanged()
- FragmentTransaction.detach(),会导致onPause()->onStop()->onDestroyView(),注意:onDestroy()和onDetach()不会调用
3.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"
- />
4.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>