Android仿简书搜索框效果的示例代码

所属分类: 软件编程 / Android 阅读数: 29
收藏 0 赞 0 分享

前言

之前用简书的时候一直是在web端,后来下载了客户端,看到了搜索的那个动画,就尝试的去写了,没写之前感觉挺容易的,写了之后,就感觉里面还是有些要注意的东西的。话不多说,直接上图。

Activity 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
  >

  <android.support.v7.widget.RecyclerView
    android:id="@+id/id_recycleview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>


  <LinearLayout
    android:id="@+id/id_ll_title_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:gravity="right"
    android:orientation="horizontal">

    <RelativeLayout
      android:id="@+id/id_title_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/search_white_bg"
      android:paddingRight="10dp"
      android:paddingLeft="10dp"
      android:gravity="center"
      android:layout_marginTop="5dp"
      android:layout_marginBottom="5dp"
      android:layout_marginRight="16dp"
      >


      <TextView
        android:id="@+id/id_tv_search_min"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:gravity="center"
        android:maxLines="1"
        android:drawableLeft="@mipmap/search_icon"
        android:text="搜索"
        android:drawablePadding="10dp"
        android:textColor="#b7b7b7"
        android:textSize="13sp"
        />

    </RelativeLayout>

  </LinearLayout>

</RelativeLayout>

这里的TextView要添加maxLines=1属性,如果不添加,当text=“搜索简书内容和朋友”时会有2行变1行的效果,看起来效果不太好。

头部视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:id="@+id/id_header_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    <TextView
      android:id="@+id/id_tv_header_view"
      android:layout_width="match_parent"
      android:layout_height="120dp"
      android:background="@color/c_3ec88e"
      android:gravity="center"
      android:text="我是头部"
     />
</RelativeLayout>

activity 头部 xml.png

下面咱们省略findViewById的代码,直接看核心代码:

变量初始化:

    //获取屏幕宽度
    mMaxWidth = ScreenUtil.getScreenWidth();
    //搜索框距离屏幕边缘的margin
    int rightMargin = Px2DpUtil.dp2px(this, 17);
    //屏幕宽度减去左右margin后的搜索框宽度最大值
    mMaxWidth = mMaxWidth -rightMargin*2;
    //搜索框宽度最小值
    mMinWidth = Px2DpUtil.dp2px(this, R.dimen.d_80);
    //header布局高度
    mHeaderHeight=Px2DpUtil.dp2px(this,R.dimen.d_120);

RecyclerView 滚动监听:

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
      }

      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        LinearLayoutManager l = (LinearLayoutManager)recyclerView.getLayoutManager();
        //获取第一个可见视图的position
        int position = l.findFirstVisibleItemPosition();
        //获取第一个完全可见视图的position
        int firstCompletelyVisibleItemPosition = l.findFirstCompletelyVisibleItemPosition();
        //当position=0时,对标题栏执行透明度变化
        if (position == 0) {
          //计算滚动的距离占header高度的比例
          double delta = Math.floor(((float) getScollYDistance(recyclerView) % mHeaderHeight));
          //给标题栏设置透明度
          mLlTitle.getBackground().setAlpha((int) delta);
        }
        //当position=1时,搜索框最大
        if (position == 1) {
          ObjectAnimator animator = ObjectAnimator.ofInt(new ViewWidthWrapper(mRlTitleLayout), "width", mMaxWidth);
          setAnimatorListener(animator,1);
        }
         //当position=0时,搜索框最小
        if(firstCompletelyVisibleItemPosition==0){
          ObjectAnimator animator = ObjectAnimator.ofInt(new ViewWidthWrapper(mRlTitleLayout), "width", mMinWidth);
          setAnimatorListener(animator,0);
        }
      }

    });

获取RecycleView垂直滚动的距离:

public int getScollYDistance(RecyclerView rv) {
    LinearLayoutManager layoutManager = (LinearLayoutManager) rv.getLayoutManager();
    //获取第一个可见item的position
    int position = layoutManager.findFirstVisibleItemPosition();
    //获取第一个position的View
    View firstVisiableChildView = layoutManager.findViewByPosition(position);
    //获取第一个可见View的高度 
    int itemHeight = firstVisiableChildView.getHeight();
    return (position) * itemHeight - firstVisiableChildView.getTop();
  }

搜索框执行的动画(ObjectAnimator):

animator.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {
      }

      @Override
      public void onAnimationEnd(Animator animation) {
        if (visibity == 1) {
          mMinTvSearchView.setText("搜索简书内容和朋友");
        }
        if (visibity == 0) {
          mMinTvSearchView.setText("搜索");
        }
      }

      @Override
      public void onAnimationCancel(Animator animation) {
      }

      @Override
      public void onAnimationRepeat(Animator animation) {
      }
    });
    animator.setDuration(100).start();

好了,以上就是搜索框效果的全部内容,代码中都有比较详细的注释。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多