简体   繁体   中英

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.

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

Let's have a look the screen snapshot.

屏幕截图

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM