• Stars
    star
    347
  • Rank 122,141 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sticky header view or suspending view for RecyclerView.

RecyclerStickyHeaderView


Sticky header view or suspending view for RecyclerView.
StickyListHeaders is an Android library that makes it easy to integrate section headers stick to the top in ListView. Inspire by it, I setup this project to implement the same effect in RecyclerView.

Effect

Usage

Setup

root build.gradle

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

app build.gradle

dependencies {
   compile 'com.github.TellH:RecyclerStickyHeaderView:1.1.0'
}

Quick Start

  • Place RecylerView into StickyHeaderView
    <tellh.com.stickyheaderview_rv.StickyHeaderView
        android:id="@+id/stickyHeaderView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:scrollbars="vertical" />
    </tellh.com.stickyheaderview_rv.StickyHeaderView>
  • Create data bean class for each item type in RecyclerView. They should extend DataBean. Override the method
    public boolean shouldSticky() to decide whether the item view should be suspended on the top.
public class User extends DataBean {
    private String login;
    private int id;
    private String avatar_url;
    private boolean shouldSticky;
    @Override
    public int getItemLayoutId(StickyHeaderViewAdapter adapter) {
        return R.layout.item_user;
    }
    public void setShouldSticky(boolean shouldSticky) {
        this.shouldSticky = shouldSticky;
    }
    // Decide whether the item view should be suspended on the top.
    @Override
    public boolean shouldSticky() {
        return shouldSticky;
    }
}
public class ItemHeader extends DataBean {
    private String prefix;
    @Override
    public int getItemLayoutId(StickyHeaderViewAdapter adapter) {
        return R.layout.header;
    }
    @Override
    public boolean shouldSticky() {
        return true;
    }
}
  • Create ViewBinder to bind different type views with specific data beans. As you see, provideViewHolder(View itemView) corresponds for onCreateViewHolder in RecyclerView, and bindView corresponds for onBindViewHolder in RecyclerView.
public class ItemHeaderViewBinder extends ViewBinder<ItemHeader, ItemHeaderViewBinder.ViewHolder> {
    @Override
    public ViewHolder provideViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }
    @Override
    public void bindView(StickyHeaderViewAdapter adapter, ViewHolder holder, int position, ItemHeader entity) {
        holder.tvPrefix.setText(entity.getPrefix());
    }
    @Override
    public int getItemLayoutId(StickyHeaderViewAdapter adapter) {
        return R.layout.header;
    }
    static class ViewHolder extends ViewBinder.ViewHolder {
        TextView tvPrefix;
        public ViewHolder(View rootView) {
            super(rootView);
            this.tvPrefix = (TextView) rootView.findViewById(R.id.tv_prefix);
        }
    }
}
  • Instantiate StickyHeaderViewAdapter for RecyclerView and register ViewBinders for each item types.
        rv = (RecyclerView) findViewById(R.id.recyclerView);
        rv.setLayoutManager(new LinearLayoutManager(this));
        List<DataBean> userList = new ArrayList<>();
        adapter = new StickyHeaderViewAdapter(userList)
                .RegisterItemType(new UserItemViewBinder())
                .RegisterItemType(new ItemHeaderViewBinder());
        rv.setAdapter(adapter);

That is all.

Please check out the Demo and source code for more information. If you have any question, feel free to raise an issue. Thanks a lot!

Thanks

License

Copyright 2016 TellH

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

GitClub

An elegent Android Client for Github. 不仅仅是Github客户端,而且是一个发现优秀Github开源项目的app
Java
658
star
2

RecyclerTreeView

TreeView implement in Android with RecyclerView.
Java
638
star
3

Android_PullToRefreshLibrary_Collection

Android下拉刷新开源库集锦,提供源码用于学习和研究
Java
240
star
4

AutoGo

Ease your code, easy go!
Java
122
star
5

FragmentLazyLoading

仿微信的Viewpager-Fragment惰性加载,延迟加载,lazy-loading
Java
70
star
6

NoListAdapter

A more elegant and easy way to build an multifunctional adapter for ListView or RecyclerView in Android.
Java
37
star
7

RecyclerViewDemo

Demo for study RecyclerView
Java
36
star
8

RxJavaDemo

学习RxJava
Java
33
star
9

Interview-Note

This is my campus interview note for Android. 我的校招Android面试笔记
25
star
10

AccessInline

An Android gradle plugin to inline methods that start with the prefix 'access$' in bytecode.
Java
20
star
11

Android-Simple-Common-PullToRefreshLayout

This is a light-weight Android PullToRefreshLayout, can be applied to any scrollable view.
Java
20
star
12

FloatingActionButtonPro

Extend from FloatingActionButton for Android
Java
8
star
13

HuffmanExtract

哈夫曼压缩,对应博文:http://blog.csdn.net/TellH/article/details/51285750
C++
8
star
14

AndroidStudioTemplate

Generate Template Files automatically in your Android Studio project.
FreeMarker
4
star
15

AlgorithmStudy

算法Demo
Java
4
star
16

AndroidLibraryLatestVersion

Check out Latest Version of some common libraries in Android developing.
1
star
17

AndroidUnitTestPractice

Android单元测试实践
Java
1
star
18

LearningCompile

探索编译原理
HTML
1
star