zoukankan      html  css  js  c++  java
  • Android连载8-动态的添加碎片

    一、动态添加碎片

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

     

  • 相关阅读:
    Azure HDInsight 现已在中国正式发布
    避免由于Windows Update自动安装安全补丁导致VM意外重启
    如何修复在Microsoft Azure中“虚拟机防火墙打开,关闭RDP的连接端口”问题
    关于Azure Auto Scale的高级属性配置
    在Azure中使用Load Runner测试TCP最大并发连接数
    Windows Azure案例分析: 选择虚拟机或云服务?
    Windows Server基础架构云参考架构:硬件之上的设计
    浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案
    在Windows Azure公有云环境部署企业应用
    如何在后台运行_Linux_命令并且将进程脱离终端
  • 原文地址:https://www.cnblogs.com/ruigege0000/p/12806128.html
Copyright © 2011-2022 走看看