(3)动态的使用Fragment
来源:互联网 发布:网络语 安利 编辑:程序博客网 时间:2024/06/07 04:05
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>
<?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
- (3)动态的使用Fragment
- 静态,动态使用Fragment的讲解。
- android Fragment动态加载的使用详解
- Android之Fragment的动态使用
- 动态使用Fragment
- 动态使用Fragment
- Fragment之动态使用
- fragment静态动态使用
- Fragment的动态加载
- Fragment的动态添加
- 使用Fragment实现动态布局
- 使用Fragment创建动态UI
- 使用Fragment建立动态UI
- 使用fragment构建动态布局
- activity中动态嵌套使用fragment的方法
- Android动态加载布局(fragment限定符的使用)
- Fragment之动态添加的简单使用步骤
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个Fragment
- C++常用的智能指针auto_ptr、shared_ptr
- netty权威指南读书笔记--liunx网络I/O模型介绍
- 记:破解MFC小程序00
- 自定义属性
- System类的 getProperties()和getProperty(String)取得当前系统的属性
- (3)动态的使用Fragment
- css国际化经验总结
- IDEA------小技巧(1)注释/取消注释快捷键
- IPC基础
- PHP关于ob_系列函数的学习总结
- 【Spring】概述
- 关于proteus中串口发送数据与实际不符的问题(如发00h,收80h)
- Linux Page Cache
- Android 全屏修改