onCreateView是Fragment生命周期方法中最重要的一个。因为在该 方法中会创建在Fragment中显示的View。

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

     // 装载布局文件

View view = inflater.inflate(R.layout.my_fragment, null); TextView textview =

(TextView)view.findViewById(R.id.textview);

testview.setText("Fragment Test");

    return view;

}

Fragment与Activity之间可以通过Fragment.setArguments方法向 Fragment传递参数值,并且可以通过Fragment.getArguments方法获取 这些传递的参数值。

 范例1:第一个Fragment程序。

 import android.app.Activity;
import android.os.Bundle; public class FirstFragmentActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_fragment);
}
}
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <fragment
android:id="@+id/titles"
android:layout_width="match_parent"
android:layout_height="match_parent" class="cn.eoe.first.fragment.LeftFragment" />
</LinearLayout>
 package cn.eoe.first.fragment;

 import java.io.InputStream;

 import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView; public class LeftFragment extends Fragment implements OnItemClickListener { private String[] data = new String[] { "灵魂战车2", "变形金刚3:月黑之时", "敢死队2" };
private ListView listView; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, null);
listView = (ListView) view.findViewById(R.id.listview_movie_list);
listView.setOnItemClickListener(this);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_activated_1,
data);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); return view;
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
try { TextView textView = (TextView) getActivity().findViewById(
R.id.textview_detail);
InputStream is = getActivity().getResources().getAssets()
.open("m" + position);
byte[] buffer = new byte[1024];
int count = is.read(buffer);
String detail = new String(buffer, 0, count, "utf-8");
if (textView == null) {
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("detail", detail);
startActivity(intent);
} else {
textView.setText(detail);
}
is.close();
} catch (Exception e) {
// TODO: handle exception
} } }
 public class DetailActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
TextView detail = (TextView) findViewById(R.id.textview_detail);
detail.setText(getIntent().getExtras().getString("detail")); }
}
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <fragment
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="cn.eoe.first.fragment.RightFragment" /> </RelativeLayout>
 public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, null);
return view;
}
}

范例2:向Fragment传递数据,并获取传递数据的值

activity_fragment_argument.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="onClick_SendData"
android:text="向Fragment传递数据" /> <FrameLayout
android:id="@+id/fragment_container1"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>
 import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class FragmentArgumentActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_argument);
} // 向Fragment传递数据
public void onClick_SendData(View view) {
// 获取传递的数据(页面)
MyFragment fragment = new MyFragment(); Bundle bundle = new Bundle(); bundle.putString("name", "Hello Fragment1");
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.fragment_container1, fragment, "fragment"); fragmentTransaction.commit(); Toast.makeText(this, "数据已成功传递.", Toast.LENGTH_LONG).show(); } // 获取传递的数据
public void onClick_ShowArgument(View view) {
EditText editText = (EditText) findViewById(R.id.edittext); String name = getFragmentManager().findFragmentByTag("fragment")
.getArguments().getString("name");
editText.setText(name);
}
}
 import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
return view;
} @Override
public void onDestroyView() {
Log.d("name", getArguments().getString("name"));
super.onDestroyView();
} }
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick_ShowArgument"
android:text="获取传递的数据" /> </LinearLayout>

5. Fragment详解的更多相关文章

  1. Fragment详解之三——管理Fragment(1)

    相关文章: 1.<Fragment详解之一--概述>2.<Fragment详解之二--基本使用方法>3.<Fragment详解之三--管理Fragment(1)>4 ...

  2. android——fragment详解

    在android开发过程中,如果使用到了导航栏.那么不可避免的就需要使用fragment来处理界面.闲着没事,就详解一下Framgent的使用方法吧. 难得写一次.本人 shoneworn shone ...

  3. Android面试收集录4 Fragment详解

    1.什么是Fragment? 你可以简单的理解为,Fragment是显示在Activity中的Activity. 它可以显示在Activity中,然后它也可以显示出一些内容. 因为它拥有自己的生命周期 ...

  4. Android 开发 之 Fragment 详解

    本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...

  5. Android Fragment详解

    一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...

  6. Fragment详解

    1 Fragment简介 1.1 Fragment的设计初衷 Android3.0引入Fragment的初衷是为大屏幕的设备提供更加灵活的动态UI设计,由于大屏设备可以容纳更多的UI组件,且这些UI组 ...

  7. Android Fragment 详解(一)

    Android从3.0开始引入fragment,主要是为了支持更动态更灵活的界面设计,比如在平板上的应用.平板机上拥有比手机更大的屏幕空间来组合和交互界面组件们.Fragment使你在做那样的设计时, ...

  8. Android Fragment详解(二):Fragment创建及其生命周期

    Fragments的生命周期 每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件. 对应生命周期可参考下图: 创建片元(Creating a Fragment) To c ...

  9. Android Fragment详解(一):概述

    Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment.你 ...

随机推荐

  1. android-exploitme(二):安装apk熟悉测试环境

    今天我们来熟悉测试环境: 1. 下载server代码,并运行 git clone https://github.com/SecurityCompass/LabServer.git 2. 这个serve ...

  2. autodock 结果pdb的生成

    Is there a way to save a protein-ligand complex as a PDB file in AutoDock? I have completed my docki ...

  3. git环境搭建

    Linux kernel  的官方 GIT地址是: http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git 可以从这个地 ...

  4. js中substr与substring的用法与区别

    substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/pic_1.png";alert(sr ...

  5. Jalopy 之 HelloWorld —— Jalopy 在 MyEclipse 下的安装与使用

    如果你要问我Jalopy是什么.我只能告诉你“它是一个格式化代码的工具”.因为我也是一个初学者. 如果你也是初次接触,那一起来学习下吧! ·安装 1.首先,下载资源 下载地址:http://sourc ...

  6. C语言输出当前日期和时间

    #include <stdio.h> #include <time.h> char* asctime2(const struct tm *timeptr) { static c ...

  7. URAL1057. Amount of Degrees(DP)

    1057 简单的数位DP  刚开始全以2进制来算的 后来发现要找最接近x,y值的那个基于b进制的0,1组合 #include <iostream> #include<cstdio&g ...

  8. Navicat数据存放位置和备份数据库路径设置

    navicat的数据库存放位置在什么地方?带着这样的疑问,我们去解决问题,navicat是默认安装,mysql也是默认安装,数据库存在默认用户所在的目录下面. 安装MySQL时,请选择“Custom” ...

  9. BOM浏览器对象

    BOM 浏览器对象 一.浏览器本身就自己有一些对象,不用创建就可以使用 window(当前浏览器窗体) 属性: status opener closed parent top 方法: alert(); ...

  10. BZOJ 1803 Query on a tree III

    树上主席树. 我靠这是第k小吧..... #include<iostream> #include<cstdio> #include<cstring> #includ ...