2019级软件工程应用与实践-人工智能快递柜(代码分析6)

2021SC@SDUSC

HomePageActivity

protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_page_activity);

        AndroidUtil.hideNavigationBar(getWindow());
        
        initChildComponent();
        
        continueUpdateTime();
        
        addSchoolBadgeClickListener();
        
        startService();

        initAttachFragment();

        initOpenCodeInputFragment();
    }

隐藏导航栏

AndroidUtil.hideNavigationBar(getWindow());

初始化子组件的状态和点击事件

initChildComponent();

持续更新主页右上角时间

continueUpdateTime();

5秒内点击10次校徽,呼出导航栏, 2秒后关闭

addSchoolBadgeClickListener();

开启柜子串口通信服务

startService();

具体介绍

    HomePageAttachFragment attach;
    LinearLayout attachLayout;
    private void initAttachFragment() {
        attachLayout = findViewById(R.id.attach_container);
        
        attach = new HomePageAttachFragment();
        attach.setTransButtonClickListener((View view) -> {
            Log.e(TAG, "attach tran button is clicked.");
            attachLayout.setVisibility(View.GONE);
            openCodeInputLayout.setVisibility(View.VISIBLE);
            openCodeInputFragment.startCounter();
        });

        FragmentTransaction ft = getFragmentManager()
                .beginTransaction();
        ft.add(R.id.fragment_container_attach, attach);
        ft.commit();
    }

此方法向界面的 fragment_container 中添加 attach。在此方法中先实例化 KeyBoardFragment 的对象,然后隐藏 attach 并展示其他内容。

FragmentTransaction ft = getFragmentManager()

获得一个FragmentTransaction的实例

ft.add(R.id.fragment_container_attach, attach);

添加一个显示详细内容的Fragment

ft.commit();

提交事务

OpenCodeInputFragment openCodeInputFragment;
    LinearLayout openCodeInputLayout;
    private void initOpenCodeInputFragment() {
        openCodeInputLayout = findViewById(R.id.open_code_input_container);
        openCodeInputFragment = new OpenCodeInputFragment();

        // 添加参数
        Bundle bundle = new Bundle();
        bundle.putString("title", "暂存11物品");
        bundle.putInt("image", R.drawable.process_save);
        openCodeInputFragment.setArguments(bundle);

        openCodeInputFragment.setRetBtnListener((View view) -> {

            Log.e(TAG, "ret button is clicked.");
            openCodeInputLayout.setVisibility(View.GONE);
            attachLayout.setVisibility(View.VISIBLE);

        });

这个描述了暂存物品的方法,在点击相关的按钮之后,界面会发生改变,显示点击的地方打开,相应的格子柜子的颜色会发安生变化,表示已经打开的位置。
然后,获得一个FragmentTransaction的实例

FragmentTransaction ft = getFragmentManager()
                .beginTransaction();

添加一个显示详细内容的Fragment

ft.add(R.id.open_code_input_frame, openCodeInputFragment);

最后进行事务提交便可完成此方法

ft.commit();
实时显示系统时间
    private void continueUpdateTime() {

        TextView currentTimeView = (TextView) findViewById(R.id.current_time);
        new Thread(() -> {
            while (true) {
                Date now = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                HomePageActivity.this.runOnUiThread(() -> {
                    currentTimeView.setText(sdf.format(now));
                });
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

这个方法主要是实现持续更新主页右上角时间,让使用者可以看到操作时的时间。
本方法整体是一个线程,线程里面无限循环,每隔10000mills发送一个消息,在主线程里面处理消息并在图像界面更新时间。采用的是Android Studio自带的方法SimpleDateFormat(),使用时需引入头文件import java.text.SimpleDateFormat;使用new SimpleDateFormat(“yyyy-MM-dd HH:mm”);设置了时间显示的形式,灵活多变。通过currentTimeView.setText(sdf.format(now));使时间区域可以在对应的位置展示出来。

但本方法有一个小小的问题,它只能获取当地时间,在不同时区的时候可能会产生错误,可以设置设置timeZone,使之在不同地区获取某个特定时区的时间。

学习笔记

findViewById(int id)

切记findViewById(int id)需要写在onCreate()之后,否则闪退(自己试着写demo的时候翻车了)
在android中,findViewById(int)是获取当前上下文中的组件,即在这句话的完整句子是:layout.findViewById(int),前面省略的layout,是默认在oncreate方法中setContentView(int layoutid)中设置的layout。
这时候,如果我们需要访问的view并不在当前activity所在的layout中,我们就需要先加载该view所在的layout,然后通过layout来访问该view,如:

    LayoutInflator layoutInflator=getLayoutInflator();
    LinearLayout ll=(LinearLayout)layoutInflator.inflator(R.layout.XXX,null);
    TextView tv=(TextView)ll.findViewById(R.id.XX);

而实际上,每次通过getLayoutInflator().inflator(R.layout.XXX)所得到的layout其实都相当于一个类的实例,也就是说,两次通过该方法得到的相同layout的实例,用它调用findViewById(int id)所得到的view是不同的。具体的例子可以看此博客:关于findViewById你所不知道的

调用的findViewById()函数其实有两种(目前我只看到两种,不确定还有没有其他的),一种是Activity类中findViewById()函数,另外一种是View类中定义的findViewById()函数。

一般我们在oncreate()方法中使用的(view)findViewById(R.id.)即是调用的Activity中的findViewById()函数,Activity中的findViewById()是从R.java中寻找东西,那么我们就要杜绝相同名字的控件;其次,在调用view中的findViewById()一定要想好父View是谁!即view.findViewById()中的view要找对,如果没有找对父View,返回基本都是null了
在其他情况写出的***view.findViewById()中调用的是view类中的findViewById(),这个使用方法较简单。如果从一个view的child view中寻找指定id的对象,所以即使几个layout的XML文件中的View的id号相同的话,只要他们没有相同的父节点,或有相同的父亲节点,但不在父节点及以上节点调用findViewById通过id来查找他们就是没有问题。
findViewById报错

Fragment
Fragment添加

方法一: 布局里的标签
标识符: tag, id, 如果都没有, container的id将会被使用.

方法二: 动态添加(本项目中使用)
动态添加利用了一个transaction:

        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag(FragmentB.TAG);
        if (null == fragment) {
            FragmentB fragmentB = new FragmentB();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragment_container, fragmentB, FragmentB.TAG)
                               .commit();
        }

commit()方法并不立即执行transaction中包含的动作,而是把它加入到UI线程队列中.如果想要立即执行,可以在commit之后立即调用FragmentManager的executePendingTransactions()方法.

commit()方法必须在状态存储之前调用,否则会抛出异常,如果觉得状态丢失没关系,可以调用commitAllowingStateLoss(). 但是除非万不得已, 一般不推荐用这个方法, 会掩盖很多错误.

构造和参数传递

所有的Fragment都必须有一个public的无参构造函数, 因为framework经常会在需要的时候重新创建实例(状态恢复时), 它需要的就是这个构造.
如果无参构造没有提供,会有异常.

所以不要给Fragment写有参数的构造函数, 也不要企图搞个什么单例的Fragment. 这些都是反设计的.

Fragment的通信

除了DialogFragment和嵌套Fragment需要与自己的parent fragment通信以外, 一般的fragment是不与其他fragment有任何通信的. 因为要求应尽量独立, 模块化, 可复用.
fragment与自己的parent activity (除了嵌套和dialog的情况外, 这个parent通常是activity) 有直接通信, 一般以这三种方式:

  1. 在构造fragment的时候, 通过Bundle传递参数.
  2. parent可以直接调用fragment的public方法, 这里也可以传递一些参数.
  3. Listener, 也即parent实现的callback接口, fragment可以在自己内部调用, 这里fragment也可以传递参数出去.

对于DialogFragment来说, 可以通过一个public的set方法将外面的target设置进去.

Fragment的生命周期

Fragment的生命周期首先和Activity的生命周期密切相关,
如果activity stopped,其中所有的fragment都不能start;
如果activity destroyed, 其中所有的fragment都会被destroyed.
只有activity在resumed状态下,fragment的生命周期可以独立改变,否则它被activity控制.
在这里插入图片描述

FragmentTransaction基础操作

FragmentTransaction 中对Fragment有如下几种操作:

attach(), detach()add(), remove(),show(), hide(),replace()

除了replace()以外其他都是成对的.

其中attach()和detach()不是很常用.
调用detach()之后, fragment实际的生命周期会走到onDestroyView(), 但不会走onDestroy()和onDetach(), 也即fragment本身并没有被销毁, 只是view被销毁了. 这和addToBackStack()的情况一样, 尽管调用detach()的时候没有addToBackStack(), 仍然只是走到view被销毁的阶段.

add()和remove()是将fragment添加和移除.
remove()比detach()要彻底一些, 如果不加入到back stack, remove()的时候, fragment的生命周期会一直走到onDetach().

show()和hide()是用来设置fragment的显示和隐藏状态, 这两个方法并不对应fragment的状态变化,只是将view设置为visible和gone,然后调用onHiddenChanged()的回调.

实际上replace() == remove() + add(), 所以它的反操作也是replace(), 只不过把add和remove的东西交换一下.

关于replace()和show(), hide()的选择, 要根据实际使用情形来定.
replace()的好处是会减少内存占用, 但是返回时需要重新走完初始化的过程.
show()和hide()只是控制了fragment的显示和隐藏, 不会改变生命周期状态, 也即fragment始终是处于running状态的, 被保持在内存中, 适用于频繁切换的情形.

就酱!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值