Android之Fragment
来源:互联网 发布:淘宝原道电商讲师东风 编辑:程序博客网 时间:2024/05/16 09:13
转载请标明出处: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; }}
0 0
- android文档之Fragment
- Android昆仑镜之Fragment
- Android之Fragment
- android之Fragment
- Android学习之 Fragment
- android之Fragment
- android之Fragment
- Android之Fragment
- android之Fragment
- Android入门之Fragment
- Android之fragment初探
- Android之FragMent
- Android之Fragment通信
- Android之Fragment学习心得
- Android之Fragment优点
- android之Fragment
- Android之Fragment
- android之Fragment生命周期
- Mybatis学习与总结
- Opencv简单编程--Mat图像作为函数参数
- 【BZOJ1101】【POI2007】Zap
- 关于TCP三次握手链接,四次握手关闭
- ed2k 网络中搜索资源并选择资源下载的分析及eMule源码梳理
- Android之Fragment
- CodeForces 580D Kefa and Dishes(DP)
- 探究JVM——运行时数据区
- 内核学习总结(Linux)
- sqoop数据迁移工具
- c++ operator的学习
- Ubuntu下面提示电缆未插入不能上网的解决办法
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Aizu 0121 Seven Puzzle