coordinatorlayout appbarlayout collapsingtoolbarlayou实现手指滑动效果
如何使用 coordinatorlayout appbarlayout collapsingtoolbarlayou实现下面gif图中的效果,再展开的时候头像处于红白中间,根据收缩程度改变头像的位置!底下的recyclerview也跟随这个移动,不会出现中间隔出一段距离!(仅提供源码复制粘贴,很简单的)
先看下效果图:
下面上代码
xml布局代码如下:
java代码如下:
/** * 获取控件信息 */ private void initview() { fragmentontstfappbar = (appbarlayout) view.findviewbyid(r.id.fragment_ontstf_appbar); fragmentontstftoolbar = (toolbar) view.findviewbyid(r.id.fragment_ontstf_toolbar); fragmentontstfportrait = (circleimageview) view.findviewbyid(r.id.fragment_ontstf_portrait); fragmentontstfname = (textview) view.findviewbyid(r.id.fragment_ontstf_name); fragmentontstfintroduce = (textview) view.findviewbyid(r.id.fragment_ontstf_introduce); fragmentontstfcollapsingtoolbarlayout = (collapsingtoolbarlayout) view.findviewbyid(r.id.fragment_ontstf_collapsingtoolbarlayout); fragmentontstfset = (imageview) view.findviewbyid(r.id.fragment_ontstf_set); fragmentontstfmessage = (imageview) view.findviewbyid(r.id.fragment_ontstf_message); fragmentontstfrecyclerview = (recyclerview) view.findviewbyid(r.id.fragment_ontstf_recyclerview); fragmentontstfcoordinatorlayout = (coordinatorlayout) view.findviewbyid(r.id.fragment_ontstf_coordinatorlayout); fragmentontstfrelativelayout = (relativelayout) view.findviewbyid(r.id.fragment_ontstf_relativelayout); setfragmentontstfrecyclerview(); appbar(); mpresenter.pselectuser(userid); } /** * 最主要的代码 * appbar滑动效果 */ private void appbar() { fragmentontstfappbar.setexpanded(true); fragmentontstfappbar.addonoffsetchangedlistener(new appbarlayout.onoffsetchangedlistener() { @override public void onoffsetchanged(appbarlayout appbarlayout, int i) { float alpha = (float) math.abs(i) / appbarlayout.gettotalscrollrange(); if (alpha == 0) { collapsingtoolbarlayout.layoutparams layoutparams = new collapsingtoolbarlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content); fragmentontstfrecyclerview.setpadding(0,fragmentontstftoolbar.getheight() / 2,0,0); layoutparams.setmargins(0, 0, 0, (-fragmentontstftoolbar.getheight() / 2)); layoutparams.gravity = gravity.bottom; fragmentontstftoolbar.setlayoutparams(layoutparams); fragmentontstfcoordinatorlayout.setclipchildren(false); } else if (fragmentontstfrelativelayout.getheight() - math.abs(i * 1.0f) == fragmentontstftoolbar.getheight()) { fragmentontstfcoordinatorlayout.setclipchildren(true); } else { int a = (int) ((fragmentontstftoolbar.getheight() / 2) * alpha); collapsingtoolbarlayout.layoutparams layoutparams = new collapsingtoolbarlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content); fragmentontstfrecyclerview.setpadding(0,fragmentontstftoolbar.getheight() / 2 - a,0,0); layoutparams.setmargins(0, 0, 0, -(fragmentontstftoolbar.getheight() / 2) - (-a)); layoutparams.gravity = gravity.bottom; fragmentontstftoolbar.setlayoutparams(layoutparams); fragmentontstfcoordinatorlayout.setclipchildren(false); } } }); }
这样就完成了!