首页 技术 正文
技术 2022年11月12日
0 收藏 643 点赞 3,568 浏览 2045 个字

在Fragment简单用法的基础上做修改

一.新建:another_right_fragment.xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:background=”#0000ff”>

<TextView

android:text=”另一个raight fragment”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:id=”@+id/textView3″

android:layout_weight=”1″ />

</LinearLayout>

二.新建AnotherRightFragment类:

public class AnotherRightFragment extends Fragment {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.another_right_fragment, container, false);

return view;

}

}

三.修改activity_main.xml:

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

xmlns:tools=”http://schemas.android.com/tools”

android:layout_width=”match_parent”

android:layout_height=”match_parent”>

<fragment

android:layout_width=”0dp”

android:layout_height=”match_parent”

android:name=”com.example.guo.fragment.LeftFragment”

android:layout_weight=”1″

android:id=”@+id/left_fragment” />

<fragment

android:layout_width=”0dp”

android:layout_height=”match_parent”

android:name=”com.example.guo.fragment.RightFragment”

android:layout_weight=”1″

android:id=”@+id/right_fragment” />

</LinearLayout>

四.修改left_fragment.xml

给按钮增加android:onClick=”myClick”

五.修改MainActivity,增加click方法,

protected void myClick(View view){

if( view.getId() == R.id.btn ){

AnotherRightFragment anotherRightFragment = new AnotherRightFragment();

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.id.right_fragment, anotherRightFragment);

fragmentTransaction.addToBackStack(null); // 按下返回键,返回上一个Fragment;不加改行,则直接退出应用

fragmentTransaction.commit();

}

}

可以看到,首先我们给左侧碎片中的按钮注册了一个点击事件,然后将动态添加碎片的

逻辑都放在了点击事件里进行。结合代码可以看出,动态添加碎片主要分为5 步。

1. 创建待添加的碎片实例。

2. 获取到FragmentManager,在活动中可以直接调用getFragmentManager()

方法得到。

3. 开启一个事务,通过调用beginTransaction()方法开启。

4. 向容器内加入碎片,一般使用replace()方法实现,需要传入容器的id 和待添加的

碎片实例。

5. 提交事务,调用commit()方法来完成。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,966
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783