Fragment(碎片)的静态建立

Fragment(碎片)的静态建立android

 

什么是碎片?
    简单来讲上图的左半边 就是一个碎片 右半边也是一个碎片 -- 基础阶段先这样理解
 
什么是静态建立碎片?
   就是在一个主Activity.xml文件布局里布局好fragment(碎片) -- 能够当作大容器的一个小布局
 
  注意:碎片类都要继承Fragment
  
   接下来实现上图界面布局 -- 碎片的静态建立
  
   一、在res/layout布局文件里有3个布局
       1 -- 一个activity_main.xml -- 用来放左边和右边的碎片
    2 -- 一个left_fragment.xml -- ListView -- 把数据绑到该ListView上
    而后在把该ListView 放到左边的碎片上
    3 -- 一个right_fragment.xml -- TextView -- 把TextView放到右边碎片上
activity_main.xml布局文件
代码
<LinearLayout 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"
    android:orientation="horizontal" >
<!-- name里 为 包名.类名 -->
<!--左边的碎片-->
    <fragment
        android:id="@+id/left_fragment"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:name="com.example.fragment_static.Left_fragment"/>
  <!--右边的碎片--> 
     <fragment
        android:id="@+id/right_fragment"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="wrap_content"
        android:name="com.example.fragment_static.Right_fragment" />
</LinearLayout>ide

------------------布局

left_fragment.xml布局文件
代码xml

<LinearLayout 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"
    android:orientation="vertical">继承

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />生命周期

</LinearLayout>
------------------
right_fragment布局文件get

代码it

<LinearLayout 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"
    android:orientation="vertical" >io

    <TextView
        android:id="@+id/right_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右边fragment" />class

</LinearLayout>

==================================

二、Left_fragment(左边碎片) 类名

代码

public class Left_fragment extends Fragment {

 private ArrayAdapter<String> adapter;
 private ListView listview;
 
 @Override//fragment被添加到宿主Activity时被回调 -- 紧回调一次
 public void onAttach(Activity activity) {
  super.onAttach(activity);
 }
 @Override//建立fragment时回调 -- 紧回调一次
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //初始化 须要的数据
  String[] names = new String[20];
  for(int i = 0;i<20;i++){
   names[i] = "lala" + i;
  }
  
  adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, names);
 }
 //必须重写该方法
 @Override//每次建立 、绘制fragment时 都会调用该 方法 -- 调用屡次
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
   
  View view = inflater.inflate(R.layout.left_fragment,container, false);
  listview = (ListView) view.findViewById(R.id.listview);
  listview.setAdapter(adapter);
  //返回一个ListView 布局视图到有该类 -- 包名.类名  的Fragment(碎片) 布局上
  return view;
 }
}
------------------

三、Right_fragment(碎片) 类

代码

public class Right_fragment extends Fragment {
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.right_fragment, container, false);
  //返回一个TextView到 右边的碎片上
  return view;
 }

-----------------

四、MainActivity 类

代码

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }
}

==============================================
  
  

 

Fragment生命周期:
(一)、Fragment基本状态:
一、活动状态:Resumed 当前Fragment位于前台,用户可见,能够得到焦点;
二、暂停状态: Paused 另外一个Activity处于前台并拥有焦点, 可是该Fragment所在的Activity仍然可见(前台Activity局部透明或者没有覆 盖整个屏幕),不过不能得到焦点;

三、中止状态:Stopped

要么是宿主Activity已经被中止, 要么是Fragment从Activity被移除但被添加到回退栈中; 中止状态的Fragment仍然活着(全部状态和成员信息被系统保持着)。 然而, 它对用户再也不可见, 而且若是Activity被销毁,它也会被销毁;

四、销毁状态:Destroyed 只能等待被回收。

(二)、Fragment生命周期:【重点】


一、onAttach(): 当该Fragment被添加到Activity时被回调。该方法只会被调用一次;

二、onCreate(): 当建立Fragment时被回调。该方法只会被调用一次;

三、onCreateView():每次建立、绘制该Fragment的View组件时回调该方法,Fragment将会显示该方法返回的View 组件;

四、onActivityCreated(): 当Fragment的宿主Activity被启动完成后回调该方法;

五、onStart(): 启动Fragment时被回调;

六、onResume(): onStart()方法后必定会回调onResume()方法;

七、onPause(): 暂停Fragment时被回调;

八、onStop(): 中止Fragment时被回调;

九、onDestroyView(): 销毁该Fragment所包含的View组件时调用;

十、onDestroy(): 销毁Fragment时被回调。该方法只会被调用一次;

十一、onDetach(): 将Fragment从Activity中删除、替换完成时调用该方法。onDestroy()方法后必定会回调onDetach()方法。 该方法只会被调用一次。

十二、onInflate():

1三、onViewCreated():

【备注:】其中大多数程序必须实现Fragment的三个回调方法分别为:
onCreate :系统建立Fragments 时调用,可作执行初始化工做或者当程序被暂停或中止时用来恢复状态,跟Activity 中的onCreate至关。 onCreateView :用于首次绘制用户界面的回调方法,必须返回要建立的Fragment 视图UI。假如你不但愿提供Fragment用户界面则能够 返回NULL。

onPause : 当用户离开这个Fragment的时候调用,这时你要提交任何应该持久的变化,由于用户可能不会回来。

相关文章
相关标签/搜索