网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Android如何使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果

这篇文章主要为大家展示了Android如何使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

专注于为中小企业提供成都网站设计、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业长汀免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了超过千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果

如何使用 CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现下面GIF图中的效果,再展开的时候头像处于红白中间,根据收缩程度改变头像的位置!底下的RecyclerView也跟随这个移动,不会出现中间隔出一段距离!(仅提供源码复制粘贴,很简单的)

先看下效果图:

Android如何使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果

下面上代码

XML布局代码如下:

<?xml version="1.0" encoding="utf-8"?>


  

    

      

        

          

            

              

                

                

              

              

              

                

                

              

              

              

                

                

              

              

              

                

                

              

            

          

        

        

          

          

        

        

          

            

            

              

              

            

          

        

      

    

    

  

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

这样就完成了!

以上就是关于Android如何使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。


当前题目:Android如何使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果
分享URL:http://bjjierui.cn/article/pescjh.html

其他资讯