当前位置: 移动技术网 > IT编程>移动开发>Android > Android连载8-动态的添加碎片

Android连载8-动态的添加碎片

2020年04月30日  | 移动技术网IT编程  | 我要评论

曹榴,容止光辉非世人,仇竹妮

一、动态添加碎片

 

<?xml version="1.0" encoding="utf-8"?>

<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 franment"

        />

​

</linearlayout>

 

​我们先来修改一个布局的页面,然后再将这个页面添加到主页面中去,如下xml

​

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    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>

​

 

我们制作好布局之后,然后开始编辑主活动

 

package com.example.fragmenttest;

​

import android.app.activity;

import android.app.fragmentmanager;

import android.app.fragmenttransaction;

import android.os.bundle;

import android.view.menu;

import android.view.menuitem;

import android.view.view;

import android.view.view.onclicklistener;

import android.widget.button;

​

public class mainactivity extends activity implements onclicklistener{

​

  @override

  protected 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;

    }

  }

}

 

​总结:(1)​创建待添加的碎片实例;​(2)获取到fragmentmanager,在活动中可以直接调用getfragmentmanager()方法得到;(3)开启一个事务,通过调用begintransaction()方法进行开启;(4)向容器中加入碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例​;(5)提交事务,调用​commit()方法完成。

二、源码:

1.项目地址

https://github.com/ruigege66/android/tree/master/uibestpractice

2.csdn:https://blog.csdn.net/weixin_44630050

3.博客园:https://www.cnblogs.com/ruigege0000/

4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网