zoukankan      html  css  js  c++  java
  • onCreateView的一个细节--Fragment

     

    public View onCreateView(LayoutInflater inflater, ViewGroup contaiiner, Bundle savedInstanceState)

    在写一个Fragment的时候,继承Fragment基类,然后,要重写的其中一个回调方法是onCreateView。如果该Fragment有界面,那么,返回的View是非空的;如果该Fragment

    是没有界面的,返回的是Null。

    这是在写Fragment中经常做的事情。不过,这里有个小细节,那就是什么时候container是为空的,为空表示什么?

    这就是本篇文章要解决的问题。

    写一个Demon之后,观察,发现了如下事实:

    1.首先是要Fragment在activity的UI中出现了,也就是说,一开始container是不可能为Null的。

    2.当因为其他情况,导致了Fragment所依附的父组件不存在了,那么此时container就是Null了。----比如,从横屏转换到竖屏,就会导致之前的界面发生改变。

    所以,答案为:

    当Fragment所依附的container,从有到无,就会导致container为空。空表示,当前Fragment所依附的ViewGroup不存在了(从有到没,一开始是有的)。

    实现测试的Demon:

    1.两个界面,一个是系统横屏时使用的,一个是系统竖屏时使用的。横屏时,会生成Fragment。

    先横屏,在界面中产生了Fragment;然后,再竖屏,此时系统使用另外一个布局文件,之前Fragment所依附的ViewGroup消失了,这时系统调用onCreateView,container为空。

    写一个监听器,将Fragment所接受到的信息传递给宿主Activity。

    判定当前,系统使用哪个布局文件(相应的表示了当前是横屏还是竖屏),是横屏,则实例化Fragment,并添加到相应的ViewGroup中。

    package com.containertest;
    
    import com.containertest.fragment.SimpleFragment;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements ContainerDetectListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            if (findViewById(R.id.fragment) != null) {
                // 将Fragment添加到R.id.fragment所指向的布局中,R.id.fragment所指向的布局是container
                SimpleFragment f = SimpleFragment.newInstanec();
                getFragmentManager().beginTransaction().add(R.id.fragment, f)
                        .commit();
    
            } else {
                // 此时是竖着拿手机,不用做任何操作
    
            }
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public void containerIsNull(boolean state) {
            // TODO Auto-generated method stub
            if (state) {
                Toast.makeText(this, "Now the onCreateView's container is null",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this,
                        "Now the onCreateView/s container is not null",
                        Toast.LENGTH_SHORT).show();
            }
    
        }
    
    }

    SimpleFragment:

    package com.containertest.fragment;
    
    import com.containertest.ContainerDetectListener;
    
    import com.containertest.R;
    import android.app.Activity;
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class SimpleFragment extends Fragment {
    
        private ContainerDetectListener listener;
    
        private static SimpleFragment f;
    
        public static SimpleFragment newInstanec() {
            if (f == null) {
                f = new SimpleFragment();
            }
            
            return f;
    
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
    
            try {
                listener = (ContainerDetectListener) activity;
    
            } catch (ClassCastException e) {
                e.printStackTrace();
                throw new ClassCastException(activity.toString()
                        + "must implement ContainerDetectListener");
            }
        }
    
        @Override
        public void onCreate(Bundle state) {
            super.onCreate(state);
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle state) {
    
            if (container == null) {
                listener.containerIsNull(true);
                return null;
            } else {
                // 计划通过代码来制定container
                listener.containerIsNull(false);
                View view = inflater.inflate(R.layout.fragment_ui, null);
                return view;
            }
    
        }
    
        @Override
        public void onPause() {
            super.onPause();
        }
    
    }

    效果图:

    开始,本身就是没有生成过Fragment:是竖屏。

    1

    然后,将手机横屏:---将会生成fragment

    ---因为会重新创建Activity,从而再次检测当前界面是横屏还是竖屏。

    2

    然后,再将手机竖屏了----这时,Fragment依然是那个Fragment,不过,因为,它所依附的ViewGroup不存在了,所以,无需给它绘制界面。

    ----这是container为Null的情况才会出现。

    3

    附上:官方关于该方法的解释

    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

    Added in API level 11

    Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null (which is the default implementation). This will be called between onCreate(Bundle) and onActivityCreated(Bundle).

    If you return a View from here, you will later be called in onDestroyView() when the view is being released.

    Parameters

    inflater
    The LayoutInflater object that can be used to inflate any views in the fragment,

    container
    If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.

    savedInstanceState
    If non-null, this fragment is being re-constructed from a previous saved state as given here.

    Returns
    • Return the View for the fragment's UI, or null.
    版权声明:
    作者:ttylinux
             
    本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Oracle查询数据表结构/字段/类型/大小
    Oracle 如何修改列的数据类型
    数组声明和使用要点
    关于转发和重定向的路径问题!
    Java高级架构师(一)第29节:完成下订单和修改库存的功能
    Java高级架构师(一)第28节:Index、商品详细页和购物车
    Java高级架构师(一)第27节:实现index功能的开发
    《深入理解Spark-核心思想与源码分析》(三)第三章SparkContext的初始化
    《深入理解Spark-核心思想与源码分析》(二)第二章Spark设计理念和基本架构
    《深入理解Spark-核心思想与源码分析》(一)总体规划和第一章环境准备
  • 原文地址:https://www.cnblogs.com/ttylinux/p/3775491.html
Copyright © 2011-2022 走看看