0

This is basically the same question as GridView - sometimes rows are top-aligned, sometimes they're bottom-aligned! but that doesn't seem to provide a sufficient answer, or at least one that works with my problem.

I have a gridview with items in it. In the item there is an image at the top followed by 3 textviews. Sometimes, depending on the value, one or two of the textviews will take up multiple lines.

When scrolling down this is fine, the image at the top is aligned with all other rows. But when you scroll the opposite direction, it's the very bottom textview that is aligned and the top of the images are out.

I will try and draw an example below.

Scrolling down

------    ------    ------

Image     Image     Image

------    ------    ------
TView1    TView1    TView1
------    ------    ------
TView2    TView2    TView2
------    TView2    ------
TView3    ------    TView3
------    TView3    ------
          ------

Scrolling Up

          ------
------              ------
          Image 
Image               Image
          ------
------    TView1    ------
TView1    ------    TView1
------    TView2    ------
TView2    TView2    TView2
------    ------    ------
TView3    TView3    TView3
------    ------    ------

I've read that I should be using layout_gravity="top" but that doesn't work. I thought it was possibly because layout_gravity isn't supposed to work in relativelayout, which is what my item is using, but when changing that to a linearlayout, it still didn't work.

Thanks

Community
  • 1
  • 1
Russ Wheeler
  • 2,590
  • 5
  • 30
  • 57

1 Answers1

0

Height of the every cell in the GridView must stay same, otherwise you will end up with such issues.

M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • I have to give it a fixed height?! Urgh that doesn't sound right/nice. Do I have to calculate that height based on the device's dimensions? – Russ Wheeler Dec 04 '13 at 14:31
  • I can guess that you have more data in `TextView` at some places. You just need to use `android:minLines` and `android:maxLines` attribute in your cell xml layout. This will make sure that your `TextView` size stays same. – M-Wajeeh Dec 04 '13 at 14:39
  • Or better approach would be to just use `android:lines` attribute. – M-Wajeeh Dec 04 '13 at 14:40
  • That was the answer in the question I referenced. Is that really the way? The custom view has to be exactly the same size? You can't just have all items aligned to top or bottom? If that is the only way, I appreciate your help, but that sounds very basic to me – Russ Wheeler Dec 04 '13 at 14:41
  • Also, if I do that, then the Textviews that don't end up having 2 or 3 lines, but might be able to, will have a big gap between them and the next textview. I don't want that, I want the only gap to appear in between each gridview item. – Russ Wheeler Dec 04 '13 at 14:43
  • Well this is the only way. You can set gravity on you `TextView` to set its content in center to make it look a bit better. If you want to align them at top or bottom then there are ways to acheive this. 1. Use a `ListView` and your row should be such that it seems there is a `GridView` 2. Hardcode you cell view in dps and arrange your items such that if there is less data then they will align at top and leave the space below. – M-Wajeeh Dec 04 '13 at 14:53
  • Or just use one of `StaggeredGridView` library https://www.google.com.pk/search?q=StaggeredGridView – M-Wajeeh Dec 04 '13 at 14:54