栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();

提交事务

学习笔记 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状态的, 被保持在内存中, 适用于频繁切换的情形.

就酱!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号