1.效果图
为了动态使用Fragment,我们修改一下Actvity的布局文件,中间使用一个FrameLayout,下面添加四个按钮
打开界面,默认显示item1对应的fragment,点击item2,主界面中的内容变为item2对应的fragment的内容。这里用到的就是动态使用fragment。
2.主页面布局
activity_test_dynamic_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f4f9"
android:orientation="vertical"
tools:context="xzy.com.myfragmentdemo.TestDynamicFragmentActivity">
<fragment
android:id="@+id/id_fragment_title"
android:name="xzy.com.myfragmentdemo.titleFragment"
android:layout_width="match_parent"
android:layout_height="45dp"
/>
<include
android:id="@+id/bottom_item_layout"
layout="@layout/bottom_menu_layout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="3dp"
/>
<FrameLayout
android:id="@+id/id_frame_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_item_layout"
android:layout_below="@id/id_fragment_title"
/>
</RelativeLayout>
引入的底部按钮的布局文件bottom_menu_layout.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:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
tools:context="xzy.com.myfragmentdemo.MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_item1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_item2"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_item3"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item3"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_item4"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="item4"
/>
</LinearLayout>
</LinearLayout>
通过点击底部的菜单,切换主界面中显示的不同内容,主界面代码为:
package xzy.com.myfragmentdemo;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class TestDynamicFragmentActivity extends Activity implements View.OnClickListener {
private Item1Fragment mItem1Fragment;
private Item2Fragment mItem2Fragment;
private ImageView mImageViewItem1;
private ImageView mImageViewItem2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_dynamic_fragment);
mImageViewItem1 = (ImageView) findViewById(R.id.iv_item1);
mImageViewItem2 = (ImageView) findViewById(R.id.iv_item2);
mImageViewItem1.setOnClickListener(this);
mImageViewItem2.setOnClickListener(this);
setDefaultFragment();
}
public void setDefaultFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
mItem1Fragment = new Item1Fragment();
fragmentTransaction.replace(R.id.id_frame_content, mItem1Fragment);
fragmentTransaction.commit();
}
public static void startActivity(Context context) {
Intent intent = new Intent(context, TestDynamicFragmentActivity.class);
context.startActivity(intent);
}
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
switch (v.getId()) {
case R.id.iv_item1:
if (mItem1Fragment == null) {
mItem1Fragment = new Item1Fragment();
}
fragmentTransaction.replace(R.id.id_frame_content, mItem1Fragment);
break;
case R.id.iv_item2:
if (mItem2Fragment == null) {
mItem2Fragment = new Item2Fragment();
}
fragmentTransaction.replace(R.id.id_frame_content, mItem2Fragment);
break;
}
fragmentTransaction.commit();
}
}
可以看到我们使用FragmentManager对Fragment进行了动态的加载,这里使用的是replace方法.其中Item1Fragment和Item2Fragment均为继承自Fragment的普通Fragment,代码很简单,就不再贴出来
注:如果使用Android3.0以下的版本,需要引入v4的包,然后Activity继承FragmentActivity,然后通过getSupportFragmentManager获得FragmentManager。不过还是建议版Menifest文件的uses-sdk的minSdkVersion和targetSdkVersion都改为11以上,这样就不必引入v4包了。