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

作者:人言落日是天涯 时间:2023-06-18 16:02:58 

前言

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

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

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行的效果,看起来效果不太好。

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

头部视图:


<?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>

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

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

来源:http://www.jianshu.com/p/4419f6448d88?utm_source=tuicool&utm_medium=referral

标签:Android,搜索框
0
投稿

猜你喜欢

  • Java通过XPath获取XML文件中符合特定条件的节点

    2023-01-19 07:42:34
  • Java面向对象之猜拳游戏

    2022-10-29 21:26:45
  • java压缩zip文件中文乱码问题解决方法

    2022-12-20 11:14:25
  • 详解SpringBoot注解读取配置文件的方式

    2023-08-05 02:51:16
  • maven仓库中心mirrors配置多个下载中心(执行最快的镜像)

    2023-05-04 22:08:33
  • Java使用RedisTemplate如何根据前缀获取key列表

    2023-03-05 01:50:40
  • Android launcher中模拟按home键的实现

    2023-03-25 02:33:48
  • Android离线Doc文档访问速度慢的有效解决方法

    2021-06-27 02:10:39
  • Windows系统中使用C#读取文本文件内容的小示例

    2023-05-05 20:27:08
  • android实现文本复制到剪切板功能(ClipboardManager)

    2023-11-28 17:40:31
  • Android 使用 Scroller 实现平滑滚动功能的示例代码

    2022-01-20 22:49:35
  • Flutter加载图片流程之ImageCache源码示例解析

    2023-07-04 01:56:23
  • c# 常见文件路径Api的使用示例

    2023-06-03 21:06:12
  • 分布式医疗挂号系统SpringCache与Redis为数据字典添加缓存

    2023-06-28 02:26:55
  • android fm单体声和立体声的切换示例代码

    2023-04-19 11:06:56
  • AndroidStudio项目打包成jar的简单方法

    2023-07-07 05:33:27
  • springboot应用访问zookeeper的流程

    2021-11-28 00:34:56
  • C#使用集合实现二叉查找树

    2023-06-01 06:17:21
  • Android个人手机通讯录开发详解

    2023-01-22 01:21:30
  • eclipse 如何创建 user library 方法详解

    2021-12-29 00:33:56
  • asp之家 软件编程 m.aspxhome.com