当前位置:主页 > 查看内容

使用Fragment

发布时间:2021-06-14 00:00| 位朋友查看

简介:使用Fragment Fragment的介绍 Fragment的生命周期 创建Fragment 在Activity中添加Fragment 实例——川菜菜谱 Fragment的介绍 Fragment碎片是一种嵌入在Activity中的UI片段它可以用来描述Activity中的一部分布局。如果Activity界面布局中的控件比较多比较复杂……

Fragment的介绍

Fragment(碎片)是一种嵌入在Activity中的UI片段,它可以用来描述Activity中的一部分布局。如果Activity界面布局中的控件比较多比较复杂,那么Activity管理起来就比较麻烦,我们可以使用Fragment把屏幕划分成几个片段,进行模块化的管理,从而使程序更加合理和充分地利用的空间。

Fragment的生命周期

Activity生命周期中有5中状态,分别是启动状态、运行状态、暂停状态、停止状态和销毁状态。

因为Fragment是被嵌入到Activity中使用的,因此它的生命周期的状态是直接受其所属Activity的生命周期状态影响。

在这里插入图片描述
Fragment生命周期与Activity的生命周期十分相似。Fragment生命周期比Activity多了以下几个方法,具体如下:

  • onAttach():Fragment和Activity建立关联时调用
  • onCreateView():Fragment创造视图时调用
  • onActivityCreate():Fragment相关联的Activity已经创建完成时调用
  • onDestroyView():Fragment关联的视图被移除时调用
  • onDetach():Fragment和Activity解除关联时调用

创建Fragment

public class NewsListFragment extends Fragment{
	public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
	View v = inflater.inflate(R.layout.fragment,container,false);
	return v;
	}
}

上述代码重写了Fragment和onCreateView()方法,并在该方法中通过LayoutInflater的inflate()方法将布局文件fragment.xml动态加载Fragment中

在Activity中添加Fragment

Fragment创建完成后并不能单独使用,还需要将Fragment添加到Activity中。在Activity中添加Fragment有两种方式:

  1. 在布局文件中添加Fragment
    在Activity引用的布局文件中添加Fragment时,需要使用标签,该标签与其他控件的标签类似,但必须指定android:name属性,其属性值为Fragment的全路径名称。在LinearLayout布局中添加NewListFragment代码:
<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"
tools:context=".MainActivity"
>
<fragment
     android:name="cn.itcast.NewListFragment"
     android:id="@+id/newslist"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>
</LinearLayout>
  1. 在Activity中动态加载Fragment

当Activity运行时,也可以将Fragment动态添加到Activity中,具体步骤如下:

(1)创建一个Fragment的实例对象
(2)获取FragmentManager的实例
(3)开启FragmentTransaction
(4)向Activity的布局容器中添加Fragment
(5)通过commit()方法提交事务

在Activity中添加Fragment的代码如下:

public class MainActivity extends Activity{
	protected void onCreate(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	NewsListFragment fragment = new NewsListFragment();//实例化Fragment对象
	FragmentManager fm = getFragmentManager();//获取FragmentManager实例
	FragmentTransaction beginTransaction = fm.beginTransaction();//开启FragmentTransaction
	beginTransaction.replace(R.id.ll,fragment);//添加一个Fragment
	beginTransaction.commit();//提交事务
	}
}

实例——川菜菜谱

在这里插入图片描述

  1. 创建工程
    创建一个名为SichuanCuisine的应用程序

  2. 放置界面控件
    请添加图片描述
    请添加图片描述

  3. 放置界面控件
    在这里插入图片描述

  4. 创建两个Fragment的布局文件
    在这里插入图片描述
    在这里插入图片描述

  5. 创建川菜列表Item界面
    在这里插入图片描述

  6. 创建ContentFragment
    在这里插入图片描述

ContentFragment类继承自Fragment,在该类中获取界面控件并将菜品做法的数据显示在控件上
通过setText()方法将获取的Activity中设置的菜品做法数据信息显示到界面控件上,创建一个initView()方法,在该方法中获取菜品做法信息的控件

  1. 创建MenuFragment
    在这里插入图片描述
    在这里插入图片描述
    MenuFragment类继承自Fragment,在该类中实现显示川菜列表的信息,点击列表Item,在界面右侧会出现对应菜品的做法信息
    通过setOnItemClickListener()方法为列表中的Item添加点击事件的监听器,在该监听器中重写onItemClick()方法,在onItemClick()方法中首先通过getActivity()方法获取Activity的实例对象,接着通过该对象的getFragmentManager()方法获取FragmentManager的实例对象,最后通过findFragmentById()方法获取到ContentFragment对象listFragment,并调用setText()方法设置点击的Item对应的菜品做法信息
  2. 编写MAinActivity
    在这里插入图片描述
    在这里插入图片描述
    在MAinActivity中将MenuFragment与ContentFragment添加到MAinActivity界面上
    通过replace()方法将ContentFragment和MenuFragment添加到对应的布局中
;原文链接:https://blog.csdn.net/weixin_46311652/article/details/115602878
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐