首页 > 代码库 > Android作业:Fragment的转换

Android作业:Fragment的转换

要实现Fragment的转换,需要两个Fragment。

首先是xml布局

技术分享
<?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>
View Code

接下来是第一个Fragment

技术分享
<?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"
tools:context="com.example.wdh.fragmentwork.FirstFragment">

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

</FrameLayout>
View Code

然后是第二个Fragment

技术分享
<?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"
tools:context="com.example.wdh.fragmentwork.ScondFragment">



<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    style="@style/Scond"/>



</FrameLayout>
View Code

最后

技术分享
package com.example.kimdemon.fragment;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

First Fragment1;
Second Fragment2;
private boolean qiehuan = true;
private boolean tuichu = false;




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


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

Fragment1 = new First();
    transaction.add(R.id.yf_show,Fragment1);
transaction.commit();

}


@Override
public void onClick(View view) {
    if(view.getId() == R.id.yf_btn1){
        tuichu = true;
        if(qiehuan){
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            if (Fragment2 == null){
                Fragment2 = new fragment2();
                transaction.replace(R.id.yf_show,Fragment2);
                transaction.commit();
                qiehuan = false;
            } else{
                transaction.replace(R.id.yf_show,Fragment2);
                transaction.commit();
                qiehuan = 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&&tuichu){

FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
qiehuan = true;
        tuichu = false;
transaction.replace(R.id.yf_show,Fragment1);
        transaction.commit();
return  false;
    } else {
        finish();
    }
    return super.onKeyDown(keyCode, event);


}

}
View Code

这次作业感觉一直在云里雾里,整个人都处于懵逼状态...

END

Android作业:Fragment的转换