fragment作用

同一程序中切换界面 比activity轻快,灵活.

fragment代码示例

ide  : android studio 1.2
sdk : 22

package com.example.f6k5i8.lfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AnotherFragment extends <span style="color:#33ccff;">Fragment</span> {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //R.layout.fragment_another是本fragment对应的资源文件,container是主布局,
        View root = inflater.inflate(R.layout.fragment_another, container, false);
        root.findViewById(R.id.btnBackFragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        return root;
    }
}

1.注意 fragment的包

注意这里的基类fragment 是 import android.support.v4.app.Fragment; 里的.不是android.app里的


2.界面配置

fragment也有界面布局xml文件: fragment_another.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/fragmentB_text"
        android:id="@+id/anotherFragment"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="后退到上一个Fragment"
        android:id="@+id/btnBackFragment"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

3.fragment与layout.xml关联

AnotherFragment与它对应的layout.xml关联的代码是 onCreateView函数中的下面语句;

View root = inflater.inflate(R.layout.fragment_another, container, false);

4.跳转到一个新的fragment示例代码

getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();

getFragmentManager().beginTransaction()
                    .addToBackStack("AnotherFragment的标记")
                    .replace(R.id.container, new AnotherFragment())
                    .commit();

有add,有replace等多种方法.



参数R.id.container是主容器的id,它在activity_main.xml中定义,且不能用R.layout.activity_main.



参数new AnotherFragment() 是被添加的fragment子视图.

注意 addToBackStack 将打开的fragment压进栈.这样方便返回

5.从新fragment返回

前提是本fragment在打开时是addToBackStack里的

 getFragmentManager().popBackStack();

最新文章

  1. 学习js回调函数
  2. 2016HUAS_ACM暑假集训4B - 递推
  3. linux-用命令形式聊天的常用命令
  4. SCP 命令(转)
  5. 网上收集的以及自己总结的iOS开发技巧
  6. TP常用函数
  7. 仿今日头条最强顶部导航指示器,支持6种模式-b
  8. (hdu)1022 Train Problem I 火车进站问题
  9. 2、第2节课html教程客户端控件/css第一课/20150917
  10. HDU 4819 Mosaic D区段树
  11. iPhone&amp;amp;iPad DFU及恢复模式刷机、降级教程
  12. 如何给TableLayout加边框
  13. 【Loadrunner】初学Loadrunner——参数化设置(Table类型关联数据库)
  14. c/c++ 模板 类型推断
  15. 借助Algorithmia网站API:用AI给黑白照片上色,复现记忆中的旧时光
  16. confirm消息对话框
  17. Importing multi-valued field into Solr from mySQL using Solr Data Import Handler
  18. iphone safari浏览器CSS兼容性的解决方案集合
  19. 使用System.Web.Optimization对CSS和JS文件合并压缩
  20. 洛谷P3527 [POI2011]MET-Meteors [整体二分]

热门文章

  1. css3文字与字体
  2. php laravel curD
  3. rails控制台进入
  4. IIS 浏览aspx页面出现 无法显示 XML 页
  5. 对linux的根目录执行强制递归移除
  6. Javascript动态加载Html元素到页面Dom文档结构时执行顺序的不同
  7. NOIP199904求Cantor表
  8. DOM4J解析xml案例
  9. 《REWORK》启示录 发出你的心声——程序员与身体
  10. struts2 笔记04 杂记