• android Fragment.


    引用:http://www.cnblogs.com/topcoderliu/archive/2011/05/09/2041669.html

    待续//参考/reference/android/app/Fragment.html  

    /guide/topics/fundamentals/fragments.html

    Fragment用来描述一些行为或一部分用户界面在一个Activity中,我们可以通过合并多个fragment在一个单独的activity中建立多个UI面板,也可以同时重用fragment在多个activity中。可以认为fragment作为activity的一节模块,fragment有自己的生命周期,接收自己的输入事件,可以从运行中的activity中添加/移除。

    创建一个fragment必须创建Fragment的子类或者存在的子类

    当系统在首次绘制用户界面调用fragment时,必须通过onCreateView在你的fragment画一个UI然后返回一个View(null表示没有fragment)

    当fragment创建时被调用,要用到onCreate()初始化一些实用的空间,比如再fragment暂停或停止时需要恢复的

    public class MyFragment extends Fragment{
          @Override
           public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                             Bundle savedInstanceState){
                      return inflater.inflate(R.layout.myfragment, container, false);
          }
    }

    Lifecycle

    The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:
     
    onAttach(Activity) called once the fragment is associated with its activity.
    onCreate(Bundle) called to do initial creation of the fragment.
    onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
    onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreaate.
    onStart() makes the fragment visible to the user (based on its containing activity being started).
    onResume() makes the fragment interacting with the user (based on its containing activity being resumed).
    As a fragment is no longer being used, it goes through a reverse series of callbacks:
     
    onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
    onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
    onDestroyView() allows the fragment to clean up resources associated with its View.
    onDestroy() called to do final cleanup of the fragment's state.
    onDetach() called immediately prior to the fragment no longer being associated with its activity.

    Layout

    在xml文件里,可以布局多个fragment,也可以布局layout之后通过替换得到fragment

    比如

    <LinearLayout  android:orientaion="horizontal">  
          <fragment <span style="color: #ff0000;">android:name</span>="com.example.mytitlefragment"/>
          <fragment  android:name="com.example.mydetailfragment"/>
    </LinearLayout>
    <LinearLayout android:orientaion="horizontal">
            <fragment <span style="color: #ff0000;">class</span><span style="color: #000000;">="com.example.FragmentLayout</span><span style="color: #ff0000;">$TitlesFragment"</span>/>
             <<span style="color: #ff0000;">FrameLayout </span>  android:id="@+id/details"/>
    </LinearLayout>

    android:name= "myclass"

    class="myclass.myStaticSubclass"

    另外注意第二个xml文件中,details通过FrameLayout布局,之后在运行时替换(programmatically add the fragment to an existing ViewGroup)

    管理fragment,必须要用到FragmentManager.

    FragmentManager提供了几个与fragments交互的APIs,比如finding fragments in the activity还有popping fragment off the back stack to restore their previous state.

    Transaction

    为了运行一次transaction,比如add or remove a fragment,必须创建一个FragmentTransaction。之后可以调用add() remove()或者replace()。

    每次applied这些transaction侯,必须call commit()之后系统才会应用这些fragment transaction到activity中去。

    和Activity互相通讯

    Fragment is implemented as an object that's independent from an Activity and can be used inside multiple activities.

    Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout:

    Although a Fragment is implemented as an object that's independent from an Activity and can be used inside multiple activities, a given instance of a fragment is directly tied to the activity that contains it.
     
    Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout:
     
    View listView = getActivity().findViewById(R.id.list);
    Likewise, your activity can call methods in the fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById() or findFragmentByTag(). For example:
     
    ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
  • 相关阅读:
    注解
    idea 快速生成返回值快捷方式
    异常处理
    finally 关键字
    博客园美化
    winform中webBrowser模拟网页操作中遇到的问题
    使用NPOI 2.1.1读取EXCEL2003/2007返回DataTable
    使用事务和SqlBulkCopy批量插入数据
    Java String比较
    Java 实例级别的锁和类级别的锁
  • 原文地址:https://www.cnblogs.com/sode/p/2877023.html
Copyright © 2020-2023  润新知