Android High Level Topic (From 田元@zhihu)

https://www.zhihu.com/question/28425576

我猜你这个阶段是把大部分demo都能跑通了吧,常见的控件也会了吧。
但是,你现在还是会的太少了。
就是会用Linearlayout/relativelayout/button/textview/edittext/imageview(的很少一部分属性)来画一些简单的界面了吧。
就是会用activity/fragment(的生命周期这么少的知识)来让ui在手机里面显示出来了吧。
就是会用asynctask(这么简单的一个过时的类)来网络请求了吧。
就是会继承了个Application类来接几个第三方服务(几行代码就接入)了吧。
就是会用个broadcast(估计只会最基本的显式广播,排队广播,粘性广播啥的都不会用)了吧。
就是会ListView+BaseAdapter+ViewHolder(总共不到10个重载的方法)或者recycleview+adapter(估计不会自定义layoutmanager,估计没才过itemanim的坑)来展示数据了吧。
这总共就多大一点知识啊,学的这些东西内容这么少,不迷茫才怪。
如果我说屈你了。
好。你说你比这强。
都会自定义控件(总共就measure draw layout这三个方法)了,还会volley(代码写的一堆callback hell),还会sqllite(这玩意没多大用,也是死东西)勒,还会xml里面定义动画(就平移 反转 透明 旋转 属性这几种)勒,甚至你还会eventbus(别说只会onEventMainThread哦),你还会rxjava(学过函数式语言的都感觉这没啥)。
你说你还追新,md控件玩的溜的很,toolbar(兼容到4.4的沉浸式状态栏会用不?)会用,drawerlayout会用(碰到过与surfaceview冲突的情况不?),还会用coordlayout+collapsinglayout+recycleview来做隐藏额头(知道那个collapsinglayout中的mode是干啥的不,自定义behavor会不?:)
这些都会了不?
啥,你都会了?再留个作业。
APP里面的main在哪?
别的桌面应用框架像Qt,人家显示控件都要new一个window,那咱app的这个渲染控件的window在哪new的,咋让咱屏幕听话跟着画的?
咱手机这么多传感器,那传感器数据咋来到咱app的?
那manifest在咱手机里面咋滚轮的,发生了啥,系统咋处理的?
我在美团里面能打开支付婊的支付界面,这在咱手机里面发生了啥?

我提出来的这些都感觉虚?回去大量学习然后写代码吧。我的这些也不是专门找书看的,而是长期开发来排除各种bug,期间读各种大牛文章,甚至翻了dev/framework层源码理解来的。
多花时间吧,花大把大把的。

https://www.zhihu.com/people/tian-yuan-17-25

又要推广长期食用的那一套了。
图片加载:fresco
事件总线:eventbus
网络请求:retrofit rxjava
数据存储:http://realm.io
数据解析:Gson,我知道它速度不快,但是太好用了
图片处理:PhotoViewsubsampling-scale-image-viewPhotoCropper
简单缓存:ASimpleCache
二维码:ZXing
新的百分比支持:https://github.com/JulienGenoud/android-percent-support-lib-sample
profile工具:leakcanary
技术博客:square家的小窝https://corner.squareup.com/
完全开源APP汇总:https://github.com/pcqpcq/open-source-android-apps

CoordinatorLayout高级用法-自定义Behavior

http://m.blog.csdn.net/article/details?id=50290421

 

Android Design Support Library使用详解

http://www.tuicool.com/m/articles/RnAneuB

 

CoordinatorLayout高级用法-自定义Behavior – Loader’s Blog – 博客频道 – CSDN.NET

http://blog.csdn.net/qibin0506/article/details/50290421

 

 

 

 

 

Advertisements

android layout specific

  1. gravity for any layout

Edit Text

Progress Bar with Attribute

More about the LinearLayout

LinearLayout: Layout_gravity

RelativeLayout

and Table Layout

relative layout, align parent bottom (put your tabs at the header line's bottom part)

but many layout is derived from the linear layout, so to achieve some effect, we don't must use the relative layout, but use the gravity in linear layout

High Level ViewPager

Having understand the baisc knowledge of the viewpager, we will go deeper about the viewpager in this post.

Firstly, we should develop the conclusion that: we don't need to update the viewpager, we only need to develop the mechanism to update the fragment in the viewpager

For example the following swipe to refresh is a scheme to update the recycler view

https://guides.codepath.com/android/Implementing-Pull-to-Refresh-Guide

back to the view pager, we can think several questions to make us understand all the staff in this area

  1. Firstly, how to show the tab in our toolbar
    we need use the TabLayout inside our android widget toobar layout

  2. Secondly, how to make fragment into our viewpager
    viewpager like the recyclerview is just a view, so we need to manage it using the data structure — adapter

  3. thirdly, how to show the title in our TabLayout, so during our scroll, we can know clearly which page we are currently at.

The solution for the previous three question is:

use a self defined viewpager, which can store the pager and the title, especially use the getTitle() method to return the title string and put them in the tablayout

continue our question, we have talked that we use the viewpageradapter to delegate for all the tasks to the viewpageradapter, but how could we delegate the work

The previous image has shown how we delegate all the work to the adapter

we have already talked about the delegation and callback, then we need to go deeper about the callback mechanism design

IT IS THE DESIGN PATTERN

  1. Since we still based all the operation logic on the recycler view and the recycler view was put in a fragment, so our fragment also want to delegate all the operation of click behavior on the list item to the adapter.

we all know that the viewholder is necessary in the recycler view, and each item (holder) will get interaction at the onBindViewHolder phase. So this is why we put the interaction in the previous function.

the fragment and the adapter should be in the same level, but the dummy and dummy content should be put in the inner package

Then it is defined in the fragment class

Another big point in this section is we have merged the handlerthread into this part to do some time consuming work (to simulate the swipe to refresh)

to make the handler thread work, or in other words, to make the swipe operation possible, we should find the swipe layout firstly

the adapter view is generally special, although it has the work to request for the view component, BUT THEY REALLY DO IS CLOSELY TO THE HODLER IT SELF, SO WITHOUT THE ADAPTER, EACH ITEM CAN BE SHOWN CLEARLY, THIS IS WHY HE ADAPTER IS REALLY IMPORTANT TO REQUEST FOR THE VIEW COMPONENT

But the layout file is inflated by the fragment part, so we should visit or access the view component in the fragment part [NOT THE VIEW PAGER ACTIVITY PART, SINCE WE ALREADY HAVE TALKED THAT THE VIEW PAGER SHOULD NOW BE UPDATED]

TO update the recycler view is really easy, we only need the following two things


The notifyDataSetChanged() at this level never fail, it is much easy than the viewpager's notifyDataSetChanged()

Back to our handler thread, in our fragment, we have created the following class to derive from the handerthread to do the task sequentially

it also related to some design patten staff, we have already talked about the handler, so we will not talk about it.

similar to we create the workerhandler object at general activity where we pass the new Handler() as the parameter, since this is the phase where the ui thread is created, so the looper can be set automatically

and our WokerHandlerThread looks like the following

what does the worker do: DISPATCH THE MESSAGE

I JUST DISPATCH IT, I HOPE SOMEONE ELSE HAVE THE ABILITY TO PROCESS IT
SO I CREATE A CALLBACK TYPE AND HOPE SOMEONE PROVIDE THE SCHEME TO PROCESS IT

who have me? Current fragment, so current fragment should implement this interface

and we have a really bad time to recycler the message, the exception is: the message is still used while being recycled

since don't use message.recycle() by yourself, the looper will handle it

other function to implement the real function of some behavior is:

And toward those action function we have the following very important conclusion

Finally, emphasis it again, if we really want a function in our data structure, for example the fragment's list's interaction, workerhandler's after dispatched's behavior

WE WANT SOME SPECIFIC OPERATION MODE, WE CREATE THE LISTENER AND WE HOPE SOMEONE ELSE PASS THE LISTENER TO ME, AND THE LISTENER DO WHAT IT SHOULD DO IN THE CASE

the adapter is the recycler view's delegate, so it should implement fragment needs interaction operation

the fragment itself want to use the handler thread, so it self should implement the handler's callback

thats it

Android View including ViewParent

We have seen the ViewParent from the getParent method, then we will go deep about this in this post

http://blog.csdn.net/guolin_blog/article/details/12921889

Firstly we should know how to get the LayoutInflater

And secondly, we should know that in order to enable the layout's size, we should create a outer layout to cover it, then the property of this layout can has some effect

after reading the previous example, then we should begin to study the underlying mechanism of the LayoutInflater

The previous content can use the following graph to illustrate

Then we will begin to study more about the View component

http://blog.csdn.net/guolin_blog/article/details/16330267

Firstly, we should study the view component's lifecycle.

We will begin with the onMeasure() method

So, going through such a complicated process, we will come to the final important function setMeasuredDimension() method to set the dimension we have speculated, but we also can set it directly through override the function.

then we will go to the onLayout phase

after determine the place (4 key point of the view) we should use the parent view to set up this view, so the actual logic of this method is completed in the viewgroup's onLayout method

but we need to implement it by ourselves, and although the functions seems to be separately, but they are closely related internally

and as a good android programmer, we should always set the measured value equals to the actual value

Finally, we will use the canvas to draw some component on the screen

Then we will go from the drawable's perspective to talk more about the view state and content related to the view-redraw

http://blog.csdn.net/guolin_blog/article/details/17045157

Firstly, we should get an overview of the view state

then the selector xml file can be used as the background

Then the principal in the behind is created through the callback mechanism

Then about the view – redraw, it is implemented using the invalidate function

To totally understand the view in android, in this section will create our own widget and import them in our layout file

http://blog.csdn.net/guolin_blog/article/details/17357967

Firstly we should know the category of the self-defined view component, it can be grouped into three parts

The following example, has demonstrated the usage of the invalidate() method

The most important logic is in the onDraw() function, in this function, we will have the basic view component created

Then we will use the toolbar to talk about the combinational widget in view part

and finally, we will begin to learn how to derive from the system's view widget

then we will set up my list view to support the swipe operation (the code can be found at the ever note page)

ONE OF THE IMPORTANT THING IS IN THE ADAPTER CLASS, WE CAN USE THE GETCONTEXT TO GET THE CONTEXT AND GET THE LAYOUTINLFATER OBJECT AS THE FIRST POST TALKS

The following code segment have used the notifydatachanged() function to update the listview

Android View Pager Part I

a general layout of the view pager xml file looks like the foregoing one. the most important part is to remember hat: we always need the app:layout_behavior to define the view pager's behave so the page can be updated correctly

And in the java code, we should have two nested class in it so the view pager part can be shown correctly.

The first one is the nested fragment view, which represent the viewable part of current activity

the second one is the page adapter, which allows the view component to apply for the content form the system

In this demo design, we didn't allocate the view pager using add method, but every time we will create a new view pager on it to make it workable

And finally, in the oncreate part code, we should do the initialization

through passing the fragment manager, we will create the adapter for our fragment. And our vie w pager should setAdapter in the onCreate Function

More advanced topics will be talked at the part two. [for example, how to update the view component in the FragmentPagerAdapter and FragmentPagerStateAdapter]

Then we will continue to talk about some high level content about the view pager

If we want to show the tabs in the appbar, we can add the following part at the appbarlayout (actually it is a view group)

and we can use a separate package to create the fragment automatically to add them to the view pager adapter

If we create the fragment with other name, other than the (itemfragment) then the auto-generated code will meet some errors. We need to change it manually.

It is the adapter, the adapter should handle the click event, so we should pass the listener

Android Context and Application Singleton from Lin Guo

http://blog.csdn.net/guolin_blog/article/details/47028975

http://stackoverflow.com/questions/6854265/getapplicationcontext-getbasecontext-getapplication-getparent

http://blog.csdn.net/guolin_blog/article/details/47028975

Firstly, let we talk about the getApplicatoinContext()

这个函数返回的这个Application的上下文,所以是与app挂钩的,所以在整个生命周期里面都是不变的,这个好理解,但是使用的时候要注意,该context是和引用的生命周期一致的,所以和activity生命周期挂钩的任务不要使用该context,比如网络访问,防止内存泄露
Then we will begin to study linguo's article

contextcompat can help use to visit the color, but it has very little relationship with what we will talk about today

那如何计算context的数量呢:

首先来看郭琳在书中给出的全局获取context的方法,可以极大地方便我们进行toast提示等

我们不妨在这里进行一下思维的发散,我们知道Activity 是context的子集,并且activity中包含了UI 的部分,所以说我们不可能利用context来获取view component,只有在fragment这种结构中,利用getActivity() 函数来获取activity实例后,才能够实现。这也说明我们的snackbar的交互模型是针对能够传入view的click 操作的,而context属于全局提示(我们在使用安卓软件的时候也可以看出这种交互方式的端倪)

接下来回到guolin的blog的思路上来

首先我们就来对比一下getapplication 和 getapplicationcontext这两种方法

上面的代码不仅讲到了getApplication 和 getApplicationContext之间返回值的一致性,也告诉我们即使我们不创建一个新的类,也可以进行application 的全局获取,但是这并不符合我们单例的设计模式,下面我们继续比较这两种获取方法

上面已经降到了getBaseContext() 这一个方法,根据stackoverflow的建议,我们知道很多情况下是不建议使用getBaseContext()这个方法的,但是作为实现了context所有功能的类,我们有必要去了解android针对这一部分的设计模式

这样就完成了我们继承关系中的耦合

然后我们继续顺着application往后面讲。之前我们之所以会将application实例的获取 (getApplicationContext()) 放在oncreate函数中,是为了满足生命周期,可以通过过下面的例子进行表示

另外一个错误就是对单例模式的错误认识, 当然如果我们需要很强的context语义,我们可以使用getApplicationContext() 来代替this的赋值,简单来说,总结如下