首页 > 代码库 > Fragment间的替换和后退

Fragment间的替换和后退

一、新建项目

 

二、创建第一个Fragment

   1.layout文件

技术分享
<?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:orientation="vertical">

    <Button
        android:id="@+id/btn_show_another_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示另一个Fragment" />

</LinearLayout>
fragment_main

   2.class文件

技术分享
package com.example.fragmentdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by 袁磊 on 2017/2/7.
 * Fragment生命周期
 */
public class PlaceHolderFragment extends Fragment {
    private static final String TAG = "PlaceHolderFragment";

    public PlaceHolderFragment() {
    }

    private Button btnShowAnotherFragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, " a onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        Log.d(TAG, "a  onCreateView");
        btnShowAnotherFragment = (Button) rootView.findViewById(R.id.btn_show_another_fragment);
        btnShowAnotherFragment.setOnClickListener(myOnClickListener);
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "a  onPause");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "a  onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "a  onDestroy");
    }

    private View.OnClickListener myOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_show_another_fragment:
                    getFragmentManager().beginTransaction()
                            .addToBackStack(null)//添加到后退栈,为了支持后退按钮
                            .replace(R.id.container, new AnotherFragment())//替换
                            .commit();//提交
                    break;
            }

        }
    };
}
PlaceHolderFragment

 

三、创建第二个Fragment

   1.layout文件

技术分享
<?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:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是另一个Fragment" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="后退" />

</LinearLayout>
fragment_another

   2.class文件

技术分享
package com.example.fragmentdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by 袁磊 on 2017/2/7.
 * Fragment常用生命周期
 */
public class AnotherFragment extends Fragment {


    private static final String TAG = "AnotherFragment";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_another, container, false);
        Log.d(TAG, "onCreateView");
        root.findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();//后退
            }
        });
        return root;
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}
AnotherFragment

 

四、使用

   1.activity_main中

技术分享
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
activity_main

   2.MainActivity中

技术分享
package com.example.fragmentdemo;

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 PlaceHolderFragment())//添加
                    .commit();//提交
        }
    }
}
MainActivity

 

五、运行效果

技术分享   技术分享

 

 

   

 

Fragment间的替换和后退