Android GridView add header and footer like listview
Asked Answered
A

3

8

Maybe you want to call addHeaderView() or addFooterView() in GridView, which it does not have.

It's nature that we want to add header view or a footer view to a GridView. Maybe your have been searching a long time like me, but finally find no solution.

Here, I will give out an solution.

Ascendancy answered 4/11, 2014 at 1:59 Comment(1)
You need to add separate layout after and before GridView in your xml file.Unpolite
A
16

I post this answer just want people who come here can get what the want.

Let's have a look the screen snapshot.

Screen Shot

the usage is very simple

GridViewWithHeaderAndFooter gridView = (GridViewWithHeaderAndFooter) v.findViewById(R.id.ly_image_list_grid);

LayoutInflater layoutInflater = LayoutInflater.from(this);
View headerView = layoutInflater.inflate(R.layout.test_header_view, null);
View footerView = layoutInflater.inflate(R.layout.test_footer_view, null);
gridView.addHeaderView(headerView);
gridView.addFooterView(footerView);

It'a single file project, now it hosts at GitHub: https://github.com/liaohuqiu/android-GridViewWithHeaderAndFooter

You can follow the link above to access the code. Happy coding.

Ascendancy answered 4/11, 2014 at 2:6 Comment(14)
@inVINCEable, thank you. It's ok now: github.com/liaohuqiu/android-GridViewWithHeaderAndFooterAscendancy
@Ascendancy Hi, I have implemented with this library. But I am facing one problem and its my requirement too. That is the header and footer should fit to the screen width not the grid items.The grid items should have right and left margins. How can I achieve using this library ?Culp
@KarthikeyanVe set margin / padding to grid items.Ascendancy
@Ascendancy how to add sections in this library we can only use one section here with its header and foooter ? but how can i add 2 sections with its header , content of section , footerRubidium
@Rubidium you can add multiple header or footer.Ascendancy
@Ascendancy no i am asking can i made sections in this lib and for each section i need header and footer is this possible ? and also how can we integrate loadMoreData on footer click ?Rubidium
@Ascendancy if u know the way pls share how can we do ?Rubidium
@Rubidium I think they are not sections, it is TabIndicators or a specify view.Ascendancy
@Ascendancy pls check tabindicators fragment i m talking about that fragment which is opening under tabindicatorRubidium
@Rubidium sorry, I'm a little confused. My hangouts: [email protected]Ascendancy
You can use RecyclerView developer.android.com/reference/android/support/v7/widget/…Swamp
hi, how can i update the headerview from the adapter? i mean lets say i have a textview inside the header, from the adapter after i changed the list, i want to count total and save into the textview inside the header. how can i do that? please adviceForetop
@AlfredAngkasa hi, for now, the best solution is use recyclerview instead of this view.Ascendancy
@srain, hi. thanks for your answer. I done it with view. I mean i send the view to the adapter, and do some logic at the adapter. it works. :)Foretop
L
6

To add a Header to the GridView, use the HeaderGridView by Google.

Just copy this class into your project and use it instead of the default GridView, and you will be able to add header using the following line:

 gridView.addHeaderView(customView);

Source: answer

Levis answered 26/5, 2015 at 12:57 Comment(0)
H
0

Actually you can create layout with two columns and use ListView. In getView you should manipulate with columns and set there some data.

Also you can create just one column in first and last row. It will look like header or footer.

Besides, you can look through these links:

https://github.com/munix/GridViewHeader

https://github.com/TonicArtos/StickyGridHeaders

Hyperemia answered 4/11, 2014 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.