Android仿簡(jiǎn)書搜索框效果的示例代碼
前言
之前用簡(jiǎn)書的時(shí)候一直是在web端,后來(lái)下載了客戶端,看到了搜索的那個(gè)動(dòng)畫,就嘗試的去寫了,沒(méi)寫之前感覺(jué)挺容易的,寫了之后,就感覺(jué)里面還是有些要注意的東西的。話不多說(shuō),直接上圖。
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屬性,如果不添加,當(dāng)text=“搜索簡(jiǎn)書內(nèi)容和朋友”時(shí)會(huì)有2行變1行的效果,看起來(lái)效果不太好。
頭部視圖:
<?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后的搜索框?qū)挾茸畲笾? mMaxWidth = mMaxWidth -rightMargin*2; //搜索框?qū)挾茸钚≈? mMinWidth = Px2DpUtil.dp2px(this, R.dimen.d_80); //header布局高度 mHeaderHeight=Px2DpUtil.dp2px(this,R.dimen.d_120);
RecyclerView 滾動(dòng)監(jiān)聽(tīng):
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(); //獲取第一個(gè)可見(jiàn)視圖的position int position = l.findFirstVisibleItemPosition(); //獲取第一個(gè)完全可見(jiàn)視圖的position int firstCompletelyVisibleItemPosition = l.findFirstCompletelyVisibleItemPosition(); //當(dāng)position=0時(shí),對(duì)標(biāo)題欄執(zhí)行透明度變化 if (position == 0) { //計(jì)算滾動(dòng)的距離占header高度的比例 double delta = Math.floor(((float) getScollYDistance(recyclerView) % mHeaderHeight)); //給標(biāo)題欄設(shè)置透明度 mLlTitle.getBackground().setAlpha((int) delta); } //當(dāng)position=1時(shí),搜索框最大 if (position == 1) { ObjectAnimator animator = ObjectAnimator.ofInt(new ViewWidthWrapper(mRlTitleLayout), "width", mMaxWidth); setAnimatorListener(animator,1); } //當(dāng)position=0時(shí),搜索框最小 if(firstCompletelyVisibleItemPosition==0){ ObjectAnimator animator = ObjectAnimator.ofInt(new ViewWidthWrapper(mRlTitleLayout), "width", mMinWidth); setAnimatorListener(animator,0); } } });
獲取RecycleView垂直滾動(dòng)的距離:
public int getScollYDistance(RecyclerView rv) { LinearLayoutManager layoutManager = (LinearLayoutManager) rv.getLayoutManager(); //獲取第一個(gè)可見(jiàn)item的position int position = layoutManager.findFirstVisibleItemPosition(); //獲取第一個(gè)position的View View firstVisiableChildView = layoutManager.findViewByPosition(position); //獲取第一個(gè)可見(jiàn)View的高度 int itemHeight = firstVisiableChildView.getHeight(); return (position) * itemHeight - firstVisiableChildView.getTop(); }
搜索框執(zhí)行的動(dòng)畫(ObjectAnimator):
animator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { if (visibity == 1) { mMinTvSearchView.setText("搜索簡(jiǎn)書內(nèi)容和朋友"); } if (visibity == 0) { mMinTvSearchView.setText("搜索"); } } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); animator.setDuration(100).start();
好了,以上就是搜索框效果的全部?jī)?nèi)容,代碼中都有比較詳細(xì)的注釋。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Android應(yīng)用動(dòng)態(tài)修改主題的方法示例
今天小編就為大家分享一篇關(guān)于Android應(yīng)用動(dòng)態(tài)修改主題的方法示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03android中選中菜單的顯示跳轉(zhuǎn)和隱式跳轉(zhuǎn)的實(shí)例介紹
android中選中菜單的顯示跳轉(zhuǎn)和隱式跳轉(zhuǎn)的實(shí)例介紹,需要的朋友可以參考一下2013-05-05Android自定義EditText實(shí)現(xiàn)登錄界面
這篇文章主要為大家詳細(xì)介紹了Android自定義EditText實(shí)現(xiàn)登錄界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12eclipse中運(yùn)行monkeyrunner腳本之環(huán)境搭建(4)
這篇文章主要為大家詳細(xì)介紹了eclipse中運(yùn)行monkeyrunner腳本之環(huán)境搭建的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Flutter onTap中讓你脫穎而出的5條規(guī)則
這篇文章主要為大家介紹了Flutter onTap中讓你脫穎而出的5條規(guī)則,小事情決定了你的熟練程度,這些小細(xì)節(jié)的有趣之處在于它們的豐富性2023-11-11Android用于校驗(yàn)集合參數(shù)的小封裝示例
本篇文章主要介紹了Android-用于校驗(yàn)集合參數(shù)的小封裝示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10