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
原创粉丝点击