zoukankan      html  css  js  c++  java
  • Fragments的初识android开发

    Fragments是Android 3.0 (API level 11)才引入的.但是它却又是向下兼容的.可以支持老的Android版本.

    只不过需要导入jar包支持(在这个目录下:android-sdk-windows\extras\android\support\v4\android-support-v4.jar),

    主要用于实现以下这种UI布局

    想要实现这样一个activity里面有多个复杂的View布局, 按照以前的惯用写法可以使用viewgroup 或者自定义一些布局来实现

    而Fragments就恰恰满足了我们这一需求,他相比我们原来实现的方法,功能更强大,

    简单说来,我个人觉得可以把Fragments看成是一个view,他比view强大的地方是他是有生命周期的 并且这个生命周期会随着他附着的那个activity的改变而改变.

    onCreate()The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

    onCreateView()The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

    onPause()The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

    这是他的三个主要的方法,或者说生命周期.当他的父activity onPause();的时候,他也会调用onPause();同样他的父activityonResume();他调用onResume();

    这样就很灵活,比如有大量bitmap的view,就可以在 onPause();释放掉无用的资源,再在onResume()的时候加载.

    这些Fragments是由FragmentManager这样一个类似栈的容器管理的,可以通过 findFragmentById() 或者findFragmentByTag() 找到添加到页面里面的Fragments.

    下面看个小小的DEMO:

    首先我们要写一个继承Fragment的类,你可以把他看成你的UI界面里的某一个模块

    public class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    System.out.println(" onCreateView ");
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.example_fragment, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    System.out.println(" onCreate ");
    super.onCreate(savedInstanceState);
    }

    @Override
    public void onPause() {
    System.out.println(" onPause ");
    super.onPause();
    }

    @Override
    public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    System.out.println(" onResume ");
    }

    }

    根据打印输出,你会发现只有第一次启动才会调用onCreateView;而这里面就是你这个UI模块的布局

    接着是你要展示的activity:

    public class MyFragmentActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // ExampleFragment exampleFragment = new ExampleFragment();
    // FragmentManager fragmentManager = getSupportFragmentManager();
    // FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    // fragmentTransaction.add(R.id.viewer, exampleFragment);
    // fragmentTransaction.commit();
    }

    }

    然后activity的布局也非常简单

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
    ="match_parent"
    android:layout_height
    ="match_parent"
    android:orientation
    ="horizontal" >

    <fragment
    android:id="@+id/list"
    android:name
    ="com.fragment.demo.ExampleFragment"
    android:layout_width
    ="0dp"
    android:layout_height
    ="match_parent"
    android:layout_weight
    ="1" />

    <fragment
    android:id="@+id/viewer"
    android:name
    ="com.fragment.demo.ExampleFragment"
    android:layout_width
    ="0dp"
    android:layout_height
    ="match_parent"
    android:layout_weight
    ="2" />

    </LinearLayout>

    这样就已经搞定,生成了2个fragment, 这算是静态的添加吧

    如果是动态的添加那就需要用到上面注释的代码:

            ExampleFragment exampleFragment = new ExampleFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.viewer, exampleFragment);
    fragmentTransaction.commit();

    差不多初识就是这样,希望有人能补充,下个项目有机会一定要尝试用用

    android

     

    posted @ 2011-12-22 15:58 Mr_Su 阅读(505) | 评论 (0) 编辑 |

     

    posted @ 2011-12-22 14:59 Mr_Su 阅读(55) | 评论 (0) 编辑 |

     

    posted @ 2011-12-22 14:59 Mr_Su 阅读(96) | 评论 (0) 编辑 |

  • 相关阅读:
    C#网页数据采集(三)HttpWebRequest
    C#获取局域网ip
    C#调用Mail发送QQ邮件
    C#操作Excel(NPOI)
    html文字两行后,就用省略号代替剩下的
    js的dom测试及实例代码
    js循环数组(总结)
    黑马vue---61、为什么vue组件的data要是一个函数
    黑马vue---59-60、组件中的data和methods
    黑马vue---31-32、vue过滤器实例
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2298885.html
Copyright © 2011-2022 走看看