fragment学两个demo就会用(一)

MainActivity

package com.example.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

public class MainActivity extends FragmentActivity {
	public static String temp="weesdf";
	public static String getTemp() {
		return temp;
	}
	public static void setTemp(String temp) {
		MainActivity.temp = temp;
	}
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

//		Intent intent=new Intent(MainActivity.this,);
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		FirstFragment first = new FirstFragment();
                //如果用add()的话会出现不完全覆盖效果
		ft.replace(R.id.container, first);
		ft.commit();
	}
	//切换到第二个fragment
	public void onClick_btn(View v){
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		SecondFragment second = new SecondFragment();
		ft.replace(R.id.container, second);
		ft.commit();
	}
}

Activity_Second

package com.example.demo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class Activity_Second extends FragmentActivity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		
	}
}

FirstFragment

package com.example.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class FirstFragment extends Fragment {

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_first, container, false);
		Button bt_first = (Button) view.findViewById(R.id.bt_first);
		bt_first.setText(MainActivity.temp);
		bt_first.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent=new Intent(getActivity(),Activity_Second.class);
				startActivity(intent);
			}
		});
		return view;
	}
}
SecondFragment

package com.example.demo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondFragment extends Fragment {

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.fragment_second, container, false);
		Button bt_second = (Button) view.findViewById(R.id.bt_second);
		bt_second.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				FragmentTransaction ft = getFragmentManager().beginTransaction();
				ft.setCustomAnimations(R.animator.move_in, R.animator.move_out);
				FirstFragment first = new FirstFragment();
				ft.replace(R.id.container, first);
				ft.commit();
			}
		});
		return view;
	}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick_btn"
        android:text="@string/app_name" /> 

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="200dp" />

</LinearLayout>

fragmentfirst.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#cba" >

    <TextView
        android:id="@+id/tv_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="这是第一个Fragment"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/bt_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="点我进入下一个" />

</LinearLayout>

fragmentsecond.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="#abc"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="这是第二个Fragment"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/bt_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="点我回到第一个" />

</LinearLayout>

已在资源中上传demo源码http://download.csdn.net/detail/djun100/5974251

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值