对于Fragment的学习:

近日初步学习了Fragment这以特殊的组件,其依托与一个Activity,与Activity的生命周期息息相关,为其设置的视图只有当其关联到一个Activity才会起效果。觉得其用处在于可以更改当前视图而不阻塞主线程,同时可以用于响应式布局,可使其在平板和手机这不同尺寸的设备上获得比较好的兼容效果。

学习写的是简易版的Fragment应用,实现在一个主页面(activty_main.layout)中手动添加一个自定义的fragment(其应用视图是Crime_fragment.layout)。需要继承的是 android.support.v4.app.Fragment

Fragment类 需要实现的主要方法:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

通过下列语句实现创建一个View实例并返回:View v = (View) inflater.inflate(R.layout.crime_fragment,container,false);

参数分别是:布局文件,父容器,是否关联

 package com.example.fragmentpractise;

 import java.util.UUID;

 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;

 public class CrimeFragment extends Fragment{

    private Crime mCrime;

    public static CrimeFragment newInstance(int id){
        CrimeFragment c = new CrimeFragment();
        return c;
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        mCrime = new Crime();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        View v = (View) inflater.inflate(R.layout.crime_fragment,container,false);
        Button date = (Button) v.findViewById(R.id.date);
        date.setText(mCrime.getDate().toString());
        return v;

    }

 }

Crime_fragment

 <?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:orientation="vertical" >
     <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:textSize="20dp"
         android:text="@string/title_label"/>
     <EditText
         android:id="@+id/title"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:textSize="16dp"
         />
     <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:textSize="20dp"
         android:text="@string/detail_label"/>
     <Button
         android:id="@+id/date"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         />
     <CheckBox
         android:id="@+id/isSolved"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/isSolved"/>

 </LinearLayout>

Fragment布局

在托管的活动类中实现通过FragmentManager调用 findFragmentById(id) 查找是否存在,最后通过事务提交Fragment

检测是否存在该Fragment并加入

附加:对 ”View v = (View) inflater.inflate(R.layout.crime_fragment,container,false);“ 的探索

这里需要补充一点,就是上述参数中的关联的布尔值,在一般使用的inflate方法中可以值应用到上述的布局文件和父容器,不声明关联布尔值,这意味着视图会默认加入到当前指定的父容器中。这里可以做个实验:将A视图加到B布局中,如果传入关联布尔值为false,你会发现原先B布局中的组件都不显示了。这时候如果你想实现视图关联可以通过B.addView(A);来实现关联;但在这里有试过尝试选择默认参数或True布尔值,你会出现运行时报错:

10-19 20:11:40.846: E/AndroidRuntime(1956): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentpractise/com.example.fragmentpractise.CrimeActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

报错信息中可以看到当我们选择默认值或者True布尔值时会提醒我们指定的子视图已经加入某个父容器中了,提醒我们需要先将其从父容器中移除;

这里由于源码不可见,只能进行猜测,那便是系统在调用了onCreateView,得到视图并加入到之前设定的父容器,这一.addView() 方法已经由系统帮我们在背后实现了。(通过打印打印 onCreateView 中的container 的Id时发现是与提交事务时指定的container Id相同;)

附加:对于findFragmentById的探索

public abstract Fragment findFragmentById (int id)

 

Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction. This first searches through fragments that are currently added to the manager's activity; if no such fragment is found, then all fragments currently on the back stack associated with this ID are searched.

Returns

The fragment if found or null otherwise.

这个方法实现了从FragmentManager实例中去寻找一个符合条件的Fragment,这里的条件受id制约,但API提到id究竟是什么呢?尝试使用了传入提交事务时的id,或者是生成布局时的给设置的id,最后在生成视图去检测都依旧不行;

为了验证这一方法,以下代码分别验证了通过Fragmentd XML的id来搜索:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        View v = (View) inflater.inflate(R.layout.crime_fragment,container,false);
        Button date = (Button) v.findViewById(R.id.date);
        date.setText(mCrime.getDate().toString());
        v.setId(R.id.test);
        return v;

    }

为布局加入Id

 fragment = fm.findFragmentById(R.id.test);
         if(fragment != null){
             Log.i("information", "true");
         } else{
             Log.i("information","false");
         }

查询是否存在

结果是:10-13 23:10:16.267: I/information(2997): false

直到后来求助了高手和查阅资料后,终于发现了问题所在:这个提交的动作并不是同步的,由事务提交请求,再由系统自己去处理,为了验证这个问题,使用了以下代码来验证:

 FragmentManager fm = getSupportFragmentManager() ;
         Fragment fragment = fm.findFragmentById(R.id.fragment_container);
         if(fragment == null){
             FragmentTransaction tran = fm.beginTransaction ();
             tran.add(R.id.fragment_container,new CrimeFragment());
             tran.commit();
         }

先提交给事务

 Timer timer= new Timer();
         timer.schedule(new TimerTask() {

             @Override
             public void run() {
                 // TODO Auto-generated method stub
                 FragmentManager fm = getSupportFragmentManager() ;
                 Fragment fragment = fm.findFragmentById(R.id.fragment_container);
                 if(fragment != null){
                     Log.i("information", "true");
                 } else{
                     Log.i("information","false");
                 }
             }
         }, 2000);

通过一个线程去延迟查找动作

结果是:10-14 22:18:47.436: I/information(1672): true

思路是使用线程,延迟2秒去检测。

附上一个自己Github上的简单Demo:https://github.com/lhppom/Fragment-Demo

Android之Fragment学习总结(1)的更多相关文章

  1. 33.Android之Fragment学习

    Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...

  2. Android之Fragment学习笔记①

    Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...

  3. [android]p7-1 fragment学习笔记

    本文源自<android权威编程指南第3版>第7章UI fragment与fragment 第7章主要内容是实现一个记录不良行为的APP(部分实现),有列表,有具体的行为内容显示.第7章主 ...

  4. Android之Fragment学习笔记②(Fragment生命周期)

    一. Fragment生命周期图                                  二.Fragment生命周期方法介绍 Fragment的生命周期和activity生命周期很像,其生 ...

  5. Android应用开发学习笔记之Fragment

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Fragment翻译成中文就是“碎片”.“片断”的意思,Fragment通常用来作为一个Activity用户界面的一 ...

  6. [转]Android 使用Fragment界面向下跳转并一级级返回

      1.首先贴上项目结构图: 2.先添加一个接口文件BackHandledInterface.java,定义一个setSelectedFragment方法用于设置当前加载的Fragment在栈顶,主界 ...

  7. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  8. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  9. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

随机推荐

  1. (gridcontrol等)通用导出excel z

    关于DevExpress Winform 的所有可打印控件的导出excel 的通用方法,并且解决DevExpress控件自带的方法存在的缺陷问题 1.解决GridControl自带方法不能导出图片: ...

  2. 每天一个 Linux 命令(8):cp 命令

    cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数.但是如果是在 ...

  3. XE6移动开发环境搭建之IOS篇(8):在Mac OSX 10.8中安装XE6的PAServer(有图有真相)

    网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 安装PAServer ...

  4. Delphi七个版本

    第一次Zack Urlocker给我看一款尚未发布的名为Delphi的产品时,我意识到她将改变我的工作—还有许多其他软件开发者的工作. 我过去为使用Windows C++ 库而挣扎,而Delphi过去 ...

  5. HDU5900

    http://acm.hdu.edu.cn/showproblem.php?pid=5900 就是给出两行数字,每行有若干的数,如果相邻的两个数字的最大公约数不是1 的话拟具可以把这两数删除,并且把第 ...

  6. Selenium + PhantomJS + python 简单实现爬虫的功能

    Selenium 一.简介 selenium是一个用于Web应用自动化程序测试的工具,测试直接运行在浏览器中,就像真正的用户在操作一样 selenium2支持通过驱动真实浏览器(FirfoxDrive ...

  7. 简单实用的Windows命令(二)

    昨天简单的记录了几个非常简单实用的Windows命令,不过我又想起来还有两个我在实际的工作中也是经常用到的命令——PING和IPCONFIG,不过我在工作中的使用都是非常简单的,用PING命令检测对应 ...

  8. MySQL_财务统计各产品品类各城市上周收入毛利表_20161202

    注:财务需要统计各产品品类各城市上周毛利情况 下面这样的表是沟通后展现的形式.数据展现形式我认为的大都是行列转 列转行的转置 从财务角度这样展现形式比较适合. 由于黄色部分为汇总项目,因此我拆分成9个 ...

  9. nginx 命令

    nginx 命令 sudo /etc/init.d/nginx configtest     测试是否配置有错 sudo /usr/local/nginx/sbin/nginx -s reload   ...

  10. mysqldb模块的简单用法

    # - *- coding:utf-8-*-import urllib2import reimport MySQLdbimport sysreload(sys)sys.setdefaultencodi ...