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就会用之一
Android两个Fragment之间通信Demo
简单的实现activity中两个fragment切换的demo。
fragment延时加载demo,展示了fragment如何延时加载的,里面有一个AbsFragemt的类,以及两个展示的Activity
TabLayout+ViewPager+Fragment实现分页滑动效果Demo
Android Fragment 简单易懂的两个小Demo,适合初学者使用。
android fragment demo 动态加载例子,两个fragment 动态切换。
分两个fragment显示的功能设置DEMO
( 1 ) android.app.Fragment:兼容的最低... 要是用android.support.v4.app.Fragment,需要继承FragmentActivity,使用getSupportFragmentManager,这两个方法都是为了 得到fragmentManager,名字一样但是包名不一样。
本Demo的两个Fragment分别包含listview和gridview,左右滑动用GestureDetector手势监听;由于listview,gridview的滑动事件和左右滑动事件冲突,所以自定义listview和gridview让其优先执行自定义的左右滑动事件。
对android developer中的fragment做了一些改动。并且分别尝试用Transcation中的hide,show方法和replace方法用来对两个fragment进行切换操作。并在MainActivity中对他们后面需要执行的步骤做了简单的说明。
该demo是在一个activity中嵌套两个fragment,利用SlidingMenu控制左边的fragment展示滑动效果,然后在右边的fragment中又嵌套四个fragment,配合RadioButton实现fragment的切换。
Android主页面 多层Fragment嵌套(Fragment+ViewPager)滑动,一个适合学习的好的demo,给学习android开发的新手朋友传播数来
两个实用的ViewPager+Frament的选项卡tab的使用,我自己在用,为了大众,也为了自己以后找得方便,分享出来。
让你实现一个完全由Fragment组成的应用,不同界面的切换全部用Fragment完成,整个程序只有一个activity,Fragment的表现看起来就像一个单独的Activity,demo中演示了很多中切换效果,有些效果如果使用Activity很难...
前言 从库的名字就能看出来,这库是用来存放我平时学习时候的小练习用的,这些小demo我会在下面...两个css小效果,掘金学来的 查看代码 圣杯和双飞翼布局 做IFE的时候遇到的,挺经典的两种布局就整理了一波。两种布局达
一个简单的仿知乎侧滑的小DEMO,分为两个包,一个Fragment,一个关于侧滑控制的包
本demo主要是用Fragment实现TabHost的功能,是完全的实现,和之前两个demo相比,这个是完全从不同的角度来实现选项卡的功能,可以看看这三篇文章,对这个demo的理解会更深刻一些...
这是一个Demo主要是对Fragment(碎片)的使用,以及动态添加数据,一个界面分为两个模块进行说明,愿能带给给大家一定的帮助。
片段演示3 (Android)练习使用Bundle对象将数据从Activity传递到片段 有两种方法可以将数据从活动...这是一个使用Bundle对象传递数据的实践项目。 Bundle对象通常用于传递原始数据类型(String,int,float,long)