Fragment是什么

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属性来作唯一标识)。

  1. <fragment  
  2.        android:name="com.example.myfragment.MyFragment"  
  3.        android:id="@+id/myfragment_1"  
  4.        android:layout_width="wrap_content"  
  5.        android:layout_height="wrap_content"  
  6.         />  



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>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值