我是靠谱客的博主 粗犷纸飞机,最近开发中收集的这篇文章主要介绍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

最后

以上就是粗犷纸飞机为你收集整理的fragment学两个demo就会用(一)的全部内容,希望文章能够帮你解决fragment学两个demo就会用(一)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部