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

相关文章
|
16天前
|
API
Fragment简介
Fragment简介
26 0
|
8月前
Flutter源码分析笔记:Widget类源码分析
本文记录阅读与分析Flutter源码 - Widget类源码分析。
60 0
Flutter源码分析笔记:Widget类源码分析
|
10月前
|
Java Android开发 容器
Android实战开发--小慕笔记UI设计(Fragment布局的使用)
Android实战开发--小慕笔记UI设计(Fragment布局的使用)
Android实战开发--小慕笔记UI设计(Fragment布局的使用)
|
前端开发
前端学习案例3-fragment之3
前端学习案例3-fragment之3
35 0
前端学习案例3-fragment之3
|
前端开发
前端学习案例2-fragment之2
前端学习案例2-fragment之2
38 0
前端学习案例2-fragment之2
|
前端开发
前端学习案例3-_fragment
前端学习案例3-_fragment
46 0
前端学习案例3-_fragment
|
前端开发
前端学习案例2-_fragment
前端学习案例2-_fragment
33 0
前端学习案例2-_fragment
|
前端开发
前端学习案例1-_fragment
前端学习案例1-_fragment
40 0
前端学习案例1-_fragment
|
前端开发
前端学习案例1-fragment之1
前端学习案例1-fragment之1
44 0
前端学习案例1-fragment之1
|
XML 缓存 算法
一个全新的RecyclerView Adapter框架源码开源
一个全新的RecyclerView Adapter框架源码开源
218 0
一个全新的RecyclerView Adapter框架源码开源