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

详解Android使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果

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

详解Android使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果

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

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

这样就完成了!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

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

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

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