android fragment的简单使用

xiaoxiao2021-02-28  117

  android系统运行在各种硬件之上,比如 手机、 手表、 平板、甚至更大的显示屏电视、出现了严重的碎片化,导致手机上app运行到其他的设备,无法适配该设备,谷歌就诞生出来一个fragment,fragment就是为碎片化而诞生的,fragment可以当成Activity的一个界面的组成部分,fragment有自己的生命周期,接收、和处理用户的事件,一个Activity可以有多个fragment,fragment的添加方式有两种,动态添加、以及静态添加。

1.静态添加fragment,把fragment当成View控件一样的使用,只不过多了一个name属性。 注:使用fragment静态添加,一定要加上id属性,否者报错。

  1.1、fragment的使用步骤, 继承fragment类,并重写onCreateView方法,此方法返回的View就是我们要显示的View,这里用一个简单的例子。来演示。一个fragment用于title,一个用于content

  titleFragment如下

public class TitleFragment extends Fragment { public TitleFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_title, container, false); view.findViewById(R.id.title).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "我是title", Toast.LENGTH_SHORT).show(); } }); return view; } }

  TitleFragment 对应的布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/title" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#88FF0000"/> </FrameLayout>

  contentfragment 同理

这里写代码片public class ContentFragment extends Fragment { public ContentFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_content, container, false); view.findViewById(R.id.content).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "我是内容", Toast.LENGTH_SHORT).show(); } }); return view; } } <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#55FF0000"/> </FrameLayout>

  1.2、将fragment写入Activity布局中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <fragment android:id="@+id/title" android:layout_width="match_parent" android:layout_height="50dp" android:name="com.app.wurui.fragmentdemo.TitleFragment"/> <fragment android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:name="com.app.wurui.fragmentdemo.ContentFragment"/> </LinearLayout>

  再次提醒,fragment标签中一定要加入id属性、、、MainActivity没有写任何代码,效果显示如下: 

2.动态添加Fragment,添加之前先了解几个API

Fragment 定义一些fragment FragmentManager 管理fragment类 获取 FragmentTransaction 保证一些列Fragment操作的原子性

获取FragmentManager

FragmentManager fm = getFragmentManager(); v4中使用 getSupportFragmentManager

获取FragmetTransaction 开启事务

FragmentTransaction ft = ft.beginTransaction();

下面可以操作fragment了,

add()将fragment向Activity中添加 remove()将fragment从Activity中移除 replace() 该方法集成add和remove方法 hide() 将传入的fragment隐藏 show() 将传入的fragment显示 setCustomAnimations() 该方法设置切换动画 detach() 会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。 attach() 重建view视图,附加到UI上并显示。 commit 提交。只有执行了此方法,删除 显示 添加等方法才会生效

  以上面的小例子,进行改造,先看看效果

  当然两个fragment没有什么变化,变化在MainActivity中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/titlebar" android:layout_width="match_parent" android:layout_height="50dp"/> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加fragment"/> <Button android:id="@+id/remove" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="移除fragment"/> <Button android:id="@+id/replace" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="替换fragment"/> </LinearLayout> public class MainActivity extends AppCompatActivity { private TitleFragment mf1; private TitleFragment2 mf2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager manager = getSupportFragmentManager(); MyOnClickListener listener = new MyOnClickListener(); findViewById(R.id.add).setOnClickListener(listener); findViewById(R.id.remove).setOnClickListener(listener); findViewById(R.id.replace).setOnClickListener(listener); } class MyOnClickListener implements View.OnClickListener{ @Override public void onClick(View v) { switch (v.getId()){ case R.id.add: add(); break; case R.id.remove: remove(); break; case R.id.replace: replace(); break; } } } private void add() { //获取FragmentManager的实例 FragmentManager fm = getSupportFragmentManager(); //开启事务 FragmentTransaction ft = fm.beginTransaction(); //添加fragment if(mf1 == null){ mf1 = new TitleFragment(); } ft.add(R.id.titlebar, mf1); //提交事务 ft.commit(); } private void remove(){ //获取FragmentManager的实例 FragmentManager fm = getSupportFragmentManager(); //开启事务 FragmentTransaction ft = fm.beginTransaction(); //移除fragment ft.remove(mf1); //提交事务 ft.commit(); } private void replace(){ //获取FragmentManager的实例 FragmentManager fm = getSupportFragmentManager(); //开启事务 FragmentTransaction ft = fm.beginTransaction(); //替换fragment ft.replace(R.id.titlebar, new TitleFragment2()); //提交事务 ft.commit(); } }

3.fragment的生命周期

  以下是fragment的官方图

可以看到Fragment比Activity多了几个额外的生命周期回调方法:

onAttach(Activity) 当Fragment与Activity发生关联时调用。 onCreateView(LayoutInflater, ViewGroup,Bundle) 创建该Fragment的视图 onDestoryView() 当该Fragment的视图被移除时调用 onDetach() 当Fragment与Activity关联被取消时调用

Fragment的基本使用就到此结束了

转载请注明原文地址: https://www.6miu.com/read-37449.html

最新回复(0)