Android中如何使用Fragment打造出炫酷效果

本文探讨了Android中Fragment的重要性和使用方法,包括其特征、生命周期、创建及两个实例。通过实例一展示了如何结合ListFragment和Fragment实现交互,实例二讲解了Fragment与RadioButton的联动。此外,还介绍了Fragment与Activity的通信、添加到Activity的方式以及FragmentManager的使用。
摘要由CSDN通过智能技术生成

作为一个Android开发人员,应该没有谁不知道Fragment!其重要性可想而知了!不多说,先上图

实例一(使用ListFragment和Fragment结合实现):
Fragment

实例一源代码代码下载地址

实例二(Fragment结合RadioButton)
Fragment

实例二源代码下载地址

Fragment

Fragment文件下载地址

Fragment的特征:

Fragment必须嵌入到Activity中使用。因此,即使Fragment拥有自己的生命周期,也会收到它所在的Activity的生命周期的控制。当Activity暂停时,Activity中所有的Fragment都会暂停;当Activity被销毁时,其中所有的Fragment将会被销毁;只有Activity处于活动状态时,程序员才可以通过方法独立地操作Fragment。

  • Fragment总是作为Activity界面组成部分。Fragment可调用getActivity()方法获取它所在的Activity。Activity可调用 FragmentManager的findFragmentById()或者findFragmentBuTag()方法来获取Fragment

  • 在Activity运行过程中,可调用FragmentManager的add(),remove(),replace()方法动态添加,移除,修改。

  • 一个Activity可以同时拥有多个Fragment,一个Fragment也可以被多个Activity复用。

  • Fragment可以响应自己的输入事件,并拥有自己的生命周期,但生命周期被所属的Activity的生命周期控制。

    Fragment常用知识点:

  • Fragment和所属Activity的通讯如何处理!

    1. Activity向Fragment传递数据:在Activity中创建Bundle数据包,并调用Fragment的setArgument(Bundle bundle)方法将Bundle数据传递给Fragment.

    2. Fragment向Activity传递数据:Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回掉接口,这样就能将Fragment的数据传给Activity了!

  • 如何将Fragment添加到Activity中:

    1. 在布局文件中使用<fragment…/>元素添加Fragment,<fragment…/>元素的android:name属性指定Fragment的实现类

    2. 在Java代码中通过FragmentTransaction对象的add()方法来添加Fragment.

  • Activity获取它包含的Fragment:用Fragment的getActivity();

  • Fragment获取它所在的Activity:调用关联的FragmentMangager的findFragmentById(int id)或者fingFragmentByTag(String tag)方法即可获取Fragment。

  • Fragment管理

    FragmentManager可以实现以下几方面的功能:

    1. 使用 findFragmentById(int id)或fingFragmentByTag(String tag)方法来获取指定的Fragment。
    2. 调用popBackStack()方法将Fragment从后台中弹出(模拟用户按下Back键)
    3. 调用addOnBackStackChangeListener()注册一个监听器,用于监听后台栈的变化。
  • Fragment事务

    如果需要添加,删除,替换Fragment,则需要借助于FragmentTransaction对象,FragmentTransaction代表Activity对Fragment执行多个改变

  • *

Fragment的生命周期

这里就不再手打赘述,直接从官网上复制最原始的解释给大家!多啰嗦一句,学习任何语言,官方文档一定要看。

The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:

  1. onAttach(Activity) :called once the fragment is associated with its activity.

  2. onCreate(Bundle): called to do initial creation of the fragment.

  3. onCreateView(LayoutInflater, ViewGroup, Bundle) :creates and returns the view hierarchy associated with the fragment.

  4. onActivityCreated(Bundle) :tells the fragment that its activity has completed its own Activity.onCreate().

  5. onViewStateRestored(Bundle) :tells the fragment that all of the saved state of its view hierarchy has been restored.

  6. onStart(): makes the fragment visible to the user (based on its containing activity being started).

  7. onResume(): makes the fragment begin interacting with the user (based on its containing activity being resumed).

As a fragment is no longer being used, it goes through a reverse series of callbacks:

  1. onPause(): fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.

  2. onStop(): fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.

  3. onDestroyView(): allows the fragment to clean up resources associated with its View.

  4. onDestroy(): called to do final cleanup of the fragment’s state.

  5. onDetach() :called immediately prior to the fragment no longer being associated with its activity.

创建Fragment

通常来说,创建Fragment只需要实现如下三个方法:
  onCreat();
  onCreatView();
  onPause();
按需求可重写其他方法。    

实例一:

MainActivity.java:用于对整个进程的控制

FoodContent:实体类

FoodListFragment:显示食品列表的Fragment

FoodDetailFragment:显示食品详细信息的Fragment

activity_main.xml:布局文件
……

MainActivity代码:

public class MainActivity extends AppCompatActivity implements FoodListFragment.Callbacks{
   


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onItemSelected(Integer id) {
        // 创建Bundle,准备向Fragment传入参数
        Bundle arguments = new Bundle();
        arguments.putInt(FoodDetailFragment.ITEM_ID, id);
        
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值