自定義Behavior實(shí)現(xiàn)上滑下滑那些事

最近有群友提出一些效果,這里做些簡單介紹:頂部topbar 底部幾個tab 中間recyclerview,好了,recyclerview上滑topbar和tab隱藏,下滑topbar和tab顯示,說到這,相信不少讀者們已經(jīng)想到了方法,那就是監(jiān)聽recyclerview,根據(jù)滑動的距離或者趨勢去顯示隱藏自己的view,這當(dāng)然是個辦法,而且實(shí)現(xiàn)起來也很快,但是這里呢,為了讓大家能去學(xué)習(xí)更多新的知識,與時俱進(jìn)嘛,所以介紹了5.0以上的Material風(fēng)格,具體怎么做,下面細(xì)細(xì)道來:

step1:添加compile依賴

    compile 'com.android.support:design:25.3.1'

step2:布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.zlx.zlxbasecv.MainActivity">

<include layout="@layout/appbar_main"/>

<include layout="@layout/content_main" />

<include layout="@layout/footer_main"/>

</android.support.design.widget.CoordinatorLayout>

<include layout="@layout/appbar_main"/> 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" /> 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:onClick="CoorClick"
    android:text="你是誰?你從哪里來?你到哪里去?"/>
</android.support.v4.widget.NestedScrollView>

<include layout="@layout/footer_main"/>布局:

此布局中,大家可以看到父布局有個自定義的behavior:app:layout_behavior="com.example.zlx.zlxbasecv.custom_view.cus_behavior.FooterBehavior" 在下面會提供

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
app:layout_behavior="com.example.zlx.zlxbasecv.custom_view.cus_behavior.FooterBehavior"
android:background="?attr/colorPrimary">


<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="Tab1"
    android:layout_weight="1"
    android:gravity="center"
    android:textColor="@android:color/white"/>
<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="Tab2"
    android:layout_weight="1"
    android:gravity="center"
    android:textColor="@android:color/white"/>
<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:text="Tab3"
    android:layout_weight="1"
    android:gravity="center"
    android:textColor="@android:color/white"/>

</LinearLayout>

布局都寫完了,接下來介紹下自定義behavior:新建個FooterBehavior 繼承 CoordinatorLayout.Behavior,重寫里面的幾個方法:

情況一:屬性的綁定

  • layoutDependsOn:根據(jù)返回的布爾值判斷兩個view是否形成綁定關(guān)系。
  • onDependentViewChanged:完成兩件事

根據(jù)View dependency的狀態(tài)改變V child的狀態(tài)
返回true,表示已經(jīng)改變V child的狀態(tài)。這個返回值給誰用?

情況二:滾動的綁定

onStartNestedScroll,返回true表示子類可以觸發(fā)nested scroll。其中參數(shù)int nestedScrollAxes表示當(dāng)前滾動方向。例如,return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;表示滾動方向是垂直的才觸發(fā)nest scroll。
onNestedPreScroll,onStartNestedScroll返回true會觸發(fā)這個函數(shù)。這個函數(shù)的參數(shù)是坐標(biāo)相關(guān)的,可以根據(jù)滑動距離寫其他View的響應(yīng)邏輯。

public class FooterBehavior extends CoordinatorLayout.Behavior<View>{

public FooterBehavior(Context context, AttributeSet attributeSet){
    super(context,attributeSet);
}

/**
 * 依賴條件,true表示綁定關(guān)系成立
 * @param parent
 * @param child
 * @param dependency
 * @return
 */
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
    return dependency instanceof AppBarLayout;
}

/**
 * 屬性依賴邏輯,返回true表示要執(zhí)行
 * @param parent
 * @param child
 * @param dependency
 * @return
 */
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    float scaleY = Math.abs(dependency.getY()) / dependency.getHeight();
    child.setTranslationY(child.getHeight() * scaleY);
    return true;
}
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,316評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,481評論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,241評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,939評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,697評論 6 409
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,182評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,247評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,406評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,933評論 1 334
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,772評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,973評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,516評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,209評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,638評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,866評論 1 285
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,644評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,953評論 2 373

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,637評論 25 708
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 46,844評論 22 665
  • 簡介: 提供一個讓有限的窗口變成一個大數(shù)據(jù)集的靈活視圖。 術(shù)語表: Adapter:RecyclerView的子類...
    酷泡泡閱讀 5,199評論 0 16
  • 清晨。 第一縷陽光,透過落地窗灑進(jìn)來。 滿室的空氣,還透著那一絲絲曖昧的氣息。床上那嬌小的身軀,絲毫沒有因?yàn)殛柟獾?..
    糖心蘋果閱讀 292評論 1 2
  • By 丁彥清 親愛的她蜜,之前在和你們聊天的時候看到你們對自己的CUP有著復(fù)雜的感情。(原諒我作為一個男生實(shí)在不好...
    她生活閱讀 1,384評論 0 4