Giter Site home page Giter Site logo

stickydecoration's Introduction

StickyDecoration

利用RecyclerView.ItemDecoration实现顶部悬浮效果

效果

支持

  • LinearLayoutManager
  • GridLayoutManager
  • 点击事件
  • 分割线

添加依赖

项目要求: minSdkVersion >= 14. 在你的build.gradle中 :

repositories {
    jcenter()// If not already there
}
dependencies {
    compile 'com.gavin.com.library:stickyDecoration:1.5.2'
}

最新版本

使用

文字悬浮——StickyDecoration

注意 使用recyclerView.addItemDecoration()之前,必须先调用recyclerView.setLayoutManager();

代码:

GroupListener groupListener = new GroupListener() {
    @Override
    public String getGroupName(int position) {
        //获取分组名
        return mList.get(position).getProvince();
    }
};
StickyDecoration decoration = StickyDecoration.Builder
        .init(groupListener)
        //重置span(使用GridLayoutManager时必须调用)
        //.resetSpan(mRecyclerView, (GridLayoutManager) manager)
        .build();
...
mRecyclerView.setLayoutManager(manager);
//需要在setLayoutManager()之后调用addItemDecoration()
mRecyclerView.addItemDecoration(decoration);

效果:

LinearLayoutManager

GridLayoutManager

支持的方法:

方法 功能 默认
setGroupBackground 背景色 #48BDFF
setGroupHeight 高度 120px
setGroupTextColor 字体颜色 Color.WHITE
setGroupTextSize 字体大小 50px
setDivideColor 分割线颜色 #CCCCCC
setDivideHeight 分割线高宽度 0
setTextSideMargin 边距(靠左时为左边距 靠右时为右边距) 10
setHeaderCount 头部Item数量(仅LinearLayoutManager) 0
方法 功能 描述
setOnClickListener 点击事件 设置点击事件,返回当前分组下第一个item的position
resetSpan 重置 使用GridLayoutManager时必须调用

自定义View悬浮——PowerfulStickyDecoration

先创建布局item_group

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/ll"
      android:orientation="horizontal"
      ...>

    <ImageView
        android:id="@+id/iv"
        .../>

    <TextView
        android:id="@+id/tv"
        .../>
</LinearLayout>

创建PowerfulStickyDecoration,实现自定View悬浮

PowerGroupListener listener = new PowerGroupListener() {
    @Override
    public String getGroupName(int position) {
        return mList.get(position).getProvince();
    }

    @Override
    public View getGroupView(int position) {
        //获取自定定义的组View
        View view = getLayoutInflater().inflate(R.layout.item_group, null, false);
        ((TextView) view.findViewById(R.id.tv)).setText(mList.get(position).getProvince());
        return view;
    }
};
PowerfulStickyDecoration decoration = PowerfulStickyDecoration.Builder
        .init(listener)
         //重置span(注意:使用GridLayoutManager时必须调用)
        //.resetSpan(mRecyclerView, (GridLayoutManager) manager)
        .build();

  ...
mRecyclerView.addItemDecoration(decoration);

效果:

效果

支持的方法:

方法 功能 默认
setGroupHeight 高度 120px
setGroupBackground 背景色 #48BDFF
setDivideColor 分割线颜色 #CCCCCC
setDivideHeight 分割线高宽度 0
setCacheEnable 是否使用缓存 使用缓存
setHeaderCount 头部Item数量(仅LinearLayoutManager) 0
方法 功能 描述
setOnClickListener 点击事件 设置点击事件,返回当前分组下第一个item的position以及对应的viewId
resetSpan 重置span 使用GridLayoutManager时必须调用
notifyRedraw 通知重新绘制 使用场景:网络图片加载后调用方法使用)
clearCache 清空缓存 在使用缓存的情况下,数据改变时需要清理缓存

Tips

1、若使用网络图片时,在图片加载完成后需要调用

decoration.notifyRedraw(mRv, view, position);

2、使用缓存时,若数据源改变,需要调用clearCache清除数据

3、点击事件穿透问题,参考demo中MyRecyclerView。issue47

更新日志

----------------------------- 1.5.2 (2019-9-3)----------------------------

  • fix:特殊情况下,吸顶效果不佳问题

----------------------------- 1.5.1 (2019-8-8)----------------------------

  • fix:setHeaderCount导致显示错乱问题

----------------------------- 1.5.0 (2019-6-17)----------------------------

  • fix:GridLayoutManager刷新后数据混乱问题

----------------------------- 1.4.12 (2019-5-8)----------------------------

  • fix:setDivideColor不生效问题

----------------------------- 1.4.9 (2018-10-9)----------------------------

  • fix:由于添加header导致的一些问题

----------------------------- 1.4.8 (2018-08-26)----------------------------

  • 顶部悬浮栏点击事件穿透问题:提供处理方案

----------------------------- 1.4.7 (2018-08-16)----------------------------

  • fix:数据变化后,布局未刷新问题

----------------------------- 1.4.6 (2018-07-29)----------------------------

  • 修改缓存方式
  • 加入性能检测

----------------------------- 1.4.5 (2018-06-17)----------------------------

  • 在GridLayoutManager中使用setHeaderCount方法导致布局错乱问题

----------------------------- 1.4.4 (2018-06-2)----------------------------

  • 添加setHeaderCount方法
  • 修改README
  • 修复bug

----------------------------- 1.4.3 (2018-05-27)----------------------------

  • 修复一些bug,更改命名

----------------------------- 1.4.2 (2018-04-2)----------------------------

  • 增强点击事件,现在可以得到悬浮条内View点击事件(没有设置id时,返回View.NO_ID)

  • 修复加载更多返回null崩溃或出现多余的悬浮Item问题(把加载更多放在Item中的加载方式)

----------------------------- 1.4.1 (2018-03-21)----------------------------

  • 默认取消缓存,避免数据改变时显示出问题

  • 添加clearCache方法用于清理缓存

----------------------------- 1.4.0 (2018-03-04)----------------------------

  • 支持异步加载后的重新绘制(如网络图片加载)

  • 优化缓存

  • 优化GridLayoutManager的分割线

----------------------------- 1.3.1 (2018-01-30)----------------------------

  • 修改测量方式

----------------------------- 1.3.0 (2018-01-28)----------------------------

  • 删除isAlignLeft()方法,需要靠右时,直接在布局中处理就可以了。

  • 优化缓存机制。

stickydecoration's People

Contributors

gavin-zyx avatar gavin-zyx2 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.