Fragment是什么

阅读: 评论:0

Fragment是什么

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,然后执行place(),那么Fragment A会执行onPause()->onStop()->onDestroyView()->onDestroy()->onDetach(),如果执行place().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=&#fragment.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和待添加的碎片实例。

place(R.id.right_layout,fragment);

6.提交事务,调用commit()方法来完成。

transactionmit();


5.viewpager+fragment实现页卡滑动切换

布局文件l如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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>

本文发布于:2024-01-29 11:23:51,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170649863414928.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Fragment
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23