(3)动态的使用Fragment

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包了。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值