• Fragment开发实战(一)


    一、 Fragment的特征:

    1. Fragment总是Activity界面的组成部分。Fragment可调用getActivity()方法获取它所在的Activity,Activity可调用FragmentManager的findFragmentById()或findFragmentByTag()方法来获取Fragment。

    2. 在Activity的运行过程中,可调用FragmentManager的add()、remove()、replace()方法动态的添加、删除或替换Fragment。

    3. 一个Activity可以同时组合多个Fragment; 反过来,一个Fragment也可以被多个Activity复用。

    4. Fragment可以响应自己的输入事件,并拥有自己的生命周期,但他们的生命周期直接被其所属的Activity的生命周期控制。


    二、 创建Fragment

    与创建Activity类似,开发者实现Fragment必须继承自Fragment基类,Android的Fragment的继承体系包括:

    Fragment

        |__  DialogFragment

        |__  ListFragment

        |__  PreferenceFragment

        |__  WebViewFragment


    开发Fragment通常需要复写其中的回调方法,一般情况下要实现如下三个方法;

       onCreate()、 onCreateView()、 onPause()


    其中,onCreateView用于该Fragment控制显示View组件,该方法返回View作为Fragment显示的View组件。

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.inflate(R.layout.fragment_first, container, false);
            Button bt_first = (Button) view.findViewById(R.id.bt_first);
            bt_first.setText(MainActivity.temp);
            bt_first.setOnClickListener(new OnClickListener()
            {
    
                public void onClick(View v)
                {
                    Intent intent = new Intent(getActivity(), Activity_Second.class);
                    startActivity(intent);
                }
            });
            return view;
        }
    上述方法中,我们通过LayoutInflater加载了布局文件,并返回了该View; 通过程序我们也可以发现,Fragment可以响应自己的输入事件(此处为Click事件)


    示例:

    1)接下来,我们开发一个简单的Fragment相关的实例,实现的效果如图:



    2)我们首先定义MainActivity:

    public class MainActivity extends FragmentActivity
    {
        public static String temp = "Hello World";
    
        public static String getTemp()
        {
            return temp;
        }
    
        public static void setTemp(String temp)
        {
            MainActivity.temp = temp;
        }
    
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            FirstFragment first = new FirstFragment();
            // 如果用add()的话会出现不完全覆盖效果
            ft.replace(R.id.container, first);
            ft.commit();
        }
    
        // 切换到第二个fragment
        public void onClick_btn(View v)
        {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            SecondFragment second = new SecondFragment();
            ft.replace(R.id.container, second);
            ft.commit();
        }
    }

    3)定义FirstFragment,它将在MainActivity的onCreate()方法里被加载:

    public class FirstFragment extends Fragment
    {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // 加载布局文件
            View view = inflater.inflate(R.layout.fragment_first, container, false);
            // 可以得到Fragment布局文件中的widget
            Button bt_first = (Button) view.findViewById(R.id.bt_first);
            // 设置按钮的文字信息
            bt_first.setText(MainActivity.temp);
            // 设置按钮的监听事件
            bt_first.setOnClickListener(new OnClickListener()
            {
    
                public void onClick(View v)
                {
                    // 在Fragment中,可以通过getActivity()方法得到管理它的Activity
                    Intent intent = new Intent(getActivity(), Activity_Second.class);
                    startActivity(intent);
                }
            });
            return view;
        }
    }
    

    4)定义第二个Fragment,该Fragment将在主界面“Click Me”按钮的事件激发后,替换第一个Fragment。

    public class SecondFragment extends Fragment
    {
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // 加载布局文件
            View view = inflater.inflate(R.layout.fragment_second, container, false);
            // 得到按钮
            Button bt_second = (Button) view.findViewById(R.id.bt_second);
            bt_second.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    // 得到FragmentTransaction
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    // 设置Fragment切换的动画
                    ft.setCustomAnimations(R.animator.move_in, R.animator.move_out);
                    // 定义第一个Fragment
                    FirstFragment first = new FirstFragment();
                    // 替换Fragment
                    ft.replace(R.id.container, first);
                    // 提交事务
                    ft.commit();
                }
            });
            return view;
        }
    }


    代码下载(免费): http://download.csdn.net/detail/zuiwuyuan/7957515

  • 相关阅读:
    定时任务
    heat创建stack失败
    python-ceilometerclient命令行(终结)
    python-ceilometerclient命令行(2)
    python-ceilometerclient命令行(1)
    类属性与对象实现,init方法的作用,绑定方法,绑定方法与普通函数的区别,继承,抽象与继承,派生与覆盖
    XML模块,面向对象思想与类的定义
    configparser模块 subprocess 模块,xlrd 模块(表格处理)
    re 正则匹配的非贪婪匹配
    login 模块,re 模块
  • 原文地址:https://www.cnblogs.com/hehe520/p/6330022.html
Copyright © 2020-2023  润新知