StickyHeaderRecyclerView,悬浮吸顶 Header,支持点击事件与状态绑定

[复制链接]
来自: smuyyh 分类: Android精品源码 上传时间: 2019-9-24 19:52:40
Tag:

项目介绍:

[i=s] 本帖最后由 smuyyh 于 2019-9-24 19:55 编辑 [/i]

github源码地址:https://github.com/smuyyh/StickyHeaderRecyclerView

RecyclerView 悬浮吸顶 Header,支持点击事件与状态绑定

<img src="https://github.com/smuyyh/StickyHeaderRecyclerView/blob/master/art/screenshot.gif" width="216" height="400" ></img>

依赖

dependencies {
    compile 'com.yuyang:stickyheaders:1.0.1'
}

用法

1. Header Model

Header Model 需要实现 StickyHeaderModel 接口

public class HeaderItem implements StickyHeaderModel {

    public final String title;

    /**
     * 状态保存示例,如果header存在一些交互性行为,在onBindViewHolder里面需要绑定悬浮header的状态
     */
    public int color = 0xff777777;

    public HeaderItem(String title) {
        this.title = title;
    }
}

public class Item {

    public final String title;
    public final String message;

    public Item(String title, String message) {
        this.title = title;
        this.message = message;
    }
}

2. Adapter

RecyclerView Adapter 需要实现 AdapterDataProvider 接口,并在 getAdapterData() 返回 model 数据,用于判断对应 position 是否为 Header

public final class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.BaseViewHolder> implements AdapterDataProvider {

    private final List<Object> dataList = new ArrayList<>();

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false));
        } else {
            return new HeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position) instanceof Item ? 0 : 1;
    }

    @Override
    public List<?> getAdapterData() {
        return dataList;
    }

    private static final class ItemViewHolder extends BaseViewHolder {

        TextView titleTextView;
        TextView messageTextView;

        ItemViewHolder(View itemView) {
            super(itemView);

            titleTextView = itemView.findViewById(R.id.tv_title);
            messageTextView = itemView.findViewById(R.id.tv_message);
        }
    }

    private static final class HeaderViewHolder extends BaseViewHolder {

        TextView titleTextView;
        TextView button;

        HeaderViewHolder(View itemView) {
            super(itemView);

            titleTextView = itemView.findViewById(R.id.tv_title);
            button = itemView.findViewById(R.id.button);
        }
    }

    static class BaseViewHolder extends RecyclerView.ViewHolder {

        BaseViewHolder(View itemView) {
            super(itemView);
        }
    }
}

3. Setup

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new StickyLinearLayoutManager(this, adapter)); // StickyLinearLayoutManager 替代 LinearLayoutManager

RecyclerAdapter adapter = new RecyclerAdapter();
adapter.setDataList(genDataList(0));
recyclerView.setAdapter(adapter);

4. Features

(4.1) Header Attach Listener

stickyLinearLayoutManager.setStickyHeaderListener(new StickyLinearLayoutManager.StickyHeaderListener() {
    @Override
    public void headerAttached(View headerView, int adapterPosition) {
        Log.d("StickyHeaderRecyclerView", "Header Attached : " + adapterPosition);
    }

    @Override
    public void headerDetached(View headerView, int adapterPosition) {
        Log.d("StickyHeaderRecyclerView", "Header Detached : " + adapterPosition);
    }
});

(4.2) Elevation

layoutManager.elevateHeaders(true); // default value : 5dp

// or
layoutManager.elevateHeaders(dpValue);

相关源码推荐:

我来说两句
所有评论(7)
allow.xuweibin 2019-9-25 09:48:37
感谢分享,安卓巴士有你更精彩:lol
回复
乱世佳人520 2019-9-25 09:54:57
感觉楼主很用心,辛苦啦~
回复
myself2015 2019-9-25 09:55:37
安卓巴士是个不错的网站,我来顶个贴~
回复
无限释囚 2019-9-25 09:56:37
感谢分享,楼主V5~
回复
ff12345 2019-9-25 10:02:18
不错不错,楼主辛苦了。。。
回复
einnius2012 2019-9-25 10:04:33
感谢分享,安卓巴士有你更精彩:lol
回复
fqfantasy 4 天前
感谢分享,安卓巴士有你更精彩:)
回复
282 1 0
代码贡献英雄榜
用户名 下载数
联系我们
首页/微信公众账号投稿
帖子代码编辑/版权问题
QQ:435399051,1294855032
如何获得代码达人称号?
如何成为签约作者?
领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )