开启事务

MainActivity。class:

package com.bwei.day_11FragmentTransaction;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private FragmentManager fm;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button weixin = (Button) findViewById(R.id.weixin);

        weixin.setOnClickListener(this);
        Button address_book = (Button) findViewById(R.id.address_book);
        address_book.setOnClickListener(this);

        fm = getSupportFragmentManager();
        
        transaction = fm.beginTransaction();
        transaction.add(R.id.frameLayout, new Weixin(), "weixin").addToBackStack(null).commit();
        

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.weixin:
            transaction = fm.beginTransaction();
            transaction.add(R.id.frameLayout, new Weixin(), "weixin").addToBackStack(null).commit();
            break;
        case R.id.address_book:
            transaction = fm.beginTransaction();
            transaction.add(R.id.frameLayout, new AddressBook(), "addressBook").addToBackStack(null)
                    .commit();

            break;

        }
    }

}

AddressBook.class:

/**
 * 
 */
package com.bwei.day_11FragmentTransaction;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author WJL
 * 
 */
public class AddressBook extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.address_book, null);
    }

}

Weixin.class:

/**
 * 
 */
package com.bwei.day_11FragmentTransaction;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author WJL
 * 
 */
public class Weixin extends Fragment {
    /*
     * (non-Javadoc)
     * 
     * @see
     * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     * android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.weixin, null);
    }

}

页面:

<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" >

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </FrameLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="7" >

        <Button
            android:id="@+id/weixin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="微信" />

        <Button
            android:id="@+id/address_book"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="通讯录" />
    </LinearLayout>

</LinearLayout>

其中的fraglayout,可以替换任何布局,因为这就是事务所在的地方,就是来替换它的,其余两个页面,全靠自己定义,随着心情来的

posted @ 2016-04-28 11:05  151  阅读(311)  评论(0编辑  收藏  举报