原先使用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)系我!