android开发之Fragment加载到一个Activity中

Fragments 是android3.0以后添加的。主要是为了方便android平板端的开发。方便适应不同大小的屏幕。此代码是为了最简单的Fragment的使用,往一个Activity中添加Fragment,主要涉及的知识点有:1、Fragment类的创建,2、Fragment的添加3、无UI的 Fragment的添加,根据Tag找回Fragment

 
    Fragment对应的Xml布局文件,
 
<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:orientation="vertical"
    tools:context=".MainActivity"
    android:weightSum="10" >
 
    <Button 
        android:id="@+id/bt"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:text="添加一个Fragment"
        android:layout_weight="2"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:text="将Fragment加载到Activity中,此Fragment中没有UI,即不需要实现onCreateView方法,可以当做此Activity的背景色" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"      
        android:text="根据Fragment的Tag找到Fragment" />
    <LinearLayout 
        android:id="@+id/lv_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:background="#123456"
        android:orientation="horizontal">
 
    </LinearLayout>
</LinearLayout>
 
1、Fragment的创建
 
package com.example.fragment1;
 
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
/**
 * @author sea
 * 创建一个Fragment至少要实现三个生命周期函数onCreate,onCreateView,onPause
 *
 */
public class MyFragment extends Fragment {
 
    /* 
     * 初始化Fragment,实例化在Fragment中的成员变量
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
 
    /* 
     * 给Fragment 加载UI的布局,返回Fragment布局文件对应的东东
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment,container, false);
 
        return view;
    }
 
    /* 
     * 当用户离开此Fragment时调用
     */
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
 
}
 
2、无UI的Fragment的创建
 
package com.example.fragment1;
 
import android.app.Fragment;
import android.os.Bundle;
 
 
public class MyFragment2 extends Fragment {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
 
    }
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
}
 
3、Fragment的添加到Activity中
 
package com.example.fragment1;
 
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
/**
 * @author sea
 * 将fragment加载到一个Activity中
 * 方法一代码:如此例子主要是用到FragmentTransaction类
 * 方法二:直接在xml文件中添加
 *
 */
public class MainActivity extends Activity {
 
    private Button button;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.bt);
        button.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                 //找到FragmentTransaction
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.
                        beginTransaction();
                MyFragment fragment = new MyFragment();
                //加到Activity中
                fragmentTransaction.add(R.id.lv_fragment_container,fragment);
                //加到后台堆栈中,有下一句代码的话,点击返回按钮是退到Activity界面,没有的话,直接退出Activity
                //后面的参数是此Fragment的Tag。相当于id
                fragmentTransaction.addToBackStack("fragment1");
                //记住提交
                fragmentTransaction.commit();
 
            }
        });
    }
}

android开发之Fragment加载到一个Activity中,布布扣,bubuko.com

android开发之Fragment加载到一个Activity中

上一篇:Android开发资源推荐第2季


下一篇:【Android 初学】11、关于Android当中的线程(初级)