手撸了个很容易实现京东购物车吸顶功能的Android库

3,001 阅读1分钟

手撸了很容易实现京东购物车吸顶功能的Android库,奉上京东购物车截图。后续会给出更多关于该库的文章。敬请关注。

京东购物车吸顶图


介绍

StickyHeaderForRecyclerView库主要的功能是方便RecyclerView实现吸顶功能。它目前支持以下功能:

  1. 支持单类型吸顶功能
  2. 支持多类型吸顶功能
  3. 支持开启和关闭吸顶功能
  4. 支持指定位置吸顶功能
  5. 支持设置吸顶偏移量
  6. 支持自定义RecyclerView上Item吸顶边界自定义
  7. 可以无缝配合AppBarLayout

效果

  • 支持单类型吸顶功能

支持单类型吸顶功能

  • 支持多类型吸顶功能

支持多类型吸顶功能

  • 支持开启和关闭吸顶功能

支持开启和关闭吸顶功能

  • 支持指定位置吸顶功能

支持指定位置吸顶功能

  • 支持设置吸顶偏移量

支持设置吸顶偏移量

  • 支持自定义RecyclerView上Item吸顶边界自定义

支持自定义RecyclerView上Item吸顶边界自定义

使用教程

  1. 安装依赖
  • 项目build.gradle增加maven地址
allprojects {
    repositories {
        jcenter()
        maven {
            url "https://dl.bintray.com/xuanyudaddy/sticky-header-recyclerview"
        }
        google()
    }
    tasks.withType(Javadoc) {
        enabled = false
    }
}
  • app目录下build.gradle增加依赖
dependencies {
   implementation 'com.xuanyu.stickyheader:stickyheader:1.0.0'
 }
  1. 布局文件增加吸顶Layout,header.layout为吸顶布局的占位布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler.view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>


    <FrameLayout
        android:id="@+id/header.layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></FrameLayout>

     
     
</FrameLayout>
  1. 创建BaseStickyHeaderModel的子类,泛型对应的Book为RecyclerView ItemView对应的实体类
public class BookStickyHeaderModel extends BaseStickyHeaderModel<Book> {

    @Override
    public View getStickyView(Context context) {
        System.out.println("jiangbin getView");
        BookView bookView = new BookView(context);
        return bookView;
    }

    @Override
    public void onBindView(View view, Book data) {

        ((BookView) view).setData(new Book(data.name + "吸顶"));
    }
}

  1. 在RecyclerView#setAdapter之后,初始化吸顶代码。需要调用StickyHeaderRegistry.registerTransfer将需要吸顶Item对应的Bean和它对应的StickyHeaderModel一一对应起来
 StickyHeaderHelper.init(mRecyclerView, mHeaderLayout, 0);
 StickyHeaderRegistry.registerTransfer(Book.class, BookStickyHeaderModel.class);
  1. 结束!Enjoy it!!

欢迎您访问github项目地址github.com/lizijin/Sti…,如果您使用本库,请提出您的宝贵意见。