Android手机平板两不误 fragment(ListView)
2015-07-30 17:10
489 查看
Fragment工程下
1:layout文件夹下先建立fragment_left.xml
2:layout文件夹下再建立fragment_right.xml
3:com.example.fragments包下新建FragmentLeft.java
4:com.example.fragments包下再新建FragmentRight.java
5:layout文件夹下activity_main.xml写 1- fragment_left 和 2-fragment_right
6:MainActivity.java 再加载5-activity_main.xml
代码如下:
fragment_left.xml
fragment_right.xml
FragmentLeft.java
这里注意继承的
activity_main.xml
注意:这里用
MainActivity.java
结果:
onAttach方法:Fragment和Activity建立关联的时候调用。
onCreateView方法:为Fragment加载布局时调用。
onActivityCreated方法:当Activity中的onCreate方法执行完后调用。
onDestroyView方法:Fragment中的布局被移除时调用。
onDetach方法:Fragment和Activity解除关联的时候调用。
Fragment真正的强大之处在于可以动态地添加到Activity当中
1:activity_main.xml用来存放手机布局
2:MenuFragment.java(内容第四步)
3:新建layout-large文件夹下的activity_main.xml,将平板分成左右两侧,左侧是fragment右侧是FrameLayout
4:MenuFragment.java
4.1 需要ListView/adapter/data
4.2 新建sound_fragment.xml
4.3 新建SoundFragment.java
4.4 新建display_fragment.xml
4.5 新建DisplayFragment.java
4.6 新建sound_acitivity.xml和SoundActivity.java
4.7新建display_acitivity.xml和DisplayActivity.java
AndroidManifest.xml
出错:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first
解决方案:
结果:
源码分享:
这里写链接内容
1:layout文件夹下先建立fragment_left.xml
2:layout文件夹下再建立fragment_right.xml
3:com.example.fragments包下新建FragmentLeft.java
4:com.example.fragments包下再新建FragmentRight.java
5:layout文件夹下activity_main.xml写 1- fragment_left 和 2-fragment_right
6:MainActivity.java 再加载5-activity_main.xml
代码如下:
fragment_left.xml
[code]<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff3300" > <TextView android:id="@+id/fragment_left" android:layout_width="match_parent" android:layout_height="match_parent" android:text="this is fragement left"/> </LinearLayout>
fragment_right.xml
[code]<?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" android:background="#ff0394"> <TextView android:id="@+id/fragment_right" android:layout_width="match_parent" android:layout_height="match_parent" android:text="this is fragement right"/> </LinearLayout>
FragmentLeft.java
[code]public class FragmentLeft extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_left, container); } }
这里注意继承的
android.app.Fragment而不是
android.support.v4.app.Fragment
activity_main.xml
[code]<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.fragments.FragmentLeft" android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"/> <fragment android:name="com.example.fragments.FragmentRight" android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"/> </LinearLayout>
注意:这里用
android:name="com.example.fragments.FragmentLeft"将两个加载fragment的java文件关联起来
MainActivity.java
[code]package com.example.fragments; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
结果:
onAttach方法:Fragment和Activity建立关联的时候调用。
onCreateView方法:为Fragment加载布局时调用。
onActivityCreated方法:当Activity中的onCreate方法执行完后调用。
onDestroyView方法:Fragment中的布局被移除时调用。
onDetach方法:Fragment和Activity解除关联的时候调用。
Fragment真正的强大之处在于可以动态地添加到Activity当中
1:activity_main.xml用来存放手机布局
2:MenuFragment.java(内容第四步)
3:新建layout-large文件夹下的activity_main.xml,将平板分成左右两侧,左侧是fragment右侧是FrameLayout
[code]*3.1:表明当前的Activity想加载activity_main这个布局文件。而Android系统又会根据当前的运行环境判断程序是否运行在大屏幕设备上,如果运行在大屏幕设备上,就加载layout-large目录下的activity_main.xml,否则就默认加载layout目录下的activity_main.xml。*
4:MenuFragment.java
4.1 需要ListView/adapter/data
4.2 新建sound_fragment.xml
4.3 新建SoundFragment.java
4.4 新建display_fragment.xml
4.5 新建DisplayFragment.java
4.6 新建sound_acitivity.xml和SoundActivity.java
4.7新建display_acitivity.xml和DisplayActivity.java
AndroidManifest.xml
[code]<activity android:name="com.example.fragment.DisplayActivity"> </activity> <activity android:name="com.example.fragment.SoundActivity"> </activity>
出错:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first
解决方案:
View view=inflater.inflate(R.layout.display_fragment, container,**false**);
结果:
源码分享:
这里写链接内容
相关文章推荐
- Android中线程的使用
- android imageview
- Android蓝牙通信
- iOS 与 Android 生命周期对比
- Android Wifi方法大全
- Android内核剖析
- Android内核剖析
- android异常:uncaught exception
- Android应用性能优化
- Android应用性能优化
- Android 通过应用程序来设置系统的日期和时间中的
- 从Android源码剖析Intent查询匹配的实现
- Gradle 1:为什么要用Gradle
- android应用程序中获取view的位置
- 打造高质量Android应用:Android开发必知的50个诀窍
- androidのXlist框架上拉下拉刷新案例
- Android Studio关联SVN相关操作
- Android中读取图片EXIF元数据之metadata-extractor的使用
- 看google怎样将android回归在自己的保护伞之下
- android异常:dialog窗口句柄泄露