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.

srain
  • 8,944
  • 6
  • 30
  • 42

3 Answers3

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.

srain
  • 8,944
  • 6
  • 30
  • 42
  • 1
    @inVINCEable, thank you. It's ok now: https://github.com/liaohuqiu/android-GridViewWithHeaderAndFooter – srain Jan 05 '15 at 23:48
  • @srain 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 ? – Karthikeyan Ve Mar 03 '15 at 08:36
  • 1
    @KarthikeyanVe set margin / padding to grid items. – srain Mar 03 '15 at 11:14
  • @srain 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 , footer – Erum Aug 24 '15 at 06:51
  • @Erum you can add multiple header or footer. – srain Aug 25 '15 at 08:23
  • @srain 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 ? – Erum Aug 25 '15 at 08:35
  • @srain if u know the way pls share how can we do ? – Erum Aug 25 '15 at 08:36
  • @Erum I think they are not sections, it is TabIndicators or a specify view. – srain Aug 26 '15 at 08:49
  • @srain pls check tabindicators fragment i m talking about that fragment which is opening under tabindicator – Erum Aug 26 '15 at 08:56
  • @Erum sorry, I'm a little confused. My hangouts: liaohuqiu@gmail.com – srain Aug 27 '15 at 13:41
  • You can use RecyclerView https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html – Sinan Dizdarević Oct 22 '15 at 21:49
  • 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 advice – Alfred Angkasa Aug 10 '16 at 05:32
  • @AlfredAngkasa hi, for now, the best solution is use recyclerview instead of this view. – srain Aug 10 '16 at 15:52
  • @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. :) – Alfred Angkasa Aug 12 '16 at 10:36
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

Community
  • 1
  • 1
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
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

QArea
  • 4,955
  • 1
  • 12
  • 22