A ListView with pinned section headers for Android

Overview

PinnedHeaderListView

This library provides a sectioned ListView with pinned headers. It looks and feels much like the default contacts app does on Android 4.0 and above

A custom adapter is provided which must be extended and used with the custom ListView.

The usage of this library is simple. You need to create an adapter that extends SectionedBaseAdapter.

There are 6 methods that need to be overridden:

  • public Object getItem(int section, int position);
  • public long getItemId(int section, int position);
  • public int getSectionCount();
  • public int getCountForSection(int section);
  • public View getItemView(int section, int position, View convertView, ViewGroup parent);
  • public View getSectionHeaderView(int section, View convertView, ViewGroup parent);

getItemView and getSectionHeaderView should be treated as you would the getItemView method from a normal Adapter. The same goes for getItem and getItemId.

getSectionCount and getCountForSection replace the getCount() method from a standard adapter. You should return the number of sections in your list in getSectionCount and the number of items in a section in getCountForSection.

In addition to these methods, there are a few others you may override:

  • public int getItemViewType(int section, int position);
  • public int getItemViewTypeCount();
  • public int getSectionHeaderViewType(int section);
  • public int getSectionHeaderViewTypeCount();

These replace the getViewTypeCount() and getItemViewType(int position) methods from a standard adapter

Note that you can return the same ItemViewType for a header and a non header and these will be cached seperately by the underlying ListView.
That is, you will never get a header view passed in as the convertView in public View getItemView(int section, int position, View convertView, ViewGroup parent);, nor will you get an item view passed in as the convertView in public View getSectionHeaderView(int section, View convertView, ViewGroup parent);

See the provided example for more details.

License

Copyright (c) 2012, James Smith
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL James Smith BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Comments
  • Error when addHeaderview

    Error when addHeaderview

    hi JimiSmith, I'm using your PinnedHeaderListView class and it works well, but when I add a headerview it doesn't work what i want, the header view which I added is replaced by a blank section header view. below is my sectionadapter class code, and what should I do. thanks.

    public View getItemView(int section, int position, View convertView, ViewGroup parent) {
        ItemViewHolder holder = null;
        if (convertView == null) {
            holder = new ItemViewHolder();
            convertView = (LinearLayout) this.mInflater.inflate(R.layout.view_add_friend_item, null);
            holder.headimg = (ImageView)convertView.findViewById(R.id.iv_addfriend_avater);
            holder.nickname = (TextView)convertView.findViewById(R.id.tv_addfriend_friend_name);
            holder.desc = (TextView)convertView.findViewById(R.id.tv_addfriend_friend_des);
            holder.addbtn = (Button)convertView.findViewById(R.id.bt_addfriend_add);
    
            convertView.setTag(holder);  
        } else {
            holder = (ItemViewHolder) convertView.getTag();
        }
        UserSection currentSection = sectionList.get(section);
        List<UserInfo> userList = currentSection.getUserList();
        UserInfo currentUser = userList.get(position);
        holder.nickname.setText(currentUser.getName());
        Utils.setPortrait(this.mContext, currentUser.getHeaderS(), holder.headimg);
    
        return convertView;
    }
    
    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        HeaderViewHolder holder = null;
        if (convertView == null) {
            holder = new HeaderViewHolder();
            LayoutInflater inflator = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = (LinearLayout) inflator.inflate(R.layout.item_section_adapter_header, null);
            holder.title = (TextView)convertView.findViewById(R.id.tv_section_title);
            convertView.setTag(holder);
    
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
        UserSection currentSection = sectionList.get(section);
        holder.title.setText(currentSection.getSectionTitle());
        return convertView;
    }
    
    opened by windjp 8
  • why i can't add header for list view

    why i can't add header for list view

    hi, i can't add view for list view, like this : mListView.addFooter(layout); the error : java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to za.co.immedia.pinnedheaderlistview.SectionedBaseAdapter

    please check this and fix it

    opened by alahammad 7
  • tag return null from getView !!

    tag return null from getView !!

    Hi jimi, when setTag in getView function and return it from activity return null ?! can check what happend . sorry i don't have log just null pointer exception ..

    opened by alahammad 4
  • add pull to refresh?

    add pull to refresh?

    Hello , First thanks for your library i loved it :+1: second i want to implement pull to refresh class to your library but sections and positions not correct.. can you help me with this issue

    thanks ..

    opened by alahammad 3
  • Is it safe to replace android-support-v4.jar with latest's

    Is it safe to replace android-support-v4.jar with latest's

    From time to time, Google will release an updated version of android-support-v4.jar. I was wondering, is it safe for me, to just simply replace the old PinnedHeaderListView-master\library\libs\android-support-v4.jar with the latest?

    Thanks.

    opened by yccheok 1
  • Making the library generic like ArrayAdapter<T>

    Making the library generic like ArrayAdapter

    Hi Jimi Smith,

    First of all, I really would like to thank you for creating such a great library. Although I had looked into several tutorials to do my own implementation, none of them meet my requirement.

    Till I found your libraries. It really saves me from few days of works. Once again, thank you so much! One thing I like about the library is that, it takes care of different convert views.

    Making the library generic like ArrayAdapter would be great. But so far, I can live without it :)

    opened by yccheok 1
  • How to get the local refresh

    How to get the local refresh

    • I am a Chinese. My English is not very good, forgive me!

    First thanks for your library.I'm using it.

    My Issues: I want to use this library to realize partial refresh(A single item to refresh,including SectionHeader and ListView's Item).

    I use listview's "getChildAt(int index)" return is a SectionHeaderView.(My English is not very good, forgive me!(^__^))

    How make?

    opened by JinyuZhang 0
  • Pull to refresh not compatible

    Pull to refresh not compatible

    https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

    SwipeRefreshLayout does not work with control. Very laggy scrolling... Issue is in how SwipeRefreshLayout calculates is it possible to scroll or not. It extracts first element from adapter and try to base own logic on it height/width/position....

    opened by OleksandrKucherenko 0
  • Index out of bounds when notifyDataSetChanged() is called.

    Index out of bounds when notifyDataSetChanged() is called.

    Hi there, I'm currently working with a ListView and a Cursor using this library.

    All works fine but when i try to change my adapter's cursor and notify that the content has changed, the AdapterView calls SectionedBaseAdapter.getItemId() (from AdapterView.rememberSyncState()) with position = 0, so the getPositionInSectionForPosition(0) call returns -1.

    So, since AdapterView will always call that method with position 0 (and no one else), I just made this simple little hack.

    opened by aballano 0
  • How to request focus for section headers when there are items behind them?

    How to request focus for section headers when there are items behind them?

    My situation is like this: I placed a button inside one section header. It was clickable when there are no items behind that section header. But to my surprise, when I was scrolling down the ListView and there were section items behind the section header, the items got focus instead of the header. This is abnormal as one might expect. Is there any means to circumvent this behavior? Please be kind to reply me because there's a deadline of my project.

    duplicate 
    opened by dannysunyu 0
  • Positions for section and item are wrong when set PinnedHeaderListView.OnItemClickListener

    Positions for section and item are wrong when set PinnedHeaderListView.OnItemClickListener

    I tried to track positions for section and item, so I added codes below into your sample project:

            listView.setOnItemClickListener(new PinnedHeaderListView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int section, int position, long id) {
                    Toast.makeText(MainActivity.this, "onItemClick: Section: " + section + " Position: " + position, Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onSectionClick(AdapterView<?> adapterView, View view, int section, long id) {
                    Toast.makeText(MainActivity.this, "onSectionClick: Section: " + section, Toast.LENGTH_SHORT).show();
                }
            });
    

    But, all toasted messages imply the section and position are wrong. For example,

    • when the HEADER 1 is clicked, "onSectionClick: Section: 0" is toasted;
    • when the HEADER 2 is clicked, "onSectionClick: Section: 0 Position: 0" is toasted;
    • when the Header for section 0 is clicked, "onSectionClick: Section: 0 Position: 1" is toasted;
    • when the Section 0 Item 14 is clicked, "onSectionClick: Section: 1 Position: 0" is toasted.

    So I reckoned codes

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int rawPosition, long id) {
                SectionedBaseAdapter adapter;
                if (adapterView.getAdapter().getClass().equals(HeaderViewListAdapter.class)) {
                    HeaderViewListAdapter wrapperAdapter = (HeaderViewListAdapter) adapterView.getAdapter();
                    adapter = (SectionedBaseAdapter) wrapperAdapter.getWrappedAdapter();
                } else {
                    adapter = (SectionedBaseAdapter) adapterView.getAdapter();
                }
                int section = adapter.getSectionForPosition(rawPosition);
                int position = adapter.getPositionInSectionForPosition(rawPosition);
    
                if (position == -1) {
                    onSectionClick(adapterView, view, section, id);
                } else {
                    onItemClick(adapterView, view, section, position, id);
                }
            }
    

    may be not accurate, which is from OnItemClickListener implements AdapterView.OnItemClickListener.

    So could you fix it?

    opened by bytebeats 6
  • How to intercept touch events on the pinned section view?

    How to intercept touch events on the pinned section view?

    When a section view is pinned at the top of screen, it becomes the pinned section view, any touch events like click, swipe will be passed to the item views behind it. But when a section view is not pinned, any click events can be listened. So do you have any idea to intercept click events occurred on a pinned section view?

    opened by bytebeats 0
  • NullPointerException when call adapter's notifyDataSetChanged()

    NullPointerException when call adapter's notifyDataSetChanged()

    When first used showing data. And do some operations, when I need to refresh datas, calling notifyDataSetChanged(), NullPointerException thrown.

    I saw the log, getItemId(int section, int position) the parameter position's value is -1.

    opened by ghost 0
  • fix a bug when run on meizu mx 3

    fix a bug when run on meizu mx 3

    if (pinnedHeaderHeight >= headerTop && headerTop > 0) { mHeaderOffset = headerTop - header.getHeight(); } else if (headerTop < 0) { header.setVisibility(INVISIBLE); } In the meizu mx 3, headerTop can greater than pinnedHeaderHeight when firstVisibleItem equals 0.So the header can strike twice.

    opened by wuliao518 0
Owner
James Smith
James Smith
An android library for section headers that stick to the top

StickyListHeaders StickyListHeaders is an Android library that makes it easy to integrate section headers in your ListView. These section headers stic

Emil Sjölander 5.5k Jan 5, 2023
Android ListView with sticky headers

DEPRECATED HeaderListView is deprecated. No new development will be taking place. Quickstart Import the HeaderListView module in your Android Studio p

Applidium 314 Nov 10, 2022
HorizontalListView is an Android ListView widget which scrolls in a horizontal manner (in contrast with the SDK-provided ListView which scrolls vertically).

HorizontalListView HorizontalListView is an Android ListView widget which scrolls in a horizontal manner (in contrast with the SDK-provided ListView w

MeetMe 722 Nov 10, 2022
Android library to display a ListView whose cells are not rigid but flabby and react to ListView scroll.

FlabbyListView This library is not maintained anymore and there will be no further releases Android library to display a ListView which cells are not

JPARDOGO 762 Nov 23, 2022
An easy to use Drag & Drop List for Android. Direct replacement of the android ListView.

DragNDropListView DragNDropListView is a direct replacement for the stock Android ListView. If you know how to use ListView, you already know how to u

null 187 Dec 22, 2022
An Android custom ListView and ScrollView with pull to zoom-in.

PullZoomView An Android custom ListView and ScrollView with pull to zoom-in. Features Set ZoomView enable Add HeaderView Custom ZoomView Parallax or N

Frank-Zhu 2.3k Dec 26, 2022
Android widget with pull to refresh for all the views,and support loadMore for ListView , RecyclerView, GridView and SwipeRefreshLayout.

CommonPullToRefresh Android widget with pull to refresh for all the views,and support loadMore for ListView,RecyclerView,GridView and SwipeRefreshLayo

null 1.1k Nov 10, 2022
A generic, customizable, open source Android ListView implementation that has 'Pull to Refresh' functionality.

Android 'Pull to Refresh' ListView library Demo video: http://www.youtube.com/watch?v=VjmdELnm3GI Project A generic, customizable, open source Android

Erik Wallentinsen 639 Nov 17, 2022
Parallax ScrollView and ListView for Android

Parallax Scrolls Parallax ListView and ScrollView for Android This project includes ScrollView with one or more parallaxed views ListView with paralla

Nir Hartmann 851 Dec 3, 2022
android custom listview,with interaction pattern load more and pull to refresh to load data dinamically

The first thing that i have to say is render thanks to johannilsson because all the part of pull to refresh listview is based in the code of his repos

Fabian Leon 447 Nov 15, 2022
Awesome Listview filter functionality in Android.

About Awesome Listview filter functionality in Android. See it in Action: https://www.youtube.com/watch?v=RO54U1ES5CA Listview with beautiful transpar

Bhavya  Mehta 446 Nov 20, 2022
Bringing extended scrolling features to Android's native ListView and ExpandableListView.

About QuickScroll QuickScroll is a library aimed at creating similar scrolling experiences to the native Contacts app's People view. It has the same s

Andras Kindler 461 Nov 11, 2022
Simple ListView based Android AccordionView

Android Accordion View Example git pull import to eclipse properties->android uncheck is library run as android application See main.xml. Screenshot L

Maciej Lopacinski 164 Nov 28, 2022
Android ListView that implements the QuickReturn UI pattern. Written from scratch with focus on performance.

QuickReturn Android ListView that implements the QuickReturn UI pattern. Written from scratch with focus on performance. Demo Usage In your build.grad

Felipe Lima 191 Nov 25, 2022
AssignmentListView is an Android ListView widget which calculate image loading times.

AssignmentListView is an Android ListView widget which calculate image loading times.

null 5 Sep 9, 2022
[] A swipe menu for ListView.

SwipeMenuListView A swipe menu for ListView. Demo Usage Add dependency dependencies { compile 'com.baoyz.swipemenulistview:library:1.3.0' } Step 1

星一 3.5k Dec 16, 2022
Android library to observe scroll events on scrollable views.

Android-ObservableScrollView Android library to observe scroll events on scrollable views. It's easy to interact with the Toolbar introduced in Androi

Soichiro Kashima 9.6k Dec 30, 2022
This project aims to provide a reusable pull to refresh widget for Android.

Pull To Refresh for Android Note This library is deprecated, a swipe refresh layout is available in the v4 support library. This project aims to provi

Johan Berg 2.5k Jan 2, 2023
Lazy load of images in Android

LazyList A simple library to display images in Android ListView. Images are being downloaded asynchronously in the background. Images are being cached

Fedor Vlasov 1.2k Nov 15, 2022