自定義View練習(xí)——自定義StickyHeadLayout

NestedScrollingParent接口提供了八個(gè)接口,按照順序大致可以這么來記:
start-->accepted-->preScroll-->scroll-->preFling-->fling-->stop

    boolean onStartNestedScroll( View child,  View target,  int axes);

    void onNestedScrollAccepted( View child, View target,  int axes);

    void onNestedPreScroll(View target, int dx, int dy,  int[] consumed);

    void onNestedScroll( View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed);

    boolean onNestedPreFling(View target, float velocityX, float velocityY);

    boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed);

    void onStopNestedScroll( View target);

    int getNestedScrollAxes();
StickyLayout.gif

如果不使用系統(tǒng)提供的CoordinatorLayout以及AppbarLayout窝撵,那么該怎么實(shí)現(xiàn)頭部懸停View呢豌汇?
NestedScrollingParent與NestedScrollingChild接口配合使用就能達(dá)到想要的效果郎楼。由于系統(tǒng)提供的諸如NestedScrollView岔霸,RecyclerView等都實(shí)現(xiàn)了NestedScrollingChild接口褒繁,因此我們只需要實(shí)現(xiàn)一個(gè)NestedScrollingParent接口举哟,來配合NestedScrollView來使用就可以了思劳。

先看一下布局

<com.goodsnow.view.custom.nestedscroll.MySimpleStickyLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:ignore="MissingDefaultResource">

    <TextView
        android:textColor="#ffffff"
        android:textSize="50dp"
        android:gravity="center"
        android:id="@+id/tvTop"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        android:text="Top" />

    <TextView
        android:id="@+id/tvMiddle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="stickyHeady"
        android:textColor="@android:color/white"
        android:textSize="20dp" />

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:gravity="center"
                android:text="scrollView"
                android:textColor="@android:color/white"
                android:textSize="40dp"
                android:layout_width="match_parent"
                android:layout_height="800dp"
                android:background="@color/design_default_color_error" />

            <TextView
                android:gravity="center"
                android:text="scrollView"
                android:textColor="@android:color/white"
                android:textSize="40dp"
                android:layout_width="match_parent"
                android:layout_height="800dp"
                android:background="@color/design_default_color_primary" />


        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</com.goodsnow.view.custom.nestedscroll.MySimpleStickyLayout>

對于MySimpleStickyLayout的代碼來說,主要分為三部分妨猩。第一部分是測量潜叛,第二部分是重寫接口,第三部分檢測邊界壶硅。

一:測量
  @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTop = findViewById(R.id.tvTop);
        mMiddle = findViewById(R.id.tvMiddle);
        mBottom = findViewById(R.id.scrollView);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ViewGroup.LayoutParams layoutParams = mBottom.getLayoutParams();
        layoutParams.height = getMeasuredHeight() - mMiddle.getMeasuredHeight();
        setMeasuredDimension(getMeasuredWidth(), mTop.getMeasuredHeight() + mMiddle.getMeasuredHeight() + mBottom.getMeasuredHeight());
    }

因?yàn)镸ySimpleStickyLayout如果不重新測量的話钠导,那么他的高度就只有屏幕的高度震嫉,而當(dāng)我們向上滑動(dòng)時(shí),底部就會(huì)出現(xiàn)空白沒有展示View的區(qū)域牡属。為了將該區(qū)域進(jìn)行繪制票堵,因此進(jìn)行了重新測量。

二:重寫接口
    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        return true;
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(target, dx, dy, consumed);

      boolean hide = dy >= 0 && getScrollY() < mTop.getHeight();
       boolean show = dy <= 0 && getScrollY() >= 0&& !ViewCompat.canScrollVertically(target, -1);
        if (hide || show) {
            scrollBy(0, dy);
            consumed[1] = dy;
        }
    }

onStartNestedScroll()返回true是因?yàn)樾枰?dāng)前View進(jìn)行攔截逮栅。在onNestedPreScroll()中進(jìn)行了是否消費(fèi)的計(jì)算悴势。其中mScrollY如果為正值,則表示向上移動(dòng)措伐,mScrollX為正值特纤,則表示向左移動(dòng)。dy為正值侥加,也表示向上移動(dòng)捧存。

三:邊界檢查
    @Override
    public void scrollTo(int x, int y) {
        if(y<0){
            y=0;
        }
        if(y>mTop.getMeasuredHeight()){
            y=mTop.getMeasuredHeight();
        }
        if(y!=getScrollY()){
            super.scrollTo(x, y);

        }
    }

內(nèi)容參考:鴻洋的博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市担败,隨后出現(xiàn)的幾起案子昔穴,更是在濱河造成了極大的恐慌,老刑警劉巖提前,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吗货,死亡現(xiàn)場離奇詭異,居然都是意外死亡狈网,警方通過查閱死者的電腦和手機(jī)宙搬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來拓哺,“玉大人勇垛,你說我怎么就攤上這事∈颗福” “怎么了窥摄?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長础淤。 經(jīng)常有香客問我,道長哨苛,這世上最難降的妖魔是什么鸽凶? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮建峭,結(jié)果婚禮上玻侥,老公的妹妹穿的比我還像新娘。我一直安慰自己亿蒸,他們只是感情好凑兰,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布掌桩。 她就那樣靜靜地躺著,像睡著了一般姑食。 火紅的嫁衣襯著肌膚如雪波岛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天音半,我揣著相機(jī)與錄音则拷,去河邊找鬼。 笑死曹鸠,一個(gè)胖子當(dāng)著我的面吹牛煌茬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播彻桃,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼坛善,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了邻眷?” 一聲冷哼從身側(cè)響起眠屎,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎耗溜,沒想到半個(gè)月后组力,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡抖拴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年燎字,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阿宅。...
    茶點(diǎn)故事閱讀 40,110評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡候衍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出洒放,到底是詐尸還是另有隱情蛉鹿,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布往湿,位于F島的核電站妖异,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏领追。R本人自食惡果不足惜他膳,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望绒窑。 院中可真熱鬧棕孙,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至肢预,卻和暖如春矛洞,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背误甚。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工缚甩, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人窑邦。 一個(gè)月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓擅威,卻偏偏與公主長得像,于是被迫代替她去往敵國和親冈钦。 傳聞我的和親對象是個(gè)殘疾皇子郊丛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評論 2 355

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