Fragment之間的切換


利用Fragment實現界面跳轉的功能,完成效果圖如圖:

主界面代碼如下:

<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.edu.niit.pageredirecting.MainActivit<LinearLayo android:id="@+id/show"
 android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<Button
android:id="@+id/bt_show"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/bt_show"
android:layout_gravity="center_horizontal"
android:gravity="center"
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"
tools:context="com.example.a15083.fragmentandactivity.FirstFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"

android:gravity="center"
android:textSize="25sp"
android:text="@string/first_fragment" />
</FrameLayout>
第二個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"
tools:context="com.example.a15083.fragmentandactivity.SecondFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:gravity="center"
android:text="@string/second_fragment"
android:textSize="25sp" />
</FrameLayout>
Java部分代碼如下:
package cn.edu.niit.pageredirecting;

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 FragmentActivity extends AppCompatActivity implements View.OnClickListener{
FirstFragment firstFragment;
SecondFragment secondFragment;
private boolean a=true; //a的作用就是來判斷第二次點擊按鈕時是否切換fragment
boolean e=false; //e的作用是用來在第一個fragment點擊返回時就執行退出程序操作

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

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
firstFragment = new FirstFragment();
transaction.add(R.id.show,firstFragment);
transaction.commit();
}

@Override
public void onClick(View view) {
if(view.getId()==R.id.bt_show){
e=true; //將e的值設置為true,使在點擊返回鍵時能夠切換fragment
if(a){
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (secondFragment == null){
secondFragment = new SecondFragment();
transaction.replace(R.id.show,secondFragment);
transaction.commit();
a=false; //設置a為false,使在第二次以后點擊按鈕能夠彈出提示
} else{
transaction.replace(R.id.show,secondFragment);
transaction.commit();
a=false; //設置a為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&&e){
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
a=true; //設置a為true,以便是點擊按鈕時能再次切換fragment
e=false; //設置e為false,使在第一個fragment時能夠退出
transaction.replace(R.id.show,firstFragment);
transaction.commit();
return false;
} else {
finish();
}
return super.onKeyDown(keyCode, event);
}
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM