CoordinatorLayout 自定义Behavior并不难,由简到难手把手带你撸三款!

 

先来看看最终的效果~~

这里写图片描述

嗯。。一个是头像上移的 另一个是模仿UC浏览器的。

(PД`q。)你不是说!有三款的吗,怎么只有两款!!!!

不要急嘛。。。 说了从简到难,第一款是介绍概念的啦。
关于CoordinatorLayout,以及系统预留ScrollBehavior使用网上以及有很多文章,这里就不阐述了,如果你还不了解,你可以查看[译]掌握CoordinatorLayout

基础概念

其实Behavior就是一个应用于View的观察者模式,一个View跟随者另一个View的变化而变化,或者说一个View监听另一个View。

在Behavior中,被观察View 也就是事件源被称为denpendcy,而观察View,则被称为child。

开始自定义 难度1 Button与TextView的爱恨情仇

首先在布局文件中跟布局设置为CoordinatorLayout,里面放一个Button和一个TextView。

这里我们在Activity中做一些手脚,让Button动起来(不要在意坐标这些细节)

此时,Button已经可以跟随手指移动了。

现在去自定义一个Behavior让TextView跟随Button一起动!

创建一个EasyBehavior类,继承于Behavior

注意两个方法

layoutDependsOn() 代表寻找被观察View

onDependentViewChanged() 被观察View变化的时候回调用的方法

在onDependentViewChanged中,我们让TextView跟随Button的移动而移动。代码比较简单,一看就懂。

Tip

必须重写带双参的构造器,因为从xml反射需要调用。

接下来,在xml中,给TextView设置我们的Behavior。

运行效果如下:

这里写图片描述
这样一个最简单的behavior就做好了。

##难度 2 仿UC折叠Behavior

这个效果布局嵌套比上一个例子些许复杂,如果看起来吃力,务必去补习CoordinatorLayout!!!!

先定义xml如下:

有一点值得注意的是,app:layout_anchor=”@id/frameLayout”这个属性,是附着的意思,这里我用作给了toolbar,代表toolbar附着在了frameLayout之上。会跟随frameLayout的scroll而变化Y的值。

思路分析

如何实现折叠呢,下半部分不用管了,AppBarLayout已经帮我们做好了,我们只要标注相应的scrollflags即可,所以,如上的布局,不做任何处理的话,作为标题的TextView是一直显示的,于是只要让TextView跟随Toolbar变化而变化就可以了。 接下来就创建一个Behavior类!

现在你应该可以很轻易的看懂这个Behavior的结构了。我们主要大展身手的地方其实是在onDependentViewChanged方法。

里面监听了Toolbar的Y坐标变化,然后让TextView的Y坐标也跟着变化。达到如预览图效果。
这里写图片描述

难度3 头像缩放效果

相信有了以上两个例子,这个效果对你来说不难了,不就是让imageView监听Toolbar然后跟随Toolbar的唯一变化而进行位移以及缩放么。

所以具体的解析就不说了,直接上个Behavior代码

还是说说坐标计算相关的吧。举个例子。如何让ImageView处于Toolbar中心呢,我的代码如下

为什么是这样? 上个图就明白了
这里写图片描述
怎么样,不难吧,哈 喜欢的点个赞 给个star哦~~

项目地址 https://github.com/githubwing/CustomBehavior

CoordinatorLayout 自定义Behavior并不难,由简到难手把手带你撸三款!》有159个想法

  1. My husband and i were really fulfilled when Raymond could conclude his researching from the precious recommendations he grabbed through the web pages. It’s not at all simplistic just to happen to be making a gift of secrets that the others may have been trying to sell. And now we acknowledge we now have the blog owner to give thanks to because of that. The most important explanations you made, the easy site menu, the friendships you can help to create – it’s many exceptional, and it’s really facilitating our son and us imagine that that content is awesome, which is seriously mandatory. Thank you for the whole lot!

  2. pwvqc 306140 960119 qndkx [url=http://bestpriceviagrausa.com/]Best Price VIAGRA[/url] um 247836 904794 tudaj [url=http://bestpriceviagrausa.com/]Best Price VIAGRA[/url] 672616 gxfen [url=http://viagra100mgtabletsretailpriusa.com/]VIAGRA 100mg Tablets Retail Price[/url] 550219 wyssh [url=http://genericviagraonlinepharmacyusa.com/]Generic VIAGRA Online Pharmacy[/url] fynil 610145 724987 bxecd http://bestpriceviagrausa.com/ ohht368690 493025 25383 630476 571916 Canadian Pharmacy Viagra krqis 504189 624333 mkvnu

  3. I just want to mention I’m very new to blogging and site-building and honestly savored this website. Probably I’m going to bookmark your blog . You certainly come with good well written articles. Thanks a lot for sharing with us your webpage.

  4. I just want to say I’m all new to blogging and site-building and seriously liked this web-site. Very likely I’m likely to bookmark your site . You absolutely come with outstanding stories. Regards for sharing with us your blog.

  5. All absolutely free clipart is created by an identical designer. Nowadays you know the best place to locate totally free clipart, you can save a good deal of Googling! AAA Clipart is a superb site on account of the substantial selection of completely free clipart images available there.

  6. I just have to notify you that I am new to wordpress blogging and clearly admired your site. Very possible I am prone to store your blog post . You certainly have fabulous article material. Get Pleasure From it for telling with us your main domain document

  7. I¡¦m not certain the place you’re getting your info, but good topic. I needs to spend some time learning much more or figuring out more. Thanks for wonderful info I used to be on the lookout for this information for my mission.

  8. I enjoy you because of all of your hard work on this site. Kim enjoys engaging in research and it’s really easy to understand why. All of us hear all about the dynamic mode you make functional things by means of the web blog and attract response from others on this idea and our favorite child is now studying a lot of things. Take advantage of the remaining portion of the new year. You have been doing a wonderful job.

  9. I really wish to reveal to you that I am new to online blogging and very much adored your website. Most likely I am most likely to store your blog post . You indeed have impressive article information. Admire it for telling with us your own website webpage

  10. naturally like your web-site however you have to take a look at the spelling on quite a few of your posts. Several of them are rife with spelling issues and I find it very troublesome to inform the reality on the other hand I¡¦ll definitely come back again.

  11. What i do not understood is in truth how you are now not actually a lot more well-liked than you may be now. You are very intelligent. You know thus considerably with regards to this matter, produced me personally believe it from a lot of varied angles. Its like women and men are not involved until it¡¦s one thing to accomplish with Girl gaga! Your own stuffs excellent. All the time deal with it up!

  12. Magnificent beat ! I wish to apprentice while you amend your website, how can i subscribe for a blog web site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear idea

  13. I just desire to show you that I am new to having a blog and certainly admired your page. Very likely I am probably to save your blog post . You simply have wonderful article material. Delight In it for telling with us all of your blog report

  14. Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a bit, but other than that, this is magnificent blog. An excellent read. I will certainly be back.

  15. Costo in farmacia italiana, medico generico e prezzo slovenia, generico prezzo migliore tranne senza ricetta medica in farmacia. Vendita in svizzera anche in farmacia senza ricetta medica, in italia prezzo e generico italia prezzo nonostante acquisto generico online sicuro

  16. I’m not sure where you’re getting your information, but good topic. I needs to spend some time learning much more or understanding more. Thanks for great information I was looking for this info for my mission.

  17. You actually make it seem so easy with your presentation however I to find this matter to be really one thing that I think I’d by no means understand. It kind of feels too complicated and extremely large for me. I’m looking forward to your subsequent put up, I¡¦ll try to get the cling of it!

  18. cialis filmtabletten 20mg

  19. Wow, awesome blog layout! How lengthy have you ever been blogging for? you make running a blog glance easy. The full glance of your web site is wonderful, as smartly as the content material!

  20. venta cialis colombia

  21. Good ¡V I should certainly pronounce, impressed with your website. I had no trouble navigating through all tabs as well as related info ended up being truly easy to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your customer to communicate. Nice task..

  22. pharmacie en ligne cialis 5 mg

  23. naturally like your web site however you have to take a look at the spelling on quite a few of your posts. Many of them are rife with spelling issues and I find it very troublesome to inform the truth on the other hand I will definitely come again again.

  24. wow look it order cialis

  25. las vegas viagra

  26. cialis smiley g remember

发表评论

电子邮件地址不会被公开。