5

I have a GridView in which I need to show images. I have applied below logic:

If 1 Photo : 1 row and 1 col and full width image.
If 2 Photos : 1 row and 2 cols with equal width of both images (half of screen width)
If 3 photos: 2 rows. 1st row will be having 1 col (1 image with full width)
and 2nd row will be having 2 images with equal width (half of screen width)

If 4 or more photos : width will be half for each column.

I have managed for the rest cases, the only problem is to set for case 3 when I am having 3 photos. I want 1 full width photo in 2 row ans 2 equal half width photos in 2nd row but my code giving me 2 half equal width photos in 1st row and 1 half width photo in 2nd row.

I need to use Gridview only, please tell me if this can be possible using Gridview. I have already asked this question but didn't get any response. Please help me if you have any idea here.

Thank you so much.

I want to have below layout:

enter image description here

Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78
  • refer http://stackoverflow.com/questions/18850704/dynamically-change-column-number-in-android-gridview – sasikumar Feb 19 '16 at 06:32
  • Try using RecyclerView with GridLayoutManager. You can set the span as you want in GridLayoutManager. – ajantha Feb 19 '16 at 06:48
  • Thanks for the above link. I have checked this out but it will not work in my case. I am using this Gridview inside a Recyclerview and will be having images in each list item. I am having one variable for each list which gives the photo count. I set the column count according to this photoCount. So, if I will be having 3 photos, in that list item I want to show 1 photo as full width and other 2 photos with half width. I want to set Different no of columns at the same position of the list item. Please help me if you have any idea here. – Prithniraj Nicyone Feb 19 '16 at 06:57
  • I have already tried Recyclerview with GridLayoutManager, but that it not working according to my case. I set spanSize of a particular position and I want to set different 2 column widths on the same position. Using Recyclerview with GridLayoutManager, I can set no of columns as 2 for position 1 or 3 for position 2 But If at the position 1 I want to set 1 row with 1 column and 2nd row with 2 columns, I am unable to do this. Can I do this using Gridview. Please help me if anyone have any idea here. Thank you so much for all your help. – Prithniraj Nicyone Feb 19 '16 at 07:02

1 Answers1

7

See code below

I have created demo and working fine

  rv = (RecyclerView) findViewById(R.id.rv);
        final MyAdapter adapter = new MyAdapter();
        rv.setAdapter(adapter);
        GridLayoutManager mLayoutManager = new GridLayoutManager(this, 2);
        rv.setLayoutManager(mLayoutManager);

        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (adapter.getItemCount() == 1) {
                    return 2;
                } else if (adapter.getItemCount() == 2) {
                    return 1;
                } else if (adapter.getItemCount() == 3) {
                    if (position == 0) {
                        return 2;
                    } else {
                        return 1;
                    }
                } else {

                    return 1;

                }


            }
        });
N J
  • 27,217
  • 13
  • 76
  • 96
  • Thanks for such efforts. One question I want to ask is that, if on position 0, I am having 3 photos. Now, I want to show one photos as full width means span count 1 and rest 2 photos, I want to show 2 half width photos. How can I do that, what should I do for that. Please help me if you know how to achieve it. Thank you so much for all your help. – Prithniraj Nicyone Feb 19 '16 at 07:16
  • that is already covered `if (adapter.getItemCount() == 3) { if (position == 0) { return 2; } else { return 1; } } ` – N J Feb 19 '16 at 07:18
  • I have tried the way you suggested but it is always dividing the whole width in 2 parts. Like I want to show a full width image if the photocount == 1, so I returned 1 as span count from getSpanSize method but it is dividing the width in 2 parts and showing only the half photo and half of the width is unused ans shown as white space. – Prithniraj Nicyone Feb 19 '16 at 08:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103972/discussion-between-nj-nilesh-j-and-prithniraj-nicyone). – N J Feb 19 '16 at 15:47
  • @NJ ,Can you please help me to solve an issue in gridview – Nikson Apr 22 '18 at 06:12
  • @Nikson what is issue?. Its better to ask new question because its old question. – N J Apr 22 '18 at 13:33
  • @NJ please take a look on it – Nikson Apr 22 '18 at 13:56
  • @NJ ,https://stackoverflow.com/questions/49963179/gridview-not-fit-for-all-screens-in-android – Nikson Apr 22 '18 at 13:58
  • I am struggling with this for last two days please help me to solve this – Nikson Apr 22 '18 at 13:59
  • @NJ did you checked it ,please guide me to solve this please – Nikson Apr 22 '18 at 14:04