1. 程式人生 > >Android 介面回撥實現Fragment的跳轉

Android 介面回撥實現Fragment的跳轉

---------------------MainActivity-------------------

package com.example.earl.fragmentinterfacejump;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

/**
 * 通過介面的方式實現fragment之間的跳轉操作
 */
public class MainActivity extends Activity implements TestFragmentA.AbtnClikListener, TestFragmentB.BbtnonclickListener {
    private TestFragmentA fragmentA;
    private TestFragmentB fragmentB;
    private TestFragmentC fragmentC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentA = new TestFragmentA();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.content, fragmentA, "A");
        ft.commit();
    }

    /**
     * 實現TestFragmentA定義的介面
     */
    @Override
    public void onAbtnclick() {
        if (fragmentB == null) {
            fragmentB = new TestFragmentB();
            fragmentB.setBbtnonclickListener(this);

        }
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content, fragmentB, "B");
        /**
         * 將當前的事務新增到了回退棧,所以TestFragmentA例項不會被銷燬,但是檢視層次依然會被銷燬
         * 在TestFragmentB介面中點選返回鍵可以返回TestFragmentA,而不是退出Activity
         */
        ft.addToBackStack(null);
        ft.commit();

    }
    /**
     * 實現TestFragmentB定義的介面
     */
    @Override
    public void onBtnclick() {
        if (fragmentC == null) {
            fragmentC = new TestFragmentC();
        }
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content, fragmentC, "C");
        ft.addToBackStack(null);
        ft.commit();
    }
}

----------------------TestFragmentA------------------

package com.example.earl.fragmentinterfacejump;

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

/**
 * 跳轉方式一
 */

public class TestFragmentA extends Fragment {
    Button toB;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e("TestFragment", "onCreateView");
        View view = inflater.inflate(R.layout.layout_fragmenta, container, false);
        toB = (Button) view.findViewById(R.id.toB);
        toB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getActivity() instanceof AbtnClikListener){
                    ((AbtnClikListener) getActivity()).onAbtnclick();
                }
            }
        });
        return view;
    }

    public interface AbtnClikListener {
        void onAbtnclick();
    }
}

--------------------TestFragmentB--------------------

package com.example.earl.fragmentinterfacejump;

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

/**
 * 跳轉方式二
 */

public class TestFragmentB extends Fragment implements View.OnClickListener{

    Button toC;
    private BbtnonclickListener bbtnonclickListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e("TestFragment", "onCreateView");
        View view = inflater.inflate(R.layout.layout_fragmentb, container, false);
        toC = (Button) view.findViewById(R.id.toC);
        toC.setOnClickListener(this);
        return view;
    }

    public void setBbtnonclickListener(BbtnonclickListener bbtnonclickListener) {
        this.bbtnonclickListener = bbtnonclickListener;
    }

    @Override
    public void onClick(View v) {
        if (bbtnonclickListener != null) {
            bbtnonclickListener.onBtnclick();
        }
    }

    public interface BbtnonclickListener {
        void onBtnclick();
    }
}

-------------------TestFragmentC------------------

package com.example.earl.fragmentinterfacejump;

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


/**
 * Created by Administrator on 2017/8/4.
 */

public class TestFragmentC extends Fragment {
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e("TestFragment", "onCreateView");
        return inflater.inflate(R.layout.layout_fragmentc, container, false);
    }

}

------------------activity_main.xml----------------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</android.support.constraint.ConstraintLayout>

-------------------layout_fragmenta.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:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是頁面A" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/toB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="toB" />
</LinearLayout>

-------------------layout_fragmentb.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:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是頁面B" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/toC"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="toC" />
</LinearLayout>

-------------------layout_fragmentc.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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是頁面C" />


</LinearLayout>