Android作业(Fragment的转换)

【Android作业(Fragment的转换)】知识养成了思想,思想同时又在融化知识。这篇文章主要讲述Android作业:Fragment的转换相关的知识,希望能为你提供帮助。
要实现Fragment的转换,需要两个Fragment。
首先是xml布局

Android作业(Fragment的转换)

文章图片
Android作业(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: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
Android作业(Fragment的转换)

文章图片
Android作业(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
Android作业(Fragment的转换)

文章图片
Android作业(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最后
Android作业(Fragment的转换)

文章图片
Android作业(Fragment的转换)

文章图片
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(); returnfalse; } else { finish(); } return super.onKeyDown(keyCode, event); }}

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

    推荐阅读