---
这个就会个布局,实现跳转都是看同学的。activity的布局 还有两个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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context="com.example.administrator.tiaozhuan.MainActivity"><LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:id="@+id/show"></LinearLayout>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show nextpage" android:layout_gravity="center" android:layout_marginTop="20dp" android:id="@+id/btnshow" android:onClick="onClick"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:orientation="vertical" android:id="@+id/txtshow2"> </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.administrator.tiaozhuan.FirstFragment"><TextView
android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="This is first fragment!"/> </FrameLayout></FrameLayout>
第二个:<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.administrator.tiaozhuan.SecondFragment"><TextView
android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="This is second fragment!"/> </FrameLayout> </FrameLayout>下面是主界面java代码 这些都是看同学的修改的。。public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Fragment firstFragment;private Fragment secondFragment;private boolean skip = true;private boolean exit = false; @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); blankFragment = new firstFragment(); FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.main, blankFragment); transaction.commit();}public void onClick(View view) {
switch (view.getId()) { case R.id.btn_main: 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.main, secondFragment); transaction.commit(); skip = false; } else { transaction.replace(R.id.main, 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.main, blankFragment); transaction.commit(); return false; } else { finish(); } return super.onKeyDown(keyCode, event); }}