原先使用recylerview,配合appbarlayout+CoordinatorLayout实现的头部吸顶,但是使用中发现严重bug,布局的head和内容分离,十分难看,所以重新搞了这个实现,代码如下:
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="默认排序"
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="默认排序"
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中监听recyclerview的滑动,代码如下:
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)
}
}
})
剩下的,比如选择筛选类型要设置双份的,切记如果使用pageadapter,请使用FragmentPagerAdapter,使用FragmentStatePagerAdapter会出现页面不刷新的bug,有问题可以随时联系我!
网友评论