Recyclerview頭部吸頂布局實(shí)現(xiàn)

原先使用recylerview扯饶,配合appbarlayout+CoordinatorLayout實(shí)現(xiàn)的頭部吸頂悯姊,但是使用中發(fā)現(xiàn)嚴(yán)重bug卖毁,布局的head和內(nèi)容分離坪哄,十分難看,所以重新搞了這個(gè)實(shí)現(xiàn)势篡,代碼如下:
xml布局:
head

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:divider="@drawable/tab_h_divide"
    android:orientation="vertical"
    android:showDividers="end">

    <LinearLayout
        android:id="@+id/mTopView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
        android:orientation="vertical">
        <com.youth.banner.Banner
            android:id="@+id/mBanner"
            android:layout_width="match_parent"
            android:layout_height="122dp" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/mSelectLayout"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="@color/white"
        android:divider="@drawable/tab_h_divide"
        android:orientation="horizontal"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:showDividers="middle">

        <LinearLayout
            android:id="@+id/mAuctionSortType"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            tools:ignore="UseCompoundDrawables">

            <TextView
                android:id="@+id/mAuctionSort"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="默認(rèn)排序"
                android:textColor="@color/black_text"
                android:drawablePadding="@dimen/dip_3"
                android:drawableEnd="@mipmap/auction_home_click"
                android:textSize="@dimen/text_11_sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/mAuctionInsType"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            tools:ignore="UseCompoundDrawables">
            <TextView
                android:id="@+id/mAuctionInsTypeText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="類型"
                android:textColor="@color/black_text"
                android:drawablePadding="@dimen/dip_3"
                android:drawableEnd="@mipmap/auction_home_click"
                android:textSize="@dimen/text_11_sp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/mLeaseNewDegreeType"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            tools:ignore="UseCompoundDrawables">
            <TextView
                android:id="@+id/mLeaseNewDegreeTypeText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="新舊程度"
                android:drawableEnd="@mipmap/auction_home_click"
                android:drawablePadding="@dimen/dip_3"
                android:textColor="@color/black_text"
                android:textSize="@dimen/text_11_sp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/mLeaseBrandType"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            tools:ignore="UseCompoundDrawables">
            <TextView
                android:id="@+id/mLeaseBrandTypeText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="品牌"
                android:drawablePadding="@dimen/dip_3"
                android:drawableEnd="@mipmap/auction_home_click"
                android:textColor="@color/black_text"
                android:textSize="@dimen/text_11_sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

recyclerview的主布局:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/mRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:overScrollMode="never"
                android:scrollbars="none"
                app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
                app:spanCount="2" />
            <LinearLayout
                android:id="@+id/mSelectLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="35dp"
                    android:background="@color/white"
                    android:baselineAligned="false"
                    android:divider="@drawable/tab_h_divide"
                    android:orientation="horizontal"
                    android:paddingTop="8dp"
                    android:paddingBottom="8dp"
                    android:showDividers="middle">

                    <RelativeLayout
                        android:id="@+id/mAuctionSortType1"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        >

                        <TextView
                            android:id="@+id/mAuctionSort1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:ellipsize="end"
                            android:singleLine="true"
                            android:layout_centerInParent="true"
                            android:text="默認(rèn)排序"
                            android:textColor="@color/black_text"
                            android:drawablePadding="@dimen/dip_3"
                            android:drawableEnd="@mipmap/auction_home_click"
                            android:textSize="@dimen/text_11_sp" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/mAuctionInsType1"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        >
                        <TextView
                            android:id="@+id/mAuctionInsTypeText1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:ellipsize="end"
                            android:layout_centerInParent="true"
                            android:singleLine="true"
                            android:text="類型"
                            android:textColor="@color/black_text"
                            android:drawablePadding="@dimen/dip_3"
                            android:drawableEnd="@mipmap/auction_home_click"
                            android:textSize="@dimen/text_11_sp" />
                    </RelativeLayout>
                    <RelativeLayout
                        android:id="@+id/mLeaseNewDegreeType1"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        >
                        <TextView
                            android:id="@+id/mLeaseNewDegreeTypeText1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:ellipsize="end"
                            android:layout_centerInParent="true"
                            android:singleLine="true"
                            android:text="新舊程度"
                            android:drawableEnd="@mipmap/auction_home_click"
                            android:drawablePadding="@dimen/dip_3"
                            android:textColor="@color/black_text"
                            android:textSize="@dimen/text_11_sp" />
                    </RelativeLayout>
                    <RelativeLayout
                        android:id="@+id/mLeaseBrandType1"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        >
                        <TextView
                            android:id="@+id/mLeaseBrandTypeText1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:ellipsize="end"
                            android:layout_centerInParent="true"
                            android:singleLine="true"
                            android:text="品牌"
                            android:drawablePadding="@dimen/dip_3"
                            android:drawableEnd="@mipmap/auction_home_click"
                            android:textColor="@color/black_text"
                            android:textSize="@dimen/text_11_sp" />
                    </RelativeLayout>
                </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:background="#e4e4e4"/>
            </LinearLayout>

        </RelativeLayout>

然后就是activity或fragment中監(jiān)聽recyclerview的滑動(dòng),代碼如下:

mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val layoutManager = mRecyclerView.layoutManager as GridLayoutManager
                val view = layoutManager.findViewByPosition(0)
                if (view != null) {
                    mSelectLayout1.setVisible(view.top.absoluteValue > mHeadView.mTopView.height)
                } else {
                    mSelectLayout1.setVisible(true)
                }
            }
        })

剩下的模暗,比如選擇篩選類型要設(shè)置雙份的禁悠,切記如果使用pageadapter,請(qǐng)使用FragmentPagerAdapter兑宇,使用FragmentStatePagerAdapter會(huì)出現(xiàn)頁面不刷新的bug碍侦,有問題可以隨時(shí)聯(lián)系我!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末隶糕,一起剝皮案震驚了整個(gè)濱河市瓷产,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌枚驻,老刑警劉巖濒旦,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異再登,居然都是意外死亡尔邓,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門锉矢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來梯嗽,“玉大人,你說我怎么就攤上這事沽损〉平冢” “怎么了?”我有些...
    開封第一講書人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵绵估,是天一觀的道長炎疆。 經(jīng)常有香客問我,道長壹士,這世上最難降的妖魔是什么磷雇? 我笑而不...
    開封第一講書人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮躏救,結(jié)果婚禮上唯笙,老公的妹妹穿的比我還像新娘螟蒸。我一直安慰自己,他們只是感情好崩掘,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開白布七嫌。 她就那樣靜靜地躺著,像睡著了一般苞慢。 火紅的嫁衣襯著肌膚如雪诵原。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評(píng)論 1 300
  • 那天挽放,我揣著相機(jī)與錄音绍赛,去河邊找鬼。 笑死辑畦,一個(gè)胖子當(dāng)著我的面吹牛吗蚌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播纯出,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蚯妇,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了暂筝?” 一聲冷哼從身側(cè)響起箩言,我...
    開封第一講書人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎焕襟,沒想到半個(gè)月后陨收,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡鸵赖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年畏吓,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卫漫。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡菲饼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出列赎,到底是詐尸還是另有隱情宏悦,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布包吝,位于F島的核電站饼煞,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏诗越。R本人自食惡果不足惜砖瞧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望嚷狞。 院中可真熱鬧块促,春花似錦荣堰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至斋扰,卻和暖如春渡八,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背传货。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來泰國打工屎鳍, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人问裕。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓哥艇,卻偏偏與公主長得像,于是被迫代替她去往敵國和親僻澎。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

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