【Android】51、动态添加碎片

来源:互联网 发布:redis存储数据大小 编辑:程序博客网 时间:2024/05/01 12:33

本篇博文最后修改时间:2016年9月6日,11:09。


一、简介

本篇介绍动态添加碎片。


二、实验平台
系统版本:Windows7 家庭普通版 32位操作系统。

三、版权声明
博主:思跡
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.csdn.net/omoiato

联系方式:315878825@qq.com

Java零基础入门交流群:541462902


四、动态添加碎片

在上一节当中我们已经学会了在布局文件中添加碎片的方法,

不过碎片真正的强大之处在于,它可以在程序运行时动态地添加到活动当中。

根据具体情况来动态地添加碎片,就可以将程序界面定制得更加多样化。
我们还是在上一节代码的基础上继续完善:


1、新建another_right_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ffff00"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:textSize="20sp"        android:text="This is another right fragment"        /></LinearLayout>


这个布局文件的代码和right_fragment.xml 中的代码基本相同,

只是将背景色改成了黄色,并改动了显示的文字。


2、新建AnotherRightFragment 作为另一个右侧碎片

public class AnotherRightFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)     {        View view = inflater.inflate(R.layout.another_right_fragment,container, false);        return view;    }}


代码同样非常简单,在onCreateView()方法中加载了刚刚创建的another_right_fragment布局。

这样我们就准备好了另一个碎片。


3、将碎片动态地添加到活动当中

修改activity_main.xml,代码如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <fragment        android:id="@+id/left_fragment"        android:name="com.example.fragmenttest.LeftFragment"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1" />    <FrameLayout        android:id="@+id/right_layout"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1" >      <fragment        android:id="@+id/right_fragment"        android:name="com.example.fragmenttest.RightFragment"        android:layout_width="match_parent"        android:layout_height="match_parent" />    </FrameLayout></LinearLayout>


可以看到,现在将右侧碎片放在了一个FrameLayout 中

在上一章中我们学过,这是Android 中最简单的一种布局,它没有任何的定位方式,所有的控件都会摆放在布局的左上角。

由于这里仅需要在布局里放入一个碎片,因此非常适合使用FrameLayout。


4、在代码中替换FrameLayout 里的内容从而实现动态添加碎片的功能。

修改MainActivity 中的代码,如下所示:

public class MainActivity extends Activity implements OnClickListener {    @Overrideprotected void onCreate(Bundle savedInstanceState)     {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button button = (Button) findViewById(R.id.button);        button.setOnClickListener(this);    }    @Override    public void onClick(View v)     {        switch (v.getId())         {            case R.id.button:            AnotherRightFragment fragment = new AnotherRightFragment();            FragmentManager fragmentManager = getFragmentManager();            FragmentTransaction transaction = fragmentManager.            beginTransaction();            transaction.replace(R.id.right_layout, fragment);            transaction.commit();            break;            default:            break;        }    }}
首先我们给左侧碎片中的按钮注册了一个点击事件,

然后将动态添加碎片的逻辑都放在了点击事件里进行。

结合代码可以看出,动态添加碎片主要分为5 步。

1. 创建待添加的碎片实例。
2. 获取到FragmentManager,在活动中可以直接调用getFragmentManager()方法得到。
3. 开启一个事务,通过调用beginTransaction()方法开启。
4. 向容器内加入碎片,一般使用replace()方法实现,需要传入容器的id 和待添加的碎片实例。
5. 提交事务,调用commit()方法来完成。
这样就完成了在活动中动态添加碎片的功能,重新运行程序,

可以看到和之前相同的界面,然后点击一下按钮,效果如图所示:


0 0
原创粉丝点击