您的位置:首页 > 移动开发 > Android开发

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

[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**);


结果:






源码分享:

这里写链接内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: