Fragment实现界面跳转

布局文件非常简单

这是主布局


<LinearLayout
    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"
    android:orientation="vertical"
    tools:context="com.example.bwqpzy.fragment1.MainActivity">
    <LinearLayout
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="1">

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:layout_weight="1">
        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/next"
            android:onClick="onClick"/>





    </LinearLayout>

</LinearLayout>

其中Fragment的两个布局都一样

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


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/first"/>

</FrameLayout>




实现界面跳转来显示界面
主要通过fragment来实现

package com.example.bwqpzy.fragment1;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.bwqpzy.fragment1.FirstFragment;
import com.example.bwqpzy.fragment1.SecondFragment;

public class MainActivity extends AppCompatActivity {
    FirstFragment firstFragment;
    SecondFragment secondFragment;
    private boolean qh = true;
    private boolean tc= false;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        firstFragment = new FirstFragment();
        transaction.add(R.id.next,firstFragment);
        transaction.commit();

    }



    public void onClick(View view) {
        if(view.getId() == R.id.next){
            tc = true;
            if(qh){
                FragmentManager manager = getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                if (secondFragment == null){
                    secondFragment = new SecondFragment();
                    transaction.replace(R.id.next,secondFragment);
                    transaction.commit();
                    qh = false;
                } else{
                    transaction.replace(R.id.next,secondFragment);
                    transaction.commit();
                    qh = false;
                }
            }else{
                Toast.makeText(this,"This is second fragment",Toast.LENGTH_SHORT).show();
            }

        }
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(event.getKeyCode()== KeyEvent.KEYCODE_BACK&&tc){

            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            qh = true;
            tc = false;
            transaction.replace(R.id.next,firstFragment);
            transaction.commit();
            return  false;
        } else {
            finish();
        }
        return super.onKeyDown(keyCode, event);


    }


}


这是实现的关键

private boolean qh = true;
private boolean tc= false;

这次作业复习了老师所讲的Fragment内容,整个界面功能实现就如此

posted @ 2017-04-18 20:35  berice  阅读(3291)  评论(0编辑  收藏  举报