我个人在对于Fragment不熟悉的情况下,会认为使用普通的Activity方法跳转会更加容易得多,以为理解起来没那么困难。
而使用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:orientation="vertical"
tools:context="com.example.kimdemon.fragment.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">
<Button
android:id="@+id/yf_btn1"
android:layout_width="250dp"
android:layout_height="wrap_content"
style="@style/button"
android:layout_gravity="center_horizontal"
android:layout_marginTop="80dp"
android:onClick="onClick"/>
</LinearLayout>
<LinearLayout
android:id="@+id/yf_show"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
这是新建的第一个关于Fragment的xml
<?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.kimdemon.fragment.fragment1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
style="@style/fragment1"/>
</FrameLayout>
这是新建的第二个关于Fragment的xml
<?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.kimdemon.fragment.fragment2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
style="@style/fragment2"/>
</FrameLayout>
这是原生的类中的代码
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{
fragment1 Fragment1;
fragment2 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 fragment1();
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);
}
}
其中有一个地方必须注意,如果关于这两个布局的类是由你自己手动新建的话,则必须写下如下的代码
package com.example.kimdemon.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragmengt1,container,false);
return view;
}
}
运行结果如图
而至于新建布局的类会不会自动生成我就不太清楚了,反正我是没有生成,所以只能自己写了