转载请标明出处:http://blog.csdn.net/wu_wxc/article/details/51285961

本文出自【吴孝城的CSDN博客】

在Android中实现页面跳转有Activity,但用较为重量级的Activity速度会比Fragment慢点

下面用Fragment来实现页面跳转


activity_main.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout 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:id="@+id/container"    tools:context="cn.wuxiaocheng.fragment.MainActivity"/>

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/btnOne"        android:text="one"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>
fragment_two.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#3c4f5e">    <Button        android:id="@+id/btnWwo"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Two" /></LinearLayout>
MainActivity.java

package cn.wuxiaocheng.fragment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        if (savedInstanceState == null) {            getSupportFragmentManager().beginTransaction()                    .add(R.id.container, new OneFragment())                    .commit();        }    }}

OneFragment.java

package cn.wuxiaocheng.fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class OneFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,                             @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_one, container, false);        view.findViewById(R.id.btnOne).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                getFragmentManager().beginTransaction()                        //添加返回键功能                        .addToBackStack(null)                        .replace(R.id.container, new TwoFragment())                        .commit();            }        });        return view;    }}
TwoFragment.java

package cn.wuxiaocheng.fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class TwoFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,                             @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_two, container, false);        view.findViewById(R.id.btnWwo).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                getFragmentManager().popBackStack();            }        });        return view;    }}










更多相关文章

  1. Android实现RecyclerView显示json数据并点击跳转页面
  2. 【React Native开发】 - react-native-swiper的使用
  3. Android(安卓)锁屏状态下拉起某个页面
  4. Android(安卓)- 小问题 - ViewPager onPageChangeListener
  5. Android(安卓)WebView H5页面 input type =“file”解决方法
  6. Activity页面状态保存 持久化
  7. Android(安卓)之 TabHost 分页面板控件
  8. Toolbar+TabLayout+ViewPager达成Android最优导航栏
  9. Android(安卓)ViewPager轮播图

随机推荐

  1. Android精讲--界面编程3
  2. Android移动应用开发:众筹APP
  3. 远程服务使用AIDL通信
  4. 如何在Activity调用surfaceView实现透明
  5. Android核心模块内容概述
  6. 教你如何开关Android的APN网络
  7. Android线程之消息机制(Handler、Message
  8. 你不知道一些神奇Android(安卓)Api-比如文
  9. Android线程间通信的Message机制
  10. Unity与Android权限设置