Quokka

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Android四月fragment跳转

要求:利用Fragment实现界面跳转的功能,完成效果如图:

首先此次作业需要三个界面一个activity两个fragment。两个fragment可以手动创建也可以一键向导创建。来看看布局
activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:gravity="center"
    tools:context="com.example.wdh.fragmentwork.MainActivity">

    <LinearLayout
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="261dp"
        android:orientation="vertical">

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal">


        <Button
            android:id="@+id/btnshow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:onClick="onClick"
            android:text="@string/btnshow" />


    </LinearLayout>

</LinearLayout>


Firstfragment


<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"
    tools:context="com.example.wdh.fragmentwork.FirstFragment">

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

</FrameLayout>

secondfragment

<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"
    tools:context="com.example.wdh.fragmentwork.ScondFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/Second" />

</FrameLayout>

(中间手误单词打错了 O.o 真是白瞎了)
虽然是三个界面的跳转,但是出来activity之外的其他两个fragment界面的java功能代码并没有添加什么。

package com.example.wdh.fragmentwork;

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.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    FirstFragment firstFragment;
    ScondFragment scondFragment;
    private boolean Y = true;
    private boolean N = 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.show, firstFragment);
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnshow) ;
        Y = true;
        if (N) {
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            if (scondFragment == null) {
                scondFragment = new ScondFragment();
                transaction.replace(R.id.show, scondFragment);
                transaction.commit();
                Y = false;
            } else {
                transaction.replace(R.id.show, scondFragment);
                transaction.commit();
                Y = false;
            }
        } else {
            Toast.makeText(this, "This is second fragment", Toast.LENGTH_SHORT).show();
        }
    }
}
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && N) {
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            Y = true;
            N = false;
            transaction.replace(R.id.show, firstFragment);
            transaction.commit();
            return false;
        } else {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }


END

posted on 2017-04-18 12:01  Quokka  阅读(125)  评论(0编辑  收藏  举报