1

Everyone knows that GridView does not supports headers and footers like a ListView.

There is a few ways to implementing this:

  • Use a ListView and form columnt manually via ViewGroups. It not works for me, because it's require a lot of layout operations like measuring and layouting, and it's difficult to implement draw selector on top.
  • Use special adapter. It works fine with a footer: we should fill last cells with a Space and manually insert after them out footer with width that equals GridView width. But this not works with headers: although header is stretched, next cells float on it.
  • Use a GridLayout. GridLayout is good, but what about performance with 500-1000 cells? AdapterView supports caching and reusing Views, as far as I know, this is not possible with GridLayout.
  • Extend GridView and write custom class, that allows to draw a header before the grid content. It's difficult, but it's should work very fast. Let's try to figure out how to do this:

    1. Measure the header. It's very simple, I have not questions about this.
    2. Layout header in the top of the grid. We also should consider with scrolling position to allow move header with whole grid content, so my first question is: how to know where bottom border should be located while scrolling?
    3. Layout whole grid content after the header. How to do that? I've newer do this before.
    4. Dispatch draw to the header view too and resolve overscrolling effect if it's not work well.
    5. Handle the scroll event and refresh header position.

So what you can suggest me? How to do header offset? Is it right to invoke relayouting with every scroll event?

bvitaliyg
  • 3,155
  • 4
  • 30
  • 46

1 Answers1

0

I searched an answer on a same situation with a GridView (but for a FooterView).

I've read attentively your suggestions and some from other websites. I had the same reflexion. I found a simple way as your tip: "Use special adapter. It works fine with a footer..." and this answer by @RaulSoto helped me a lot. But when I tried to update my gridview, I had a NPE, because my footer was not like the layout of my items and I had a custom filter which recalculated the getCount() method, but without understand that another view was added.

Finally, I found only solution which works: a custom class.

Create your own class as you said: "Extend GridView and write custom class" but don't extend GridView. You should extend with ListView and measure the entire width, the column width and the number of columns. I think, it's less difficult that to extend GridView, calculate the height of the header view and move it as you move your gridview or refresh the header each time you handle a scroll event..
I searched to do it in this way and I took this little project on GitHub: HFGridView by Sergey Burish. It was exactly what I need, no more.
I only added a custom attrs file in my app and customize a bit his project to have the expected result (especially, it was to have one column in portrait, two in landscape mode, refering to the numColumns attribute in my layout).
And when I try, just for test, to add a HeaderView and refresh the content with adding new items, the header view stays at the top of my gridview list, without refreshing himself.

So, I think you should search to create your class as GridView extends ListView. Refer you to the HFGridView by SBurish, it is very simple to understand how it does.

Hope this helps you with your purpose.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99