赵雅智_fragment动态添加实现
2014-06-13 10:17
232 查看
步骤
新建主layout包含两个RelativeLayout布局
新建2个Fragment类分别对应2个layout
在主activity中进行动态添加
获取FragmentManager对象
获取FragmentTransaction对象
添加Fragment对象
提交事务
运行效果:
主要代码:
activity_main.xml
activity_fragment1.xml
activity_fragment2.xml
MainActivity.java
Fragment1.java
Fragment2.java
新建主layout包含两个RelativeLayout布局
新建2个Fragment类分别对应2个layout
在主activity中进行动态添加
获取FragmentManager对象
获取FragmentTransaction对象
添加Fragment对象
提交事务
运行效果:
主要代码:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > </RelativeLayout> <RelativeLayout android:id="@+id/relativeLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativeLayout1" > </RelativeLayout> </RelativeLayout>
activity_fragment1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#FFFF6F" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
activity_fragment2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#ffa6ff" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
MainActivity.java
package com.example.android_fragment2; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1.获取FragmentManager对象 FragmentManager manager = getFragmentManager(); //2.获取FragmentTransaction对象 FragmentTransaction transaction = manager.beginTransaction(); //添加Fragment对象 transaction.add(R.id.relativeLayout1, new Fragment1(), "fragment1"); transaction.add(R.id.relativeLayout2, new Fragment2(), "fragment2"); //4.提交 transaction.commit(); } }
Fragment1.java
package com.example.android_fragment2; 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 { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.activity_fragment1, container, false); } }
Fragment2.java
package com.example.android_fragment2; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.activity_fragment2, container, false); } }
相关文章推荐
- fragment中添加ViewPager实现界面切换,动态添加radiobutton
- Kotlin实现动态添加Fragment碎片到FrameLayout布局中
- Android Fragment基础之动态添加Fragment 实现两个Fragment之间的通信
- ViewPager实现TabHost动态添加、删除Fragment,用红色小圆球指示当前页面
- 简化 RecyclerView 的多种 type 的 adapter,Fragment 可以动态添加到 RecyclerView 上,实现复杂的界面分多个模块开发
- ViewPager+Fragment实现TabHost,Fragment动态添加、删除,Tab选项卡跟随滑动
- ViewPager实现TabHost动态添加、删除Fragment,用红色小圆球指示当前页面
- ViewPager+Fragment实现Tabhost,动态添加删除Fragment,可供第三方使用的类库
- ViewPager+Fragment实现TabHost,Fragment动态添加、删除,Tab选项卡跟随滑动
- ViewPager+Fragment实现Tabhost,动态添加删除Fragment,可供第三方使用的类库
- 仿新浪热门微博页面动态添加Tab标签与Fragment联动的实现
- Fragment 实现tab页卡切换并保存界面状态,动态添加Fragment
- 如何实现申请单的动态添加(WEB页面)
- javascript实现的动态添加表单元素input,button等(appendChild)
- 使用MYSQL、PHP和FLEX实现上传图片,动态在DataGrid中添加下载按钮和上传的图片.
- DataGrid 动态添加模板列 实现代码
- javascript实现的动态添加表单元素input,button等(appendChild)
- 如何实现服务器端下页面动态添加JavaScript脚本
- 在Web报表中实现动态添加字段、 动态绑定查询或其它数
- 在Css样式表中添加javascript,实现动态效果