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_fragment"
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"
android:orientation="vertical"
tools:context="com.example.dell.fragmentapplication.FragmentActivity">
<LinearLayout
android:id="@+id/begin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/diyiduan"
android:layout_gravity="center"
android:layout_marginTop="200dp"
android:textSize="30sp"/>
<Button
android:id="@+id/nextpage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/nextpage"
android:onClick="onClick"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
二、Fragment页面布局
1、 <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"
android:orientation="vertical"
tools:context="com.example.dell.fragmentapplication.firstFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/diyiduan"
android:layout_gravity="center"
android:layout_marginTop="200dp"
android:textSize="30sp"/>
<Button
android:id="@+id/nextpage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/nextpage"
android:onClick="onClick"/>
</LinearLayout>
</FrameLayout>
2、<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.dell.fragmentapplication.secondFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dierduan"
android:layout_gravity="center"
android:layout_marginTop="200dp"
android:textSize="30sp"/>
<Button
android:id="@+id/nextpage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/nextpage"
android:onClick="onClick"/>
</LinearLayout>
</FrameLayout>
三、JAVA功能的具体实现
package com.example.dell.fragmentapplication;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class FragmentActivity extends AppCompatActivity implements View.OnClickListener {
private Fragment firstFragment;
private Fragment secondFragment;
private boolean skip = true;
private boolean exit = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
firstFragment = new BlankFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.begin, firstFragment);
transaction.commit();
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.nextpage:
jump();
break;
}
}
private void jump() {
boolean exit = true;
if (skip) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (secondFragment == null) {
secondFragment = new SecondFragment();
transaction.replace(R.id.begin, secondFragment);
transaction.commit();
skip = false;
} else {
transaction.replace(R.id.begin, secondFragment);
transaction.commit();
skip = false;
}
} else {
Toast.makeText(this, "This is second fragment", Toast.LENGTH_LONG).show();
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && exit) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
skip = true;
exit = false;
transaction.replace(R.id.begin, firstFragment);
transaction.commit();
return false;
} else {
finish();
}
return super.onKeyDown(keyCode, event);
}
}
package com.example.dell.fragmentapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class secondFragment extends Fragment {
public secondFragment() {
}
@Override
public class SecondFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_second,container,false);
return view;
}
}