之前我演示了简单的如何静态加载fragment,但是这种方式在日常的开发中,其实使用的并不是很多,很多情况下,我们都是需要动态的进行fragment的更改,所以这次我就来演示一下简单的动态加载fragment的方法,也就是使用add()、remove()、replace()这三个方法,话不多说,我们来演示一下代码:
首先是在前一章的基础上,新增了一些文件,见我的目录结构:
其中,我在MainActivity的布局文件中,放了两个按钮,实现了跳转,分别进去的是DemoFragmentActivity和ManageFragmentActivity,这里我们主要说明一下ManageFragmentActivity的使用方法。
MainActivity中的java代码:
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void fragmentDemo(View view){
startActivity(new Intent(this,DemoFragmentActivity.class));
}
public void fragmentManage(View view){
startActivity(new Intent(this,ManageFragmentActivity.class));
}
}
MainActivity的布局文件xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.czj01.fragmentdemo.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fragmentDemo"
android:text="Fragment基本使用方法"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fragmentManage"
android:text="Fragment基本管理方法"/>
</LinearLayout>
ManageFragmentActivity的java代码:
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class ManageFragmentActivity extends AppCompatActivity {
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_fragment);
}
public void add(View view){
fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentTest fragmentTest = new FragmentTest();
fragmentTransaction.add(R.id.fl_container,fragmentTest,"fragment");
fragmentTransaction.commit();
}
public void remove(View view){
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentManager.findFragmentByTag("fragment"));
fragmentTransaction.commit();
}
public void replace(View view){
// FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.remove(fragmentManager.findFragmentByTag("fragment"));
// fragmentManager = getSupportFragmentManager();
// AnotherFragmentTest anotherFragmentTest = new AnotherFragmentTest();
// fragmentTransaction.add(R.id.fl_container,anotherFragmentTest,"anotherFragment");
// fragmentTransaction.commit();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AnotherFragmentTest anotherFragmentTest = new AnotherFragmentTest();
fragmentTransaction.replace(R.id.fl_container,anotherFragmentTest);
fragmentTransaction.commit();
}
public void onBackPressed(){
finish();
}
}
其实,动态加载fragment,主要是需要使用FragmentManage这个类,先创建一个这个累的对象,因为我这里使用的fragment都是supportv4里面的,所以我要使用
getSupportFragmentManager();如何使用的是app下面的,那就直接getFragmentManage()就可以了。
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_manage_fragment" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.czj01.fragmentdemo.ManageFragmentActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="add" android:text="add_fragment"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="remove" android:text="remove_fragment"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="replace" android:text="replace_fragment"/> <FrameLayout android:id="@+id/fl_container" android:layout_width="wrap_content" android:layout_height="wrap_content"> </FrameLayout> </LinearLayout>
fragment 和anotherfragment 的java代码:import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by czj01 on 2017/5/8. */ public class FragmentTest extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_list, container, false); } }
两个布局文件fragment_list和another_fragment_list布局文件:import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by czj01 on 2017/5/9. */ public class AnotherFragmentTest extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.another_fragment_list, container, false); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#8E8E8E" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="100dp" android:layout_height="100dp" android:text="Fragment"/> </LinearLayout>
?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#892525" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="100dp" android:layout_height="100dp" android:text="another_fragment"/> </LinearLayout>