Android Fragment实现.docx

上传人:b****5 文档编号:6497819 上传时间:2023-01-07 格式:DOCX 页数:17 大小:185.61KB
下载 相关 举报
Android Fragment实现.docx_第1页
第1页 / 共17页
Android Fragment实现.docx_第2页
第2页 / 共17页
Android Fragment实现.docx_第3页
第3页 / 共17页
Android Fragment实现.docx_第4页
第4页 / 共17页
Android Fragment实现.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

Android Fragment实现.docx

《Android Fragment实现.docx》由会员分享,可在线阅读,更多相关《Android Fragment实现.docx(17页珍藏版)》请在冰豆网上搜索。

Android Fragment实现.docx

AndroidFragment实现

Android手机平板使用Fragment实现兼容手机和平板的程序

我们先来看一下Android手机的设置界面,点击一下Sound,可以跳转到声音设置界面,如下面两张图所示:

   

  然后再来看一下AndroidPad的设置界面,主设置页面和声音设置页面都是在一个界面显示的,如下图所示:

   

  如果这分别是两个不同的App做出的效果,那没有丝毫惊奇之处。

但如果是同一个App,在手机上和平板上运行分别有以上两种效果的话,你是不是就已经心动了?

我们现在就来模拟实现一下。

  首先你需要对Fragment有一定的了解,如果你还没接触过Fragment,建议可以先阅读AndroidFragment完全解析,关于碎片你所需知道的一切这篇文章。

并且本次的代码是运行在Android4.0版本上的,如果你的SDK版本还比较低的话,建议可以先升升级了。

  新建一个Android项目,取名叫FragmentDemo。

打开或新建MainActivity作为程序的主Activity,里面有如下自动生成的内容:

  

1.[java]viewplaincopyprint?

2.  publicclassMainActivityextendsActivity{

3.  @Override

4.  publicvoidonCreate(BundlesavedInstanceState){

5.  super.onCreate(savedInstanceState);

6.  setContentView(R.layout.activity_main);

7.  }

8.  }

复制代码

  作为一个Android老手,上面的代码实在太小儿科了,每个Activity中都会有这样的代码。

不过今天我们的程序可不会这么简单,加载布局这一块还是大有文章的。

  打开或新建res/layout/activity_main.xml作为程序的主布局文件,里面代码如下:

  

1.[html]viewplaincopyprint?

2.   

3.  xmlns:

tools="

4.  android:

layout_width="fill_parent"

5.  android:

layout_height="fill_parent"

6.  android:

orientation="horizontal"

7.  tools:

context=".MainActivity">

8.   

9.  android:

id="@+id/menu_fragment"

10.  android:

name="com.example.fragmentdemo.MenuFragment"

11.  android:

layout_width="fill_parent"

12.  android:

layout_height="fill_parent"

13.  />

14.  

15.  这个布局引用了一个MenuFragment,我们稍后来进行实现,先来看一下今天的一个重点,我们需要再新建一个activity_main.xml,这个布局文件名和前面的主布局文件名是一样的,但是要放在不同的目录下面。

16.  在res目录下新建layout-large目录,然后这个目录下创建新的activity_main.xml,加入如下代码:

17.  [html]viewplaincopyprint?

18.   

19.  xmlns:

tools="

20.  android:

layout_width="fill_parent"

21.  android:

layout_height="fill_parent"

22.  android:

orientation="horizontal"

23.  android:

baselineAligned="false"

24.  tools:

context=".MainActivity"

25.  >

26.   

27.  android:

id="@+id/left_fragment"

28.  android:

name="com.example.fragmentdemo.MenuFragment"

29.  android:

layout_width="0dip"

30.  android:

layout_height="fill_parent"

31.  android:

layout_weight="1"

32.  />

33.   

34.  android:

id="@+id/details_layout"

35.  android:

layout_width="0dip"

36.  android:

layout_height="fill_parent"

37.  android:

layout_weight="3"

38.  >

复制代码

  

  这个布局同样也引用了MenuFragment,另外还加入了一个FrameLayout用于显示详细内容。

其实也就是分别对应了平板界面上的左侧布局和右侧布局。

  这里用到了动态加载布局的技巧,首先Activity中调用setContentView(R.layout.activity_main),表明当前的Activity想加载activity_main这个布局文件。

而Android系统又会根据当前的运行环境判断程序是否运行在大屏幕设备上,如果运行在大屏幕设备上,就加载layout-large目录下的activity_main.xml,否则就默认加载layout目录下的activity_main.xml。

  关于动态加载布局的更多内容,可以阅读Android官方提供的支持不同屏幕大小的全部方法这篇文章。

  下面我们来实现久违的MenuFragment,新建一个MenuFragment类继承自Fragment,具体代码如下:

  

1.[java]viewplaincopyprint?

2.  publicclassMenuFragmentextendsFragmentimplementsOnItemClickListener{

3.  /**

4.  *菜单界面中只包含了一个ListView。

5.  */

6.  privateListViewmenuList;

7.  /**

8.  *ListView的适配器。

9.  */

10.  privateArrayAdapteradapter;

11.  /**

12.  *用于填充ListView的数据,这里就简单只用了两条数据。

13.  */

14.  privateString[]menuItems={"Sound","Display"};

15.  /**

16.  *是否是双页模式。

如果一个Activity中包含了两个Fragment,就是双页模式。

17.  */

18.  privatebooleanisTwoPane;

19.  /**

20.  *当Activity和Fragment建立关联时,初始化适配器中的数据。

21.  */

22.  @Override

23.  publicvoidonAttach(Activityactivity){

24.  super.onAttach(activity);

25.  adapter=newArrayAdapter(activity,android.R.layout.simple_list_item_1,menuItems);

26.  }

27.  /**

28.  *加载menu_fragment布局文件,为ListView绑定了适配器,并设置了监听事件。

29.  */

30.  @Override

31.  publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){

32.  Viewview=inflater.inflate(R.layout.menu_fragment,container,false);

33.  menuList=(ListView)view.findViewById(R.id.menu_list);

34.  menuList.setAdapter(adapter);

35.  menuList.setOnItemClickListener(this);

36.  returnview;

37.  }

38.  /**

39.  *当Activity创建完毕后,尝试获取一下布局文件中是否有details_layout这个元素,如果有说明当前

40.  *是双页模式,如果没有说明当前是单页模式。

41.  */

42.  @Override

43.  publicvoidonActivityCreated(BundlesavedInstanceState){

44.  super.onActivityCreated(savedInstanceState);

45.  if(getActivity().findViewById(R.id.details_layout)!

=null){

46.  isTwoPane=true;

47.  }else{

48.  isTwoPane=false;

49.  }

50.  }

51.  /**

52.  *处理ListView的点击事件,会根据当前是否是双页模式进行判断。

如果是双页模式,则会动态添加Fragment。

53.  *如果不是双页模式,则会打开新的Activity。

54.  */

55.  @Override

56.  publicvoidonItemClick(AdapterView

>arg0,Viewview,intindex,longarg3){

57.  if(isTwoPane){

58.  Fragmentfragment=null;

59.  if(index==0){

60.  fragment=newSoundFragment();

61.  }elseif(index==1){

62.  fragment=newDisplayFragment();

63.  }

64.  getFragmentManager().beginTransaction().replace(R.id.details_layout,fragment).commit();

65.  }else{

66.  Intentintent=null;

67.  if(index==0){

68.  intent=newIntent(getActivity(),SoundActivity.class);

69.  }elseif(index==1){

70.  intent=newIntent(getActivity(),DisplayActivity.class);

71.  }

72.  startActivity(intent);

73.  }

74.  }

75.  }

复制代码

  这个类的代码并不长,我简单的说明一下。

在onCreateView方法中加载了menu_fragment这个布局,这个布局里面包含了一个ListView,然后我们对这个ListView填充了两个简单的数据"Sound"和"Display"。

又在onActivityCreated方法中做了一个判断,如果Activity的布局中包含了details_layout这个元素,那么当前就是双页模式,否则就是单页模式。

onItemClick方法则处理了ListView的点击事件,发现如果当前是双页模式,就动态往details_layout中添加Fragment,如果当前是单页模式,就直接打开新的Activity。

  我们把MenuFragment中引用到的其它内容一个个添加进来。

新建menu_fragment.xml文件,加入如下代码:

  

1.[html]viewplaincopyprint?

2.  

xmlversion="1.0"encoding="UTF-8"?

>

3.   

4.  android:

layout_width="fill_parent"

5.  android:

layout_height="fill_parent">

6.   

7.  android:

id="@+id/menu_list"

8.  android:

layout_width="fill_parent"

9.  android:

layout_height="fill_parent"

10.  >

复制代码

  然后新建SoundFragment,里面内容非常简单:

  

1.[java]viewplaincopyprint?

2.  publicclassSoundFragmentextendsFragment{

3.  @Override

4.  publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){

5.  Viewview=inflater.inflate(R.layout.sound_fragment,container,false);

6.  returnview;

7.  }

8.  }

复制代码

  这里SoundFragment需要用到sound_fragment.xml布局文件,因此这里我们新建这个布局文件,并加入如下代码:

  

1.[html]viewplaincopyprint?

2.  

3.  

android="    android:

layout_width="match_parent"

4.  android:

layout_height="match_parent"

5.  android:

background="#00ff00"

6.  android:

orientation="vertical">

7.  

layout_width="wrap_content"

8.  android:

layout_height="wrap_content"

9.  android:

layout_centerInParent="true"

10.  android:

textSize="28sp"

11.  android:

textColor="#000000"

12.  android:

text="Thisissoundview"

13.  />

复制代码

  

  同样的道理,我们再新建DisplayFragment和display_fragment.xml布局文件:

  

1.[java]viewplaincopyprint?

2.  publicclassDisplayFragmentextendsFragment{

3.  publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){

4.  Viewview=inflater.inflate(R.layout.display_fragment,container,false);

5.  returnview;

6.  }

7.  }

8.  [html]viewplaincopyprint?

9.  

10.  

android="    android:

layout_width="match_parent"

11.  android:

layout_height="match_parent"

12.  android:

background="#0000ff"

13.  android:

orientation="vertical">

14.  

layout_width="wrap_content"

15.  android:

layout_height="wrap_content"

16.  android:

layout_centerInParent="true"

17.  android:

textSize="28sp"

18.  android:

textColor="#000000"

19.  android:

text="Thisisdisplayview"

20.  />

复制代码

  

  然后新建SoundActivity,代码如下:

  

1.[java]viewplaincopyprint?

2.  publicclassSoundActivityextendsActivity{

3.  @Override

4.  protectedvoidonCreate(BundlesavedInstanceState){

5.  super.onCreate(savedInstanceState);

6.  setContentView(R.layout.sound_activity);

7.  }

8.  }

复制代码

  这个Activity只是加载了一个布局文件,现在我们来实现sound_activity.xml这个布局文件:

 

1. [html]viewplaincopyprint?

2.  

3.  

android="    android:

id="@+id/sound_fragment"

4.  android:

name="com.example.fragmentdemo.SoundFragment"

5.  android:

layout_width="match_parent"

6.  android:

layout_height="match_parent">

7.  

复制代码

  这个布局文件引用了SoundFragment,这样写的好处就是,以后我们只需要在SoundFragment中修改代码,SoundActivity就会跟着自动改变了,因为它所有的代码都是从SoundFragment中引用过来的。

  好,同样的方法,我们再完成DisplayActivity:

 

1. [java]viewplaincopyprint?

2.  publicclassDisplayActivityextendsActivity{

3.  @Override

4.  protectedvoidonCreate(BundlesavedInstanceState){

5.  super.onCreate(savedInstanceState);

6.  setContentView(R.layout.display_activity);

7.  }

8.  }

复制代码

  然后加入display_activity.xml:

  

1.[html]viewplaincopyprint?

2.  

3.  

android="    android:

id="@+id/display_fragment"

4.  android:

name="com.example.fragmentdemo.DisplayFragment"

5.  android:

layout_width="match_parent"

6.  android:

layout_height="match_parent">

复制代码

  

  现在所有的代码就都已经完成了,我们来看一下效果吧。

  首先将程序运行在手机上,效果图如下:

   

  分别点击Sound和Display,界面会跳转到声音和显示界面:

   

  然后将程序在平板上运行,点击Sound,效果图如下:

   

  然后点击Display切换到显示界面,效果图如下:

   

  这样我们就成功地让程序同时兼容手机和平板了。

当然,这只是一个简单的demo,更多复杂的内容需要大家自己去实现了。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1