5. Fragment详解

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详解的更多相关文章
- Fragment详解之三——管理Fragment(1)
相关文章: 1.<Fragment详解之一--概述>2.<Fragment详解之二--基本使用方法>3.<Fragment详解之三--管理Fragment(1)>4 ...
- android——fragment详解
在android开发过程中,如果使用到了导航栏.那么不可避免的就需要使用fragment来处理界面.闲着没事,就详解一下Framgent的使用方法吧. 难得写一次.本人 shoneworn shone ...
- Android面试收集录4 Fragment详解
1.什么是Fragment? 你可以简单的理解为,Fragment是显示在Activity中的Activity. 它可以显示在Activity中,然后它也可以显示出一些内容. 因为它拥有自己的生命周期 ...
- Android 开发 之 Fragment 详解
本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...
- Android Fragment详解
一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...
- Fragment详解
1 Fragment简介 1.1 Fragment的设计初衷 Android3.0引入Fragment的初衷是为大屏幕的设备提供更加灵活的动态UI设计,由于大屏设备可以容纳更多的UI组件,且这些UI组 ...
- Android Fragment 详解(一)
Android从3.0开始引入fragment,主要是为了支持更动态更灵活的界面设计,比如在平板上的应用.平板机上拥有比手机更大的屏幕空间来组合和交互界面组件们.Fragment使你在做那样的设计时, ...
- Android Fragment详解(二):Fragment创建及其生命周期
Fragments的生命周期 每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件. 对应生命周期可参考下图: 创建片元(Creating a Fragment) To c ...
- Android Fragment详解(一):概述
Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment.你 ...
随机推荐
- iOS xcode缓存问题
Question: When I try to build my app in Xcode, I get this error message:PCH file built from a differ ...
- 【原创】Eclipse中为SVN设置快捷键
SVN是深受开发者喜爱的版本控制工具,其较CVS有更好的控制策略.在Android开发中,我也选择SVN作为版本控制工具.Eclipse的SVN插件名叫Subclipse,可以到htt ...
- MyBatis学习总结_08_Mybatis3.x与Spring4.x整合
一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:create -DgroupId=me.gacl -DartifactId=spring4-myba ...
- CentOS下对Apache的中文乱码处理
# vi /etc/sysconfig/i18nLANG="en_US.UTF-8"SYSFONT="latarcyrheb-sun16" 默认的语言是英文,如 ...
- ExtJs combobox支持模糊匹配
其实很简单,我们只需要在创建下拉框时,给下拉框添加一个监听事件,代码如下: //以下监听事件用于对下拉项进行模糊匹配 ,listeners:{ ...
- linux网络相关
ethtool:http://www.ibm.com/developerworks/cn/linux/1304_wangjy_ethtools/ 网卡特性:http://blog.chinaunix. ...
- [转]Debian 安装与卸载包命令(APT&&DPKG)
转自:zhangjunhd 的BLOG 1.APT主要命令apt-cache search ------package 搜索包sudo apt-get install ------package 安 ...
- USACO Section 3.3: Home on the Range
到最后发现是DP题 /* ID: yingzho1 LANG: C++ TASK: range */ #include <iostream> #include <fstream> ...
- Flex通过Blazeds利用Remoteservice与后台java消息推送
http://www.cnblogs.com/xia520pi/archive/2012/05/26/2519343.html http://computerdragon.blog.51cto.com ...
- 二维码工具类 - QrcodeUtils.java
二维码工具类,提供多种生成二维码.解析二维码的方法,包括中间logo的二维码等方法. 源码如下:(点击下载 - QrcodeUtils.java.MatrixToImageWriterEx.java. ...