Coder Social home page Coder Social logo

recyclerstickyheaderview's Introduction

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.

recyclerstickyheaderview's People

Contributors

tellh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

recyclerstickyheaderview's Issues

Sticky header view keeps stuck.

I have 2 non sticky views and one sticky view. When i scroll to bottom and return to top sticky keeps stuck and it repeated on screen. I tried it for multiple times but it happens only on fast scrolling please find output in attachment. Thank you.
device-2018-06-16-174908
device-2018-06-16-174938

哈喽哈喽

可以做这个列子里面的吗?急急急~~~

Previous header always visible when I try to clear the list and add new element

Hello thanks for you sticky header recyler view, I got an issue if I scroll the list and then if I try to remove all element in the list and add some new one, the last stickyheader is always visible on top. If I don't scroll the list all works fine and the last sticky header don't appear

What is the good way for remove every element in the list I try to create a new adapter or use adapter.clear(recyclerView) or adapter.clearViewBinderCache() nothing works and create some new error.
I think that when I'm scrolling the last sticky header is store somewhere and when I try to delete element I don't delete this sticky header

Thanks for your help

ScrollToPosition(position) method not working properly.

I want to scroll recyclerview to particular position when list load in recyclerview.
i used two methods but not working.

  1. mRecyclerView.scrollToPosition(position);
  2. mRecyclerView.getLayoutManager().scrollToPosition(position);

Any help.

StickyHeader not keeping on top

Hi,
Testing your library before implementation, I found an issue when you perform a fast scroll up and then down. The header doesn't keep on top and doesn't show totally.

I attach an image for more information.

stickyheader issue

快速滚动会有问题 fast scroll

如果定制了不同分组吸附式标题的显示,当快速滚动的时候,bindView刷新过快,会导致固定头刷新不过来或者消失

ItemHeaderViewBinder bindView is busy

StickyHeaderView empty list on Scroll error

Process: uz.iutlab.jiznstudenta, PID: 28326
java.lang.IllegalArgumentException: maxSize <= 0
at android.util.LruCache.(LruCache.java:82)
at tellh.com.stickyheaderview_rv.StickyHeaderView$1.onScrollStateChanged(StickyHeaderView.java:72)
at android.support.v7.widget.RecyclerView.dispatchOnScrollStateChanged(RecyclerView.java:4609)
at android.support.v7.widget.RecyclerView.setScrollState(RecyclerView.java:1371)
at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2829)
at android.view.View.dispatchTouchEvent(View.java:10013)

When StickyHeaderView is empty it gives exception

Crash on Android 4.X*

Hello, I am facing issue with this library it is throwing crash on Android device with OS 4.*
can you please help or advice

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.simsim.android, PID: 3769
                                                                  android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
                                                                      at android.view.LayoutInflater.createView(LayoutInflater.java:620)
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                                                                      at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                      at tellh.com.stickyheaderview_rv.adapter.StickyHeaderViewAdapter.onCreateViewHolder(StickyHeaderViewAdapter.java:54)
                                                                      at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
                                                                      at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
                                                                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
                                                                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
                                                                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
                                                                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
                                                                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
                                                                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
                                                                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
                                                                      at android.view.View.layout(View.java:14817)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                      at tellh.com.stickyheaderview_rv.StickyHeaderView.onLayout(StickyHeaderView.java:161)
                                                                      at android.view.View.layout(View.java:14817)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                      at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
                                                                      at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
                                                                      at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:869)
                                                                      at android.view.View.layout(View.java:14817)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                      at android.view.View.layout(View.java:14817)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                                                                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                                                                      at android.view.View.layout(View.java:14817)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                      at android.view.View.layout(View.java:14817)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                                                                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                                                                      at android.view.View.layout(View.java:14817)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                      at android.view.View.layout(View.java:14817)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                  	at an

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.