如何查阅这个东西:listpreferenceefragment。我不要解释,我只要怎么能查出来,它是android3.0后加上的。

android,xml开发,下面的header干嘛用的?android:fragment又是什么?我在文档查不到?“org…ment&干什_百度知道
android,xml开发,下面的header干嘛用的?android:fragment又是什么?我在文档查不到?“org…ment&干什
title=&&gt:summary=& /ic_settings_applications&设置应用的相关选项&quot.PreferenceActivityTest$Prefs1Fragment&quot:fragment=
程序选项设置&
@drawable/header android么用:icon=&
android。在线等 &lt.app.crazyit?我查不到$这符号的用法
我有更好的答案
按默认排序
只要是用在PreferenceActivity里面onBuildHeaders方法,分别是左边的header对应右边的内容。你可以看看平板的系统选项header是用在PreferenceActivity里面的
其他类似问题
android的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁当前访客身份:游客 [
当前位置:
&&& 相信大家对Perference都比较熟悉了,也就是我们常说的偏好设置,首选项设置,可以保存一些数据,例如我们在上一次使用的时候的一些内容,希望在下一次启动后依然生效,而不需要再进行配置那么麻烦。一般这个时候我们便会使用perference键值对的方式来处理,在android3.0之前,我们一般去继承Preference这个基类去给用户呈现一个可以设置的界面,其中的layout需要自己编写,今天我们研究的是3.0之后使用碎片技术的首选项配置方法,即使用PreferenceFragement来实现。
&--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public abstract class PreferenceFragment extends Fragment
以一个列表来展示首选项对象的层级关系,这些首选项将自动地保存为SharedPreferences,使用户可以用他们来进行交互。为了能够重新获得ShaedPreferences的实例,该Fragement中的层级首选项将会在同一个包下面使用带有一个上下文的PreferenceManager.getDefaultSharedPreferences作为这个fragement 。
此外,所展示的首选项将会遵循系统首选项的视觉风格,通过使用XML文件来创建各个首选项的视图层级(可以被显示在许多页面)会非常简单。基于上述原因,推荐在应用中使用这个fragement(作为一个超类)来处理首选项问题。
一个PreferenceScreen对象应该在首选项层级的顶部。此外,随后在层次结构PreferenceScreen表示一个屏幕分割处——就是包含随后的PreferenceScreen应显示在另一个屏幕页面上。首选项框架处理从首选项层次结构显示了这些其他屏幕内容。
首选项层次结构可以有很多种方式形成:
●从一个XML文件制定的层次结构。
●从不同的activity,每一个activity通过meta-data在一个XML文件中制定他自己的首选项。
●从一个以PreferenceScreen为根的层次结构对象。
为了从一个XML文件中获取界面,使用addPreferenceFromResource(int)方法。根元素应该使用PreferenceScreen。随后的元素可以指向实际的首选项的子类。正如上面提到的,在层次结构中随后的PreferenceScreen将导致屏幕分割处。
为了指定一个意图来查询都带有各自首选项的activitiy,使用addPreferenceFromIntent方法。每个activity可以在manifest文件中指定meta-data来指向一个XML文件资源。这些资源文件将被填充到单独的首选项层次结构并且通过这个fragment来展示。
为了指定一个以PreferenceScreen为根元素的对象,使用setPreferenceScreen(PreferenceScreen)方法。
方便起见,这个fragment实现了一个用于当前层次结构中任意首选项的点击事件监听器,onPreferenceTreeClick(PreferenceScreen,Preference).
以上翻译自PreferenceFragment的官方文档,可自行查阅其原版说明
----------------------------------------------------------------------------------------------------------------------------------------
根据官方文档的说明以及自带的APIdemo中的例子,我自己使用PreferenceFragment写了一个例子,效果与之前使用继承PreferenceActivity差不多,步骤如下:
①创建一个工程
②在MainAcitivity中添加菜单按钮及对应效果
package com.example.
import android.os.B
import android.view.M
import android.view.MenuI
import android.app.A
import android.content.I
public class MainActivity extends Activity {
private static final int menu_setting = 1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,menu_setting,1,&设置&).setIcon(android.R.drawable.ic_menu_preferences);
return super.onCreateOptionsMenu(menu);
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
Intent intent = new Intent(this, FragmentPreferences.class);
startActivity(intent);
③在res文件夹下面新建一个xml文件夹,在里面新建一个preferences.xml文件
&PreferenceScreen xmlns:android=&/apk/res/android& &
&PreferenceCategory android:title=&In-line preferences& &
&CheckBoxPreference
android:key=&checkbox_preference&
android:summary=&这是一个复选框&
android:title=&复选框设置& /&
&/PreferenceCategory&
&PreferenceCategory android:title=&Dialog-based preferences& &
&EditTextPreference
android:dialogTitle=&请输入你最喜欢的种族&
android:key=&edittext_preference&
android:summary=&一个使用了编辑文本对话框的例子&
android:title=&请输入你最喜欢的种族& /&
&ListPreference
android:dialogTitle=&请选择一项&
android:entries=&@array/entries_list_preference&
android:entryValues=&@array/entryvalues_list_preference&
android:key=&list_preferenc&
android:summary=&一个使用了列表对话框的例子&
android:title=&请选择一项& /&
&/PreferenceCategory&
&PreferenceCategory android:title=&Launch preferences& &
&!-- This PreferenceScreen tag serves as a screen break (similar to page break in word processing). Like for other preference types, we assign a key here so it is able to save and restore its instance state. --&
&PreferenceScreen
android:key=&screen_preference&
android:summary=&展示另一个首选项配置页面&
android:title=&页面首选项& &
&!-- 你可以在这里放置更多的首选项内容,将被在下一个页面呈现出来 --&
&CheckBoxPreference
android:key=&next_screen_checkbox_preference&
android:summary=&在另一个页面展示但出于同一个层级的首选项配置&
android:title=&复选框设置& /&
&/PreferenceScreen&
&PreferenceScreen
android:summary=&从一个意图中启动一个activity&
android:title=&意图首选项& &
android:action=&android.intent.action.VIEW&
android:data=&& /&
&/PreferenceScreen&
&/PreferenceCategory&
&PreferenceCategory android:title=&Preference attributes& &
&CheckBoxPreference
android:key=&parent_checkbox_preference&
android:summary=&这是一个可见的父类&
android:title=&父类复选框首选项& /&
&!-- 子类的可见类型是由样式属性定义的 --&
&CheckBoxPreference
android:dependency=&parent_checkbox_preference&
android:key=&child_checkbox_preference&
android:layout=&?android:attr/preferenceLayoutChild&
android:summary=&这是一个可见的子类&
android:title=&子类复选框首选项& /&
&/PreferenceCategory&
&/PreferenceScreen&
④FragmentPreferences.java的代码部分
package com.example.
import android.app.A
import android.os.B
import android.preference.PreferenceF
public class FragmentPreferences extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragement()).commit();
public static class PrefsFragement extends PreferenceFragment{
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
最后是运行之后的效果截图:
&&&&&&&&&&&&&&&&&&&&&&&&
大致对PreferenceFragment的学习使用就是这样啦,以后在高版本上面开发的时候可以考虑这种方式。
原文链接:
共有0个评论
有什么技术问题吗?
长平狐的其他问题
类似的话题PreferenceFragment 使用 小结
时间: 17:01:36
&&&& 阅读:6
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&& Perference也就是我们常说的偏好设置,首选项设置,能够自己主动保存一些数据,比如我们在上一次使用的时候的一些内容,则在下一次启动后依旧生效,而不须要再进行配置。当用户改变设置时,系统就会更新SharedPreference文件里相应的值。perference使用键值对的方式来处理,在android3.0之前,我们一般去继承Preference这个基类,去给用户呈现一个能够设置的界面,当中的layout须要自己编写,而在3.0之后,使用的是碎片技术的首选项配置方法,即
使用PreferenceFragement来实现。
+++++++++++++++++++++++++++++++++++=
为了从一个XML文件里获取界面,使用addPreferenceFromResource(int)方法。根元素应该使用PreferenceScreen,在层次结构中随后的PreferenceScreen将导致屏幕切割处。
为了指定一个意图来查询都带有各自首选项的activitiy,使用addPreferenceFromIntent方法。每一个activity能够在manifest文件里指定meta-data来指向一个XML文件资源。这些资源文件将被填充到单独的首选项层次结构而且通过这个fragment来展示。
为了指定一个以PreferenceScreen为根元素的对象,使用setPreferenceScreen(PreferenceScreen)方法。
+++++++++++++++++++++++
方便起见,这个fragment实现了一个用于当前层次结构中随意首选项的点击事件监听器,
onPreferenceTreeClick(PreferenceScreen,Preference).
----------------------------------------------------------------------------------------------------------------------------------------
&&& 使用PreferenceFragment 的一个样例,效果与之前使用继承PreferenceActivity差点儿相同,过程例如以下:
1.创建一个project,在MainAcitivity中 加入菜单button 及 相应效果
------------------------------& cut here& -----------------------------------
&&& package com.example. &
&&& import android.os.B &
&&& import android.view.M &
&&& import android.view.MenuI &
&&& import android.app.A &
&&& import android.content.I &
&&& public class MainActivity extends Activity { &
&&&&&&&& &
&&&&&&& private static final int menu_setting = 1; &
&&&&&&& @Override &
&&&&&&& protected void onCreate(Bundle savedInstanceState) { &
&&&&&&&&&&& super.onCreate(savedInstanceState); &
&&&&&&&&&&& setContentView(R.layout.main); &
&&&&&&& } &
&&&&&&&& &
&&&&&&& @Override &
&&&&&&& public boolean onCreateOptionsMenu(Menu menu) { &
&&&&&&&&&&& // TODO Auto-generated method stub &
&&&&&&&&&&& menu.add(0,menu_setting,1,&设置&).setIcon(android.R.drawable.ic_menu_preferences); &
&&&&&&&&&&& return super.onCreateOptionsMenu(menu); &
&&&&&&& } &
&&&&&&&& &
&&&&&&& @Override &
&&&&&&& public boolean onOptionsItemSelected(MenuItem item) { &
&&&&&&&&&&& // TODO Auto-generated method stub &
&&&&&&&&&&& super.onOptionsItemSelected(item); &
&&&&&&&&&&& Intent intent = new Intent(this, FragmentPreferences.class); &
&&&&&&&&&&& startActivity(intent); &
&&&&&&&&&&& &
&&&&&&& } &
&------------------------------& cut here& -----------------------------------
2.在res目录以下新建一个xml目录,在里面新建一个preferences.xml文件
------------------------------& cut here& -----------------------------------
&&& &PreferenceScreen xmlns:android=&/apk/res/android& & &
&&&&&&& &PreferenceCategory android:title=&In-line preferences& & &
&&&&&&&&&&& &CheckBoxPreference &
&&&&&&&&&&&&&&& android:key=&checkbox_preference& &
&&&&&&&&&&&&&&& android:summary=&这是一个复选框& &
&&&&&&&&&&&&&&& android:title=&复选框设置& /& &
&&&&&&& &/PreferenceCategory& &
&&&&&&& &PreferenceCategory android:title=&Dialog-based preferences& & &
&&&&&&&&&&& &EditTextPreference &
&&&&&&&&&&&&&&& android:dialogTitle=&请输入你最喜欢的种族& &
&&&&&&&&&&&&&&& android:key=&edittext_preference& &
&&&&&&&&&&&&&&& android:summary=&一个使用了编辑文本对话框的样例& &
&&&&&&&&&&&&&&& android:title=&请输入你最喜欢的种族& /& &
&&&&&&&&&&& &ListPreference &
&&&&&&&&&&&&&&& android:dialogTitle=&请选择一项& &
&&&&&&&&&&&&&&& android:entries=&@array/cities& &
&&&&&&&&&&&&&&& android:entryValues=&@array/airport_codes& &
&&&&&&&&&&&&&&& android:key=&list_preferenc& &
&&&&&&&&&&&&&&& android:summary=&一个使用了列表对话框的样例& &
&&&&&&&&&&&&&&& android:title=&请选择一项& /& &
&&&&&&& &/PreferenceCategory& &
&&&&&&& &PreferenceCategory android:title=&Launch preferences& && &
&&&&&&&&&&& &!-- This PreferenceScreen tag serves as a screen break (similar to page break in word processing). Like for other preference types, we assign a key here so it is able to save and restore its instance state. --& &
&&&&&&&&&&& &PreferenceScreen &
&&&&&&&&&&&&&&& android:key=&screen_preference& &
&&&&&&&&&&&&&&& android:summary=&展示还有一个首选项配置页面& &
&&&&&&&&&&&&&&& android:title=&页面首选项& && &
&&&&&&&&&&&&&&& &!-- 你能够在这里放置很多其它的首选项内容,将被在下一个页面呈现出来 --& &
&&&&&&&&&&&&&&& &CheckBoxPreference &
&&&&&&&&&&&&&&&&&&& android:key=&next_screen_checkbox_preference& &
&&&&&&&&&&&&&&&&&&& android:summary=&在还有一个页面展示但出于同一个层级的首选项配置& &
&&&&&&&&&&&&&&&&&&& android:title=&复选框设置& /& &
&&&&&&&&&&& &/PreferenceScreen& &
&&&&&&&&&&& &PreferenceScreen &
&&&&&&&&&&&&&&& android:summary=&从一个意图中启动一个activity& &
&&&&&&&&&&&&&&& android:title=&意图首选项& & &
&&&&&&&&&&&&&&& &intent &
&&&&&&&&&&&&&&&&&&& android:action=&android.intent.action.VIEW& &
&&&&&&&&&&&&&&&&&&& android:data=&& /& &
&&&&&&&&&&& &/PreferenceScreen& &
&&&&&&& &/PreferenceCategory& &
&&&&&&& &PreferenceCategory android:title=&Preference attributes& & &
&&&&&&&&&&& &CheckBoxPreference &
&&&&&&&&&&&&&&& android:key=&parent_checkbox_preference& &
&&&&&&&&&&&&&&& android:summary=&这是一个可见的父类& &
&&&&&&&&&&&&&&& android:title=&父类复选框首选项& /& &
&&&&&&&&&&& &!-- 子类的可见类型是由样式属性定义的 --& &
&&&&&&&&&&& &CheckBoxPreference &
&&&&&&&&&&&&&&& android:dependency=&parent_checkbox_preference& &
&&&&&&&&&&&&&&& android:key=&child_checkbox_preference& &
&&&&&&&&&&&&&&& android:layout=&?android:attr/preferenceLayoutChild& &
&&&&&&&&&&&&&&& android:summary=&这是一个可见的子类& &
&&&&&&&&&&&&&&& android:title=&子类复选框首选项& /& &
&&&&&&& &/PreferenceCategory& &
&&& &/PreferenceScreen& &
&------------------------------& cut here& -----------------------------------
3、FragmentPreferences.java的代码部分
&------------------------------& cut here& -----------------------------------
&&& package com.example. &
&&& import android.app.A &
&&& import android.os.B &
&&& import android.preference.PreferenceF &
&&& public class FragmentPreferences extends Activity { &
&&&&&&& @Override &
&&&&&&& protected void onCreate(Bundle savedInstanceState) { &
&&&&&&&&&&& // TODO Auto-generated method stub &
&&&&&&&&&&& super.onCreate(savedInstanceState); &
&&&&&&&&&&& getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragement()).commit(); &
&&&&&&& } &
&&&&&&&& &
&&&&&&&& &
&&&&&&& public static class PrefsFragement extends PreferenceFragment{ &
&&&&&&&&&&& @Override &
&&&&&&&&&&& public void onCreate(Bundle savedInstanceState) { &
&&&&&&&&&&&&&&& // TODO Auto-generated method stub &
&&&&&&&&&&&&&&& super.onCreate(savedInstanceState); &
&&&&&&&&&&&&&&& addPreferencesFromResource(R.xml.preferences); &
&&&&&&&&&&& } &
&&&&&&& } &
&------------------------------& cut here& -----------------------------------
大致对PreferenceFragment的学习使用就是这样,以后在高版本号上面开发的时候能够考虑这样的方式
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&& &&&&it168&& && && &&
版权所有 鲁ICP备号-4
打开技术之扣,分享程序人生!底部菜单栏之Fragment的详细介绍和使用方法 - Android-Home
- 博客频道 - CSDN.NET
7064人阅读
由于TabActivity在Android4.0以后已经被完全弃用,那么我就不再浪费口水继续讲解它了,取而代之的是Fragment。Fragment是Android3.0新增的概念,Fragment翻译成中文是碎片的意思,不过却和Activity十分的相似,这一篇我花大量的篇幅来详细的讲解Fragment的介绍和使用方法。
一、Fragment的基础知识介绍
1.1.1 特性
& && &&&Fragment是activity的界面中的一部分或一种行为。可以把多个Fragment组合到一个activity中来创建一个多界面
并且可以在多个activity中重用一个Fragment。可以把Fragment任务模块化的一段activity,它具有自己的生命周期,
接收它自己的事件,并可以在activity运行时被添加或删除。
& && & Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响。例
如:当activity暂停时,他拥有的所有的Fragment都暂停了,当activity销毁时,他拥有的所有Fragment都被销毁。然
而,当activity运行时(在onResume()之后,onPause()之前),可以单独地操作每个Fragment,比如添加或删除它
们。当中执行上述针对Fragment的事务时,可以将事务添加到一个栈中,这个栈被activity管理,栈中的每一条都是一
个Fragment的一次事务。有了这个栈,就可以反向执行Fragment的事务,这样就可以在Fragment级支持“返回”键
(向后导航)。
& && &&&当向activity中添加一个Fragment时,它须置于ViewGroup控件中,并且需定义Fragment自己的界面。可以在
layout.xml布局文件中声明Fragment,元素为:&fragment&;也可以在代码中创建Fragment,然后把它加入到
ViewGroup控件中。然而,Fragment不一定非要放在activity的界面中,它可以隐藏在后台为activity工作。
1.1.2 生命周期
onCreate():
& &&&当创建fragment时系统调用此方法。在其中必须初始化fragment的基础组件们。可参考activity的说明;
onCreateView():
& &&&系统在fragment要画自己的界面时调用(在真正显示之前)此方法,这个方法必须返回fragment的layout的根控
件,如果这个fragment不提供界面,那它应返回null;
onPause():
& &&&大多数程序应最少对fragment实现这三个方法,当然还有其它几个回调方法可应该按情况实现之,所有的声明周期
回调函数在“操控fragment的生命周期”一节中有详细讨论。
下图为fragment的生命周期(它所在的activity处于运行状态)
Activity和Fragment生命周期对比图如下:
两个的生命周期很类似,也息息相关。
1.1.3 派生类
DialogFragment
& & 显示一个浮动的对话框。使用这个类创建对话框是替代activity创建对话框的最佳选择。因为可以把fragmentdialog
放入到activity的返回栈中,使用户能再返回到这个对话框。
ListFragment
& & 显示一个列表控件,就像ListActivity类,它提供了很多管理列表的方法,比如onListItemClick()方法响应click事件。
PreferenceFragment
& & 显示一个由Preference对象组成的列表,与PreferenceActivity相同。它用于为程序创建“设置”activity。
& &&&写一个读新闻的程序,可以用一个fragment显示标题列表,另一个fragment显示选中标题的内容,这两个fragment
都在一个activity上,并排显示。那么这两个fragment都有自己的生命周期并响应自己感兴趣的事件。于是,不需要再
像手机上那样用一个activity显示标题列表,用另一个activity显示新闻内容;现在可以把两者放在一个activity上同时显
示出来。如下图:
& && & Fragment必须被写成可重用的模块。因为fragment有自己的layout,自己进行事件响应,拥有自己的生命周期和
行为,所以可以在多个activity中包含同一个Fragment的不同实例。这对于让世界在不同的屏幕尺寸下都能给用户完美
的体验尤其重要。比如可以在程序运行于大屏幕中时启动包含很多fragment的activity,而在运行小屏幕时启动一个包
含少量fragment的activity。
& && &&&刚才读新闻的程序,当检测到程序运行于大屏幕时,启动activityA,将标题列表和新闻内容这两个fragment都放
在activityA中;当检测到程序运行于小屏幕时,还是启动activityA,但此时A中只有标题列表fragment,当选中一个标
题时,activityA启动activityB,B中含有新闻内容fragment。
1.3 创建Fragment
& && && &要创建fragment,必须从Fragment或Fragment的派生类派生出一个类。Fragment的代码写起来有些像activity。
它具有跟activity一样的回调方法,比如onCreate(),onStart(),onPause()和onStop()。实际上,如果想把老的程序改为
使用fragment,基本上只需要把activity的回调方法的代码移到fragment中对应的方法即可。
1.3.1添加有界面的Fragment
& && & Fragment一般作为activity的用户界面的一部分,把它自己layout嵌入到activity的layout中。一个要为fragment提
供layout,必须实现onCreateView()回调方法,然后在这个方法中返回一个View对象,这个对象时fragment的layout的
& && & 注意:如果fragment是从ListFragment中派生的,就不需要实现onCreateView()方法了,因为默认的实现已经返
回了ListView控件对象。
& && & 要从onCreateView()方法中返回layout对象,可以从layout.xml布局文件中生成layout对象。为了帮助这样做,
onCreateView()提供了一个layoutInflater对象。举例:以下代码展示了一个Fragment的子类如何从layout.xml布局文件
example_fragment.xml中生成对象。
public static ExampleFragment extends Fragment {
publicView onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
returninflater.inflate(R.layout.example_fragment, container, false);
onCreateView()参数中的container是存放fragment的layout的ViewGroup对象。saveInstanceState参数是一个Bundle,跟activity的onCreate()中Bundle差不多,用于状态恢复。但是fragment的onCreate()中也有Bundle参数,所以此处的Bundle中存放的数据与onCreate()中存放的数据还是不同的。Inflate()方法中有三个参数:&&&1&
layout的资源ID;&&&2& 存放fragment的layout的ViewGroup;&&&3& 布尔数据表示是否在创建fragment的layout期间,把layout附加到container上(在这个例子中,因为系统已经把layout插入到container中了,所以值为false,如果为true会导致在最终的layout中创建多余的ViewGroup)。& && &下面讲述如何把它添加到activity中。把fragment添加到activity一般情况下,fragment把它的layout作为activity的layout的一部分合并到activity中,有两种方法将一个fragment添加到activity中:
方法一:在activity的layout.xml文件中声明fragment
&?xmlversionxmlversion=&1.0& encoding=&utf-8& ?&
&LinearLayoutxmlns:androidLinearLayoutxmlns:android=& /apk/res/android&
android:orientation=&horizontal&
android:layout_width=&match_parent&
android:layout_height=&match_parent& &
&fragmentandroid:namefragmentandroid:name=&com.android.cwj.ArticleListFragment&
android:id=&@+id/list&
android:layout_weight=&1&
android:layout_width=&0dp&
android:layout_height=&match_parent& /&
&fragmentandroid:namefragmentandroid:name=&com.android.cwj.ArticleReaderFragment&
android:id=&@+id/viewer&
android:layout_weight=&2&
android:layout_width=&0dp&
android:layout_height=&match_parent& /&
&/LinearLayout&
以上代码中,&fragment&中声明一个fragment。当系统创建上例中的layout时,它实例化每一个fragment,然后调用它们的onCreateView()方法,以获取每个fragment的layout。系统把fragment返回的view对象插入到&fragment&元素的位置,直接代替&fragment&元素。注:每个fragment都需要提供一个ID,系统在activity重新创建时用它来恢复fragment,也可以用它来操作fragment进行其它的事物,比如删除它。有三种方法给fragment提供ID:&&&1&
为Android:id属性赋一个数字;&&&2& 为Android:tag属性赋一个字符串。如果没有使用上述任何一种方法,系统将使用fragment的容器的ID。
方法二:在代码中添加fragment到一个ViewGroup& && &&&这种方法可以在运行时,把fragment添加到activity的layout中。只需指定一个要包含fragment的ViewGroup。为了完成fragment的事务(比如添加,删除,替换等),必须使用FragmentTransaction的方法。可以从activity获取FragmentTransaction,如下:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
然后可以用add()方法添加一个fragment,它有参数用于指定容纳fragment的ViewGroup。如,Add()的第一个参数是容器ViewGroup,第二个是要添加的fragment。一旦通过FragmentTransaction对fragment做出了改变,必须调用方法commit()提交这些改变。不仅在无界面的fragment中,在有界面的fragment中也可以使用tag来作为唯一的标志,这样在需要获取fragment对象时,要调用findFragmentTag()。
1.3.2 添加没有界面的Fragment& && && &上面演示了如何添加fragment来提供界面,然而,也可以使用fragment为activity提供后台的行为而不用显示fragment的界面。要添加一个没有界面的fragment,需要在activity中调用方法add(Fragment,String)(它支持用一个唯一的字符串做为fragment的“tag”,而不是viewID)。这样添加的fragment由于没有界面,所以在实现它时不需要调用实现onCreateView()方法。&
&& &&&使用tag字符串来标示一个fragment并不是只能用于没有界面的fragment上,也可以把它用于有界面的fragment上,但是,如果一个fragment没有界面,tag字符串将成为它唯一的选择。获取以tag表示的fragment,需使用方法findFragmentByTab()。
1.4 Fragment管理& && &要管理fragment,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()。可以用FragmentManager来做以下事情:& && &&1& 使用方法findFragmentById()或findFragmentByTag(),获取activity中已存在的fragment;& && &&2& 使用方法popBackStack()从activity的后退栈中弹出fragment(这可以模拟后退键引发的动作),用方法addOnBackStackChangedListenner()注册一个侦听器以监视后退栈的变化;&
&& &&3& 还可以使用FragmentManager打开一个FragmentTransaction来执行fragment的事务,比如添加或删除fragment。& && & 在activity中使用fragment的一个伟大的好处是能根据用户的输入对fragment进行添加、删除、替换以及执行其他动作的能力。提交的一组fragment的变化叫做一个事务。事务通过FragmentTransaction来执行。还可以把每个事务保存在activity的后退栈中,这样就可以让用户在fragment变化之间导航(跟在activity之间导航一样)。
可以通过FragmentManager来取得FragmentTransaction的实例,如下:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
一个事务是在同一时刻执行的一组动作(很像数据库中的事务)。可以用add(),remove(),replace()等方法构成事务,最后使用commit()方法提交事务。在调用commit()之前,可以用addToBackStack()把事务添加到一个后退栈中,这个后退栈属于所在的activity。有了它,就可以在用户按下返回键时,返回到fragment执行事务之前的状态。如下例:演示了如何用一个fragment代替另一个fragment,同时在后退栈中保存被代替的fragment的状态。
//创建一个fragment
Fragment newFragment = new ExampleFragment();
//实例化fragment事务管理器
FragmentTransaction transaction = getFragmentManager().beginTransaction();
//用新创建的fragment来代替fragment_container
transaction.replace(R.id.fragment_container,newFragment);
//添加进栈中
transaction.addToBackStack(null);
//提交事务
解释:newFragment代替了控件ID R.id.fragment_container所指向的ViewGroup中所含的任何fragment。然后调用addToBackStack(),此时被代替的fragment就被放入后退栈中,于是当用户按下返回键时,事务发生回溯,原先的fragment又回来了。如果向事务添加了多个动作,比如多次调用了add(),remove()等之后又调用了addToBackStack()方法,那么所有的在commit()之前调用的方法都被作为一个事务。当用户按返回键时,所有的动作都被反向执行(事务回溯)。&
事务中动作的执行顺序可随意,但要注意以下几点:&1& 必须最后调用commit();
&2& 如果添加了多个fragment,那么它们的现实顺序跟添加顺序一致(后显示的覆盖前面的)&3& 如果在执行的事务中有删除fragment的动作,而且没有调用addToBackStack(),那么当事务提交时,那些被删除的fragment就被销毁了。反之,那些fragment就不会被销毁,而是处于停止状态。当用户返回时,它们会被恢复。
&4& 但是,调用commit()后,事务并不会马上执行。它会在activity的UI线程(其实就是主线程)中等待直到现成能执行的时候才执行。如果必要,可以在UI线程中调用executePendingTransactions()方法来立即执行事务。但一般不需要这样做,除非有其它线程在等待事务的执行。& &&&注意:只能在activity处于可保存状态的状态时,比如running中,onPause()方法和onStop()方法中提交事务,否则会引发异常。这是因为fragment的状态会丢失。如果要在可能丢失状态的情况下提交事务,请使用commitAllowingStateLoss()。
1.5 Fragment与Activity通讯& && && && &尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后查找activity中的控件们(findViewById())。& && && && && & 有时,可能需要fragment与activity共享事件。一个好办法是在fragment中定义一个回调接口,然后在activity中实现之。例如,还是那个新闻程序的例子,它有一个activity,activity中含有两个fragment。fragmentA显示新闻标题,fragmentB现实标题对应的内容。fragmentA必须在用户选择了某个标题时告诉activity,然后activity再告诉fragmentB,fragmentB就显示出对应的内容。&&二、Fragment实例讲解一
2.1 实例效果图
点击“存储”按钮显示的界面:
点击wifi“按钮”显示的界面:
2.2 项目结构
2.3 具体代码编写1、左边页面布局界面,frag_list.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&无线和网络&
android:textSize=&30sp& /&
android:layout_width=&match_parent&
android:layout_height=&1px&
android:background=&@color/lineColor& /&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:layout_marginLeft=&10dp&
android:orientation=&horizontal& &
android:id=&@+id/wifi&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:text=&WI-Fi&
android:textSize=&30sp& /&
&ToggleButton
android:id=&@+id/toggleButton&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:layout_marginLeft=&20dp&
android:text=&开& /&
&/LinearLayout&
android:layout_width=&match_parent&
android:layout_height=&1px&
android:background=&@color/lineColor& /&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&设备&
android:textSize=&30sp& /&
android:layout_width=&match_parent&
android:layout_height=&1px&
android:background=&@color/lineColor& /&
android:id=&@+id/saveBut&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:layout_marginLeft=&10dp&
android:text=&存储&
android:textSize=&35sp& /&
&/LinearLayout&
2、右边页面布局界面,frag_detail.xml:
&span style=&font-size:12&&&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:background=&@color/right&
android:orientation=&vertical& &
&RelativeLayout
android:id=&@+id/save&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:layout_margin=&10dp&
android:visibility=&gone& &
&include layout=&@layout/save& /&
&/RelativeLayout&
&RelativeLayout
android:id=&@+id/wifi&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:layout_margin=&10dp&
android:visibility=&gone& &
&include layout=&@layout/wifi& /&
&/RelativeLayout&
&/LinearLayout&&/span&
3、主布局界面,main.xml:
&LinearLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&horizontal&
tools:context=&.AndroidFragmentActivity& &
&!-- 主頁面 --&
&!-- 左边页面 --&
android:id=&@+id/frag_list&
android:name=&co.cm.fragement.FragementList&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:layout_weight=&2& /&
&!-- 右面页面 --&
android:id=&@+id/frag_detail&
android:name=&co.cm.fragement.FragementDetails&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:layout_weight=&1& /&
&/LinearLayout&
4、list_item.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:background=&@color/left&
android:orientation=&horizontal& &
&ImageView
android:id=&@+id/img&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content& /&
android:id=&@+id/txt_title&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&Large Text&
android:textAppearance=&?android:attr/textAppearanceLarge& /&
&/LinearLayout&
5、save.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
android:layout_width=&match_parent&
android:layout_height=&1px&
android:background=&@color/lineColor& /&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_marginLeft=&10dp&
android:text=&内部存储空间&
android:textSize=&30sp& /&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_marginBottom=&5dp&
android:layout_marginLeft=&10dp&
android:layout_marginTop=&5dp&
android:text=&1GB/1.98GB&
android:textSize=&20sp& /&
android:layout_width=&match_parent&
android:layout_height=&1px&
android:background=&@color/lineColor& /&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_marginLeft=&20dp&
android:text=&总容量&
android:textSize=&30sp& /&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_marginBottom=&5dp&
android:layout_marginLeft=&20dp&
android:layout_marginTop=&5dp&
android:text=&1.98GB&
android:textSize=&20sp& /&
android:layout_width=&match_parent&
android:layout_height=&1px&
android:background=&@color/lineColor& /&
&/LinearLayout&
6、wifi_list:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
android:id=&@+id/wifi_name&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:text=&qinjin_tp_2& /&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&horizontal& &
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&信号强度
android:id=&@+id/wifi_name_state&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:text=&还没有连接& /&
&/LinearLayout&
&/LinearLayout&
7、wifi.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&RelativeLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
&LinearLayout
android:id=&@+id/wifiLinear&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&vertical& &
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&vertical& &
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&MAC地址
android:textSize=&@dimen/textsize& /&
android:id=&@+id/mac_address&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&MAC地址 &
android:textSize=&@dimen/textsize& /&
&/LinearLayout&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&vertical& &
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&接入点的BSSID :&
android:textSize=&@dimen/textsize& /&
android:id=&@+id/bssid&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&接入点的BSSID &
android:textSize=&@dimen/textsize& /&
&/LinearLayout&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&vertical& &
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&IP地址: &
android:textSize=&@dimen/textsize& /&
android:id=&@+id/ip_address&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&IP地址 &
android:textSize=&@dimen/textsize& /&
&/LinearLayout&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&vertical& &
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&id
android:textSize=&@dimen/textsize& /&
android:id=&@+id/id&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&id &
android:textSize=&@dimen/textsize& /&
&/LinearLayout&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&vertical& &
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=& WifiInfo的所有信息包
android:textSize=&@dimen/textsize& /&
android:id=&@+id/info&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&WifiInfo的所有信息包
android:textSize=&@dimen/textsize& /&
&/LinearLayout&
android:id=&@+id/listview&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:layout_marginBottom=&2dp& &
&/ListView&
&/LinearLayout&
android:id=&@+id/wifiText&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_centerInParent=&true&
android:text=&要查看可用的网络,请打开wifi&
android:textSize=&@dimen/textsize& /&
&/RelativeLayout&
8、主界面类,AndroidFragmentActivity.java:
package co.cm.
import co.cm.fragement.R;
import android.app.A
import android.content.C
import android.os.B
public class AndroidFragmentActivity extends Activity {
// 主activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WifiAdmin.getWifiAdmin().setmContext(AndroidFragmentActivity.this);
WifiAdmin.getWifiAdmin().getWifiMeathod();
9、左面fragment界面类,FragmentList.java:
package co.cm.
import co.cm.fragement.R;
import android.app.F
import android.os.B
import android.os.H
import android.os.M
import android.util.L
import android.view.LayoutI
import android.view.V
import android.view.View.OnClickL
import android.view.ViewG
import poundB
import poundButton.OnCheckedChangeL
import android.widget.LinearL
import android.widget.TextV
import android.widget.ToggleB
* @author yuyang
功能描述:左面fragment界面类,该类提供了选项操作
public class FragementList extends Fragment {
//点击切换到wifi存储界面
private TextV
//点击切换到save存储界面
private TextView saveB
//定义右面fragment实例
private FragementDetails frag_
//打开关闭wifi按钮
private ToggleButton toggleB
//toggleButton按钮是否被点击
private boolean isChecked =
//监听button状态线程标志位
private boolean butIsRunning =
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// 在这里初始化fragment的页面
return inflater.inflate(R.layout.frag_list, container, false);
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// 由于fragment不是activity,不是oncreated,而是onActivityCreated
setView();
setListener();
startThread();// 启动控制button的线程,当wifi状态不是在1或者3的时候,不可点击,
// if (frag != null && frag.isInLayout()) {
// switch (arg2) {
// case 0:
// frag.setText(&0000&);
* 给按钮设置监听
public void setListener() {
saveBut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
frag_detail.setSaveShow();
wifi.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
frag_detail.setWifiShow();
Log.i(&111&, WifiAdmin.getWifiAdmin().checkState() + &===-=-&);
checktoggleButton();// 当点回到wifi界面时,刷新button的状态
toggleButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i(&111&, isChecked + &/& + WifiAdmin.getWifiAdmin().checkState());
if (isChecked) {
WifiAdmin.getWifiAdmin().OpenWifi();
frag_detail.setWifiShow();
// toggleButton.setText(&关闭&);
toggleButton.setChecked(false);
isChecked =
WifiAdmin.getWifiAdmin().CloseWife();
frag_detail.setWifiShow();
// toggleButton.setText(&打开&);
toggleButton.setChecked(true);
isChecked =
toggleButton.setClickable(false);
public void checktoggleButton() {
if (WifiAdmin.getWifiAdmin().checkState() == 1) {
toggleButton.setChecked(true);
isChecked =
if (WifiAdmin.getWifiAdmin().checkState() == 3) {
toggleButton.setChecked(false);
isChecked =
public void setView() {
wifi = (TextView) getView().findViewById(R.id.wifi);
toggleButton = (ToggleButton) getView().findViewById(R.id.toggleButton);
saveBut = (TextView) getView().findViewById(R.id.saveBut);
// 实例化右面界面,以便操纵里面的方法F
frag_detail = (FragementDetails) getFragmentManager().findFragmentById(R.id.frag_detail);
// 初始化button的装态
if (WifiAdmin.getWifiAdmin().checkState() == 3) {
toggleButton.setChecked(false);
isChecked =
if (WifiAdmin.getWifiAdmin().checkState() == 1) {
toggleButton.setChecked(true);
isChecked =
toggleButton.setClickable(true);
public void onDestroy() {
frag_detail.stopWifiThread();
butIsRunning =
super.onDestroy();
private void startThread() {
butIsRunning =
new Thread(new Runnable() {
public void run() {
while (butIsRunning) {
//只有wifi状态改变变化完毕之后才能允许点击按钮
if (WifiAdmin.getWifiAdmin().checkState() == 3) {
if (!isChecked) {
toggleButton.setClickable(true);
} else if (WifiAdmin.getWifiAdmin().checkState() == 1) {
if (isChecked) {
toggleButton.setClickable(true);
}).start();
10、右面fragment界面类
package co.cm.
import java.util.ArrayL
import java.util.L
import co.cm.fragement.R;
import android.app.F
import android.net.wifi.ScanR
import android.net.wifi.WifiC
import android.os.B
import android.os.H
import android.os.M
import android.util.L
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.BaseA
import android.widget.LinearL
import android.widget.ListV
import android.widget.RelativeL
import android.widget.TextV
* @author yangyu
功能描述:右面fragment界面类,该类实现了右面显示的操作
public class FragementDetails extends Fragment {
private TextView mac_address, bssid, ip_address, id, info, wifiT
private ListView listV
private LinearLayout wifiL
private RelativeLayout save,
private boolean ThreadFlag =
//wifi数据适配器
private WifiAdapter wifiA
// 扫描出的网络连接列表
private List&ScanResult& mWifiList = new ArrayList&ScanResult&();
// 网络连接列表
private List&WifiConfiguration& mWifiConfiguration =
private int nowWifiState = 0;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_detail, container, false);
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setView();
// setListener();
setWifiShow();
* 显示wifi界面
public void setWifiShow() {
//通过隐藏显示来达到不同页面内容的切换
save.setVisibility(View.GONE);
wifi.setVisibility(View.VISIBLE);
stopWifiThread();
refreshWifi();
* 显示保存界面
public void setSaveShow() {
stopWifiThread();
save.setVisibility(View.VISIBLE);
wifi.setVisibility(View.GONE);
* 初始化组件
public void setView() {
// -----------------wifi-----------------
wifiText = (TextView) getView().findViewById(R.id.wifiText);
mac_address = (TextView) getView().findViewById(R.id.mac_address);
bssid = (TextView) getView().findViewById(R.id.bssid);
ip_address = (TextView) getView().findViewById(R.id.ip_address);
id = (TextView) getView().findViewById(R.id.id);
info = (TextView) getView().findViewById();
listView = (ListView) getView().findViewById(R.id.listview);
wifiLinear = (LinearLayout) getView().findViewById(R.id.wifiLinear);
save = (RelativeLayout) getView().findViewById(R.id.save);
wifi = (RelativeLayout) getView().findViewById(R.id.wifi);
wifiAdapter = new WifiAdapter();
listView.setAdapter(wifiAdapter);
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
nowWifiState = WifiAdmin.getWifiAdmin().checkState();
// 当wifi打开时,刷新wifi列表的内容
if (nowWifiState == 3) {
mWifiList = WifiAdmin.getWifiAdmin().GetWifiList();
// 如果刚开始检测的wifi列表为空,则创建一个实例化的wifi而不是null,负责会在adpter里面报错
if (mWifiList != null) {
// 如果wifi列表发生改变,则更新,else不更新
if (!mWifiList.toString().equals(
WifiAdmin.getWifiAdmin().getLastWifiList().toString())) {
WifiAdmin.getWifiAdmin().setLastWifiList(mWifiList);
wifiAdapter.notifyDate();
mWifiList = new ArrayList&ScanResult&();
refreshMeathod();
super.handleMessage(msg);
* 刷新wifi的状态
public void refreshWifi() {
new Thread(new Runnable() {
public void run() {
ThreadFlag =
while (ThreadFlag) {
// Log.i(&111&, WifiAdmin.getWifiAdmin().checkState() +
// &!!!&);
Message msg = handler.obtainMessage();
handler.sendMessage(msg);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}).start();
public void refreshMeathod() {
// 此处可用switch
if (nowWifiState == 3) {
wifiLinear.setVisibility(View.VISIBLE);
wifiText.setVisibility(View.INVISIBLE);
mac_address.setText(WifiAdmin.getWifiAdmin().GetMacAddress() + &&);
bssid.setText(WifiAdmin.getWifiAdmin().GetBSSID() + &&);
ip_address.setText(WifiAdmin.getWifiAdmin().GetIPAddress() + &&);
id.setText(WifiAdmin.getWifiAdmin().GetNetworkId() + &&);
info.setText(WifiAdmin.getWifiAdmin().GetWifiInfo() + &&);
} else if (nowWifiState == 1) {
wifiText.setVisibility(View.VISIBLE);
wifiLinear.setVisibility(View.INVISIBLE);
wifiText.setText(&要查看可用的网络,请打开wifi&);
} else if (nowWifiState == 2) {
wifiText.setVisibility(View.VISIBLE);
wifiLinear.setVisibility(View.INVISIBLE);
wifiText.setText(&wifi正在打开&);
} else if (nowWifiState == 4) {
wifiText.setVisibility(View.VISIBLE);
wifiLinear.setVisibility(View.INVISIBLE);
wifiText.setText(&wifi正在关闭&);
wifiText.setVisibility(View.VISIBLE);
wifiLinear.setVisibility(View.INVISIBLE);
wifiText.setText(&我不知道wifi正在做什么&);
public void stopWifiThread() {
ThreadFlag =
public class WifiAdapter extends BaseAdapter {
public int getCount() {
return mWifiList.size();
public Object getItem(int position) {
return mWifiList.get(position);
public long getItemId(int position) {
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertV
final ChatViewH
if (convertView == null) {
vh = new ChatViewHolder();
view = View.inflate(WifiAdmin.getWifiAdmin().getmContext(),
R.layout.wifi_list, null);
vh.wifi_name = (TextView) view.findViewById(R.id.wifi_name);
vh.wifi_name_state = (TextView) view
.findViewById(R.id.wifi_name_state);
view.setTag(vh);
vh = (ChatViewHolder) view.getTag();
vh.wifi_name.setText(mWifiList.get(position).SSID.toString());// 网络的名字,唯一区别WIFI网络的名字
vh.wifi_name_state.setText(mWifiList.get(position).level + &&);
public void notifyDate() {
notifyDataSetChanged();
public class ChatViewHolder {
TextView wifi_// 网络的名字,唯一区别WIFI网络的名字
TextView wifi_name_// 所发现的WIFI网络信号强度
11、wifiAdmin类,提供了wifi操作的方法,WifiAdmin.java:
package co.cm.
import java.util.ArrayL
import java.util.L
import android.content.C
import android.net.wifi.ScanR
import android.net.wifi.WifiC
import android.net.wifi.WifiI
import android.net.wifi.WifiM
import android.net.wifi.WifiManager.WifiL
import android.util.L
* @author yangyu
wifiAdmin提供了wifi操作的方法
public class WifiAdmin {
private static WifiAdmin wifiA
private WifiManager mWifiManager =
private WifiInfo mWifiInfo =
// 扫描出的网络连接列表
private List&ScanResult& mWifiList = new ArrayList&ScanResult&();
// 扫描出的网络连接列表
private List&ScanResult& lastWifiList = new ArrayList&ScanResult&();
// 网络连接列表
private List&WifiConfiguration& mWifiConfiguration =
private WifiLock mWifiLock =
// 上次网络状态
private int lastWifiState = 0;
//定义上下文Context
Context mC
public List&ScanResult& getLastWifiList() {
return lastWifiL
public void setLastWifiList(List&ScanResult& lastWifiList) {
this.lastWifiList = lastWifiL
public int getLastWifiState() {
return lastWifiS
public void setLastWifiState(int lastWifiState) {
this.lastWifiState = lastWifiS
public static WifiAdmin getWifi() {
return wifiA
public Context getmContext() {
public void setmContext(Context mContext) {
this.mContext = mC
public static WifiAdmin getWifiAdmin() {
if (wifiAdmin == null) {
wifiAdmin = new WifiAdmin();
return wifiA
public void getWifiMeathod() {
mWifiManager = (WifiManager) mContext
.getSystemService(mContext.WIFI_SERVICE);
mWifiInfo = mWifiManager.getConnectionInfo();
* 打开wifi
public void OpenWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
Log.i(&111&, &open 失败&);
* 关闭wifi
public void CloseWife() {
if (mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
Log.i(&111&, &close 失败&);
* 锁定wifi
public void lockWifi() {
mWifiLock.acquire();
public void rlockWifi() {
if (mWifiLock.isHeld()) {
mWifiLock.acquire();
// 检查当前wifi状态WIFI网卡的状态是由一系列的整形常量来表示的。
//1.WIFI_STATE_DISABLED : WIFI网卡不可用(1)
//2.WIFI_STATE_DISABLING : WIFI网卡正在关闭(0)
//3.WIFI_STATE_ENABLED : WIFI网卡可用(3)
//4.WIFI_STATE_ENABLING : WIFI网正在打开(2) (WIFI启动需要一段时间)
//5.WIFI_STATE_UNKNOWN : 未知网卡状态
public int checkState() {
return mWifiManager.getWifiState();
* 创建一个wifilock
public void Createwifilock() {
mWifiLock = mWifiManager.createWifiLock(&Testss&);
* 得到配置好的网络
public List&WifiConfiguration& GetConfinguration() {
return mWifiC
* 连接配置好的指定ID的网络
* @param index
public void ConnectConfiguration(int index) {
if (index & mWifiConfiguration.size()) {
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,true);
* 开始扫描网络
public void StartScan() {
mWifiManager.startScan();
// 得到扫描结果
mWifiList = mWifiManager.getScanResults();
// 得到配置好的网络连接
mWifiConfiguration = mWifiManager.getConfiguredNetworks();
* 得到网络列表
public List&ScanResult& GetWifiList() {
mWifiManager.startScan();
// 得到扫描结果
mWifiList = mWifiManager.getScanResults();
return mWifiL
public List&WifiConfiguration& getmWifiConfiguration() {
return mWifiC
* 查看扫描结果
public StringBuilder LookUpScan() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i & mWifiList.size(); i++) {
stringBuilder.append(&Index_& + new Integer(i + 1).toString() + &:&);
// 将ScanResult信息转换成一个字符串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append(&\n&);
return stringB
* 得到MAC地址
public String GetMacAddress() {
return (mWifiInfo == null) ? &NULL& : mWifiInfo.getMacAddress();
* 得到接入点的BSSID
public String GetBSSID() {
return (mWifiInfo == null) ? &NULL& : mWifiInfo.getBSSID();
* 得到IP地址
public int GetIPAddress() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
* 得到连接的ID
public int GetNetworkId() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
* 得到WifiInfo的所有信息包
public String GetWifiInfo() {
return (mWifiInfo == null) ? &NULL& : mWifiInfo.toString();
* 添加一个网络并连接
public void AddNetwork(WifiConfiguration wcg) {
int wcgID = mWifiManager.addNetwork(wcg);
mWifiManager.enableNetwork(wcgID, true);
* 断开指定ID的网络
public void DisconnectWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
小结: 当我们需要在一个界面中处理很多事情的时候,可以推荐使用fragment,因为他会把我们的activity分割成很多小块,每个小块都有他的生命周期,非常方便,而有时我们会用单例模式来存储每个页面都有的东西。
三、Fragment实例讲解二 3.1 项目的效果图& && && && && && && && && && && && && && &
3.2 项目结构目录
3.3 代码具体编写1、标题栏的布局界面,title_view.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&50dip&
android:background=&@drawable/title_bg&
android:orientation=&horizontal& &
android:id=&@+id/left_btn&
style=&@style/Text.Title_Button&
android:layout_width=&wrap_content&
android:layout_height=&35dip&
android:layout_gravity=&center_vertical&
android:background=&@drawable/title_btn_back&
android:minWidth=&60dip& /&
android:id=&@+id/title_text&
style=&@style/Text.Title&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:layout_weight=&1& /&
android:id=&@+id/right_btn&
style=&@style/Text.Title_Button&
android:layout_width=&wrap_content&
android:layout_height=&35dip&
android:layout_gravity=&center_vertical&
android:background=&@drawable/title_btn&
android:minWidth=&70dip& /&
&/LinearLayout&
2、首页的fragment页面,这里就列出一个,fragment_home.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&vertical& &
&com.eoe.tampletfragment.view.TitleView
android:id=&@+id/title&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content& /&
android:id=&@+id/fragment_home_text&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:text=&@string/fragment_home_text&
android:textSize=&18sp& /&
&/LinearLayout&
3、帮助Activity界面,activity_help.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:background=&@drawable/activity_bg&
android:orientation=&vertical& &
&com.eoe.tampletfragment.view.TitleView
android:id=&@+id/title&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content& /&
&/LinearLayout&
4、主页面布局,activity_main.xml:
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:background=&@drawable/activity_bg&
android:orientation=&vertical& &
android:id=&@+id/fragment_home&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:layout_weight=&1&
class=&com.eoe.tampletfragment.fragment.HomeFragment& /&
android:id=&@+id/fragment_search&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:layout_weight=&1&
class=&com.eoe.tampletfragment.fragment.SearchFragment& /&
android:id=&@+id/fragment_settings&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:layout_weight=&1&
class=&com.eoe.tampletfragment.fragment.SettingsFragment& /&
&com.eoe.tampletfragment.fragment.FragmentIndicator
android:id=&@+id/indicator&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:background=&@drawable/tab_footer_bg& /&
&/LinearLayout&
详细说明:&&&1& 主页面MainActivity继承自FragmentActivity类,负责实现导航按钮所对应页面的显示和隐藏。
(详细实现见源码)
&&&2& 主页面由底部导航栏和面板组成。&&&3& fragment标签所对应Fragment的实现类。
&&&4& com.eoe.tampletfragment.fragment.FragmentIndicator标签所对应的是底部导航栏。& &
5、自定义顶部工具栏,TitleView.java:
package com.eoe.tampletfragment.
import android.content.C
import android.util.AttributeS
import android.view.LayoutI
import android.view.V
import android.widget.B
import android.widget.FrameL
import android.widget.TextV
import com.eoe.tampletfragment.R;
* @author yangyu
功能描述:自定义顶部工具栏
public class TitleView extends FrameLayout implements View.OnClickListener {
private Button mLeftB
private Button mRightB
private TextView mT
private OnLeftButtonClickListener mOnLeftButtonClickL
private OnRightButtonClickListener mOnRightButtonClickL
public interface OnLeftButtonClickListener {
public void onClick(View button);
public interface OnRightButtonClickListener {
public void onClick(View button);
public void setLeftButton(String text, OnLeftButtonClickListener listener) {
mLeftBtn.setText(text);
mLeftBtn.setVisibility(View.VISIBLE);
mOnLeftButtonClickListener =
public void setLeftButton(int stringID, OnLeftButtonClickListener listener) {
mLeftBtn.setText(stringID);
mLeftBtn.setVisibility(View.VISIBLE);
mOnLeftButtonClickListener =
public void removeLeftButton() {
mLeftBtn.setText(&&);
mLeftBtn.setVisibility(View.INVISIBLE);
mOnLeftButtonClickListener =
public void hiddenLeftButton() {
mLeftBtn.setVisibility(View.INVISIBLE);
public void showLeftButton() {
mLeftBtn.setVisibility(View.VISIBLE);
public void setRightButton(String text, OnRightButtonClickListener listener) {
mRightBtn.setText(text);
mRightBtn.setVisibility(View.VISIBLE);
mOnRightButtonClickListener =
public void setRightButton(int stringID, OnRightButtonClickListener listener) {
mRightBtn.setText(stringID);
mRightBtn.setVisibility(View.VISIBLE);
mOnRightButtonClickListener =
public void removeRightButton() {
mRightBtn.setText(&&);
mRightBtn.setVisibility(View.INVISIBLE);
mOnRightButtonClickListener =
public void hiddenRightButton() {
mRightBtn.setVisibility(View.INVISIBLE);
public void showRightButton() {
mRightBtn.setVisibility(View.VISIBLE);
public TitleView(Context context) {
this(context, null);
public TitleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
public TitleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.title_view, this, true);
mLeftBtn = (Button) findViewById(R.id.left_btn);
mLeftBtn.setVisibility(View.INVISIBLE);
mLeftBtn.setOnClickListener(this);
mRightBtn = (Button) findViewById(R.id.right_btn);
mRightBtn.setVisibility(View.INVISIBLE);
mRightBtn.setOnClickListener(this);
mTitle = (TextView) findViewById(R.id.title_text);
mTitle.setVisibility(View.INVISIBLE);
public void setTitle(String text) {
mTitle.setVisibility(View.VISIBLE);
mTitle.setText(text);
public void setTitle(int stringID) {
mTitle.setVisibility(View.VISIBLE);
mTitle.setText(stringID);
public void onClick(View v) {
switch (v.getId()) {
case R.id.left_btn:
if(mOnLeftButtonClickListener != null)
mOnLeftButtonClickListener.onClick(v);
case R.id.right_btn:
if(mOnRightButtonClickListener != null)
mOnRightButtonClickListener.onClick(v);
6、自定义底部工具栏,FragmentIndicator.java:
package com.eoe.tampletfragment.
import android.content.C
import android.graphics.C
import android.util.AttributeS
import android.util.TypedV
import android.view.G
import android.view.V
import android.view.View.OnClickL
import android.widget.ImageV
import android.widget.LinearL
import android.widget.TextV
import com.eoe.tampletfragment.R;
* @author yangyu
功能描述:自定义底部工具栏
public class FragmentIndicator extends LinearLayout implements OnClickListener {
private int mDefaultIndicator = 0;
private static int mCurI
private static View[] mI
private OnIndicateListener mOnIndicateL
private static final String TAG_ICON_0 = &icon_tag_0&;
private static final String TAG_ICON_1 = &icon_tag_1&;
private static final String TAG_ICON_2 = &icon_tag_2&;
private static final String TAG_TEXT_0 = &text_tag_0&;
private static final String TAG_TEXT_1 = &text_tag_1&;
private static final String TAG_TEXT_2 = &text_tag_2&;
private static final int COLOR_UNSELECT = Color.argb(100, 0xff, 0xff, 0xff);
private static final int COLOR_SELECT = Color.WHITE;
private FragmentIndicator(Context context) {
super(context);
public FragmentIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
mCurIndicator = mDefaultI
setOrientation(LinearLayout.HORIZONTAL);
private View createIndicator(int iconResID, int stringResID, int stringColor,
String iconTag, String textTag) {
LinearLayout view = new LinearLayout(getContext());
view.setOrientation(LinearLayout.VERTICAL);
view.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
view.setGravity(Gravity.CENTER_HORIZONTAL);
ImageView iconView = new ImageView(getContext());
iconView.setTag(iconTag);
iconView.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
iconView.setImageResource(iconResID);
TextView textView = new TextView(getContext());
textView.setTag(textTag);
textView.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
textView.setTextColor(stringColor);
textView.PLEX_UNIT_SP, 16);
textView.setText(stringResID);
view.addView(iconView);
view.addView(textView);
private void init() {
mIndicators = new View[3];
mIndicators[0] = createIndicator(R.drawable.ic_home_focused,
R.string.tab_home, COLOR_SELECT, TAG_ICON_0, TAG_TEXT_0);
mIndicators[0].setBackgroundResource(R.drawable.indic_select);
mIndicators[0].setTag(Integer.valueOf(0));
mIndicators[0].setOnClickListener(this);
addView(mIndicators[0]);
mIndicators[1] = createIndicator(R.drawable.ic_search_normal,
R.string.tab_search, COLOR_UNSELECT, TAG_ICON_1, TAG_TEXT_1);
mIndicators[1].setBackgroundColor(Color.alpha(0));
mIndicators[1].setTag(Integer.valueOf(1));
mIndicators[1].setOnClickListener(this);
addView(mIndicators[1]);
mIndicators[2] = createIndicator(R.drawable.ic_settings_normal,
R.string.tab_settings, COLOR_UNSELECT, TAG_ICON_2, TAG_TEXT_2);
mIndicators[2].setBackgroundColor(Color.alpha(0));
mIndicators[2].setTag(Integer.valueOf(2));
mIndicators[2].setOnClickListener(this);
addView(mIndicators[2]);
public static void setIndicator(int which) {
// clear previous status.
mIndicators[mCurIndicator].setBackgroundColor(Color.alpha(0));
ImageView prevI
TextView prevT
switch(mCurIndicator) {
prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_0);
prevIcon.setImageResource(R.drawable.ic_home_normal);
prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_0);
prevText.setTextColor(COLOR_UNSELECT);
prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_1);
prevIcon.setImageResource(R.drawable.ic_search_normal);
prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_1);
prevText.setTextColor(COLOR_UNSELECT);
prevIcon =(ImageView) mIndicators[mCurIndicator].findViewWithTag(TAG_ICON_2);
prevIcon.setImageResource(R.drawable.ic_settings_normal);
prevText = (TextView) mIndicators[mCurIndicator].findViewWithTag(TAG_TEXT_2);
prevText.setTextColor(COLOR_UNSELECT);
// update current status.
mIndicators[which].setBackgroundResource(R.drawable.indic_select);
ImageView currI
TextView currT
switch(which) {
currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_0);
currIcon.setImageResource(R.drawable.ic_home_focused);
currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_0);
currText.setTextColor(COLOR_SELECT);
currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_1);
currIcon.setImageResource(R.drawable.ic_search_focused);
currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_1);
currText.setTextColor(COLOR_SELECT);
currIcon =(ImageView) mIndicators[which].findViewWithTag(TAG_ICON_2);
currIcon.setImageResource(R.drawable.ic_settings_focused);
currText = (TextView) mIndicators[which].findViewWithTag(TAG_TEXT_2);
currText.setTextColor(COLOR_SELECT);
mCurIndicator =
public interface OnIndicateListener {
public void onIndicate(View v, int which);
public void setOnIndicateListener(OnIndicateListener listener) {
mOnIndicateListener =
public void onClick(View v) {
if (mOnIndicateListener != null) {
int tag = (Integer) v.getTag();
switch (tag) {
if (mCurIndicator != 0) {
mOnIndicateListener.onIndicate(v, 0);
setIndicator(0);
if (mCurIndicator != 1) {
mOnIndicateListener.onIndicate(v, 1);
setIndicator(1);
if (mCurIndicator != 2) {
mOnIndicateListener.onIndicate(v, 2);
setIndicator(2);
7、首页fragment页面,HomeFragment.java:
package com.eoe.tampletfragment.
import android.content.I
import android.os.B
import android.support.v4.app.F
import android.support.v4.app.FragmentA
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.TextV
import com.eoe.tampletfragment.HelpA
import com.eoe.tampletfragment.R;
import com.eoe.tampletfragment.view.TitleV
import com.eoe.tampletfragment.view.TitleView.OnLeftButtonClickL
import com.eoe.tampletfragment.view.TitleView.OnRightButtonClickL
* @author yangyu
功能描述:首页fragment页面
public class HomeFragment extends Fragment {
private View mP
private FragmentActivity mA
private TitleView mT
private TextView mT
* Create a new instance of DetailsFragment, initialized to show the text at
* 'index'.
public static HomeFragment newInstance(int index) {
HomeFragment f = new HomeFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt(&index&, index);
f.setArguments(args);
public int getShownIndex() {
return getArguments().getInt(&index&, 0);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mActivity = getActivity();
mParent = getView();
mTitle = (TitleView) mParent.findViewById(R.id.title);
mTitle.setTitle(R.string.title_home);
mTitle.setLeftButton(R.string.exit, new OnLeftButtonClickListener(){
public void onClick(View button) {
mActivity.finish();
mTitle.setRightButton(R.string.help, new OnRightButtonClickListener() {
public void onClick(View button) {
goHelpActivity();
mText = (TextView) mParent.findViewById(R.id.fragment_home_text);
private void goHelpActivity() {
Intent intent = new Intent(mActivity, HelpActivity.class);
startActivity(intent);
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
public void onDestroy() {
super.onDestroy();
8、Activity帮助界面,HelpActivity.java:
package com.eoe.
import android.os.B
import android.support.v4.app.FragmentA
import android.view.W
* @author yangyu
功能描述:帮助Activity界面
public class HelpActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_help);
9、Activity主界面,MainActivity.java:
package com.eoe.
import android.os.B
import android.support.v4.app.F
import android.support.v4.app.FragmentA
import android.view.V
import android.view.W
import com.eoe.tampletfragment.fragment.FragmentI
import com.eoe.tampletfragment.fragment.FragmentIndicator.OnIndicateL
* @author yangyu
功能描述:主Activity类,继承自FragmentActivity
public class MainActivity extends FragmentActivity {
public static Fragment[] mF
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setFragmentIndicator(0);
* 初始化fragment
private void setFragmentIndicator(int whichIsDefault) {
mFragments = new Fragment[3];
mFragments[0] = getSupportFragmentManager().findFragmentById(R.id.fragment_home);
mFragments[1] = getSupportFragmentManager().findFragmentById(R.id.fragment_search);
mFragments[2] = getSupportFragmentManager().findFragmentById(R.id.fragment_settings);
getSupportFragmentManager().beginTransaction().hide(mFragments[0])
.hide(mFragments[1]).hide(mFragments[2]).show(mFragments[whichIsDefault]).commit();
FragmentIndicator mIndicator = (FragmentIndicator) findViewById(R.id.indicator);
FragmentIndicator.setIndicator(whichIsDefault);
mIndicator.setOnIndicateListener(new OnIndicateListener() {
public void onIndicate(View v, int which) {
getSupportFragmentManager().beginTransaction()
.hide(mFragments[0]).hide(mFragments[1])
.hide(mFragments[2]).show(mFragments[which]).commit();
protected void onResume() {
super.onResume();
protected void onPause() {
super.onPause();
原文出处:
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:214600次
积分:2855
积分:2855
排名:第5558名
原创:55篇
转载:16篇
评论:230条
(3)(1)(1)(2)(1)(4)(14)(7)(1)(2)(1)(1)(9)(18)(1)(3)(5)

我要回帖

更多关于 preference 的文章

 

随机推荐