Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView with different column width in rows

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

like image 843
Prithniraj Nicyone Avatar asked Feb 19 '16 06:02

Prithniraj Nicyone


1 Answers

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;

                }


            }
        });
like image 161
N J Avatar answered Oct 16 '22 17:10

N J