Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GridView add header and footer like listview

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.

like image 716
srain Avatar asked Nov 04 '14 01:11

srain


2 Answers

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.

like image 63
srain Avatar answered Oct 01 '22 08:10

srain


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

like image 32
Sami Eltamawy Avatar answered Oct 01 '22 07:10

Sami Eltamawy