您的位置:首页 > 移动开发 > Android开发

2015-06-16

2015-06-17 19:10 393 查看
Fragment:

   a: 在一个活动中动态开启一个Fragment

    1.创建这个Fragment的对象 fg;

    2.利用Activity的getFragmentManager()方法得到这个Fragment的管理权

    3.利用这个管理权的beginTransaction方法得到一个事务Transaction

    4.利用这个事务的replace方法,意思就是用当前的Fragment代替哪一个东西。

    5.commit方法提交。

   b:inflater:

    1.注册的布局,通过findViewById()可以加载到界面。这个加载时静态的。这个方法只能用在活动中,比如像常见的Fragment(可以直接inflater.inflate),Adapter就不可以调用这个方法。只能通过<LayoutInflater> 这个对象动态的得到

            要得到这个对象有三个方法:

            (1)最直接的在Activity中就像getFragmentManager()一样,Activity中有直接的方法。

                    写为:LayoutInflater inflater=getLayoutInflater();

                        源码:public PhoneWindow(Context context)

                                {   

                                 super(context);   

                                    mLayoutInflater = LayoutInflater.from(context);

                                }

            (2)LayoutInflater inflater=LayoutInflater.from(context);  

                         源碼:     public static LayoutInflater from(Context context)

                                {   

                                 LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService

                                         (Context.LAYOUT_INFLATER_SERVICE);

                                    if (LayoutInflater == null)

                                    {       

                                     throw new AssertionError("LayoutInflater not found.");   

                                    }   

                                    return LayoutInflater;

                                }

            (3)LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                                WINDOW_SERVICE  WindowManager   管理打开的窗口程序

                                LAYOUT_INFLATER_SERVICE LayoutInflater  取得xml里定义的view

                                ACTIVITY_SERVICE    ActivityManager 管理应用程序的系统状态

                                POWER_SERVICE   PowerManger 电源的服务

                                ALARM_SERVICE   AlarmManager    闹钟的服务

                                NOTIFICATION_SERVICE    NotificationManager 状态栏的服务

                                KEYGUARD_SERVICE    KeyguardManager 键盘锁的服务

                                LOCATION_SERVICE    LocationManager 位置的服务,如GPS

                                SEARCH_SERVICE  SearchManager   搜索的服务

                                VEBRATOR_SERVICE    Vebrator    手机震动的服务

                                CONNECTIVITY_SERVICE    Connectivity    网络连接的服务

                                WIFI_SERVICE    WifiManager Wi-Fi服务

                                TELEPHONY_SERVICE   TeleponyManager 电话服务

                                inflate 方法

                                通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:

                                public View inflate (int resource, ViewGroup root) 

                                public View inflate (XmlPullParser parser, ViewGroup root)

                                public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)  

                                public View inflate (int resource, ViewGroup root, boolean attachToRoot)

                                示意代码:

                                LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);       

                                View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));       

                                //EditText editText = (EditText)findViewById(R.id.content);// error 

                                EditText editText = (EditText)view.findViewById(R.id.content);

                                对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。

                

    2.它们调用的内容也不同。

            一般,inflater是加载一个布局。而findViewById()是加载一个控件的id。Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能android上还有一个与Inflate()类似功能的方法叫findViewById(),二者有时均可使用,但也有区别

        区别在于:

            如果你的Activity里用到别的layout,比如对话框layout,你还要设置这个layout上的其他组件的内容,你就必须用inflate()方法先将对话框的layout找出来,然后再用findViewById()找到它上面的其它组件。例如:

            [html] view plaincopy

            View view1=View.inflate(this,R.layout.dialog_layout,null);  

                

              TextViewdialogTV=(TextView)view1.findViewById(R.id.dialog_tv);  

                

              dialogTV.setText("abcd");  

            注:R.id.dialog_tv是在对话框layout上的组件,而这时若直接用this.findViewById(R.id.dialog_tv)肯定会报错。

            [html] view plaincopy

            View viewStub = ((ViewStub) findViewById(R.id.stubView)).inflate();  

            Inflate()或可理解为“隐性膨胀”,隐性摆放在view里,inflate()前只是获得控件,但没有大小没有在View里占据空间,inflate()后有一定大小,只是出于隐藏状态

   c:关于Adapter public View getView()效率的提升:

                            ----------------效率提升其实本质就是缓存技术--------------------

        ------getView()这个方法的本质就是得到一个View对象,然后加载它的布局。得到它的实例,然后对其操作-----------

        首先第一个缓存:系统提供的convertView就是一个缓存。

        第二个缓存:ViewHolder,顾名思义就是提供缓存的地方。 首先你要自己创建一个类(ViewHolder),通过view.setTag() 和view.getTag() ,刚得到这个View的时候通过第一个方法保存,已经得到的要通过view.getTag()来取出这个ViewHolder。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android