Android GridView add header and footer like listview

徘徊边缘 提交于 2019-11-29 14:38:08

问题


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.


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/26726538/android-gridview-add-header-and-footer-like-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!