0

I have a standard GridView displaying images. I would like for every 20 items/images that are shown, a custom layout to be shown.

Here is an good example of what I mean:

enter image description here

Would anyone know how to achieve this? any advice is appreciated. Thank you.

Jack
  • 2,043
  • 7
  • 40
  • 69

1 Answers1

1

Would anyone know how to achieve this?

I did it. You can achieve this easily by using RecyclerView and GridLayoutManager and 2 view types in RecyclerView.Adapter. You can see codes below: With type TYPE_ADS, you span colum by 2. With normal item TYPE_ITEM, you don't span colum (means value = 1)

mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
GridLayoutManager glm = new GridLayoutManager(this, 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(mAdapter.getItemViewType(position)){
                    case MyAdapter.TYPE_ADS:
                        return 2;
                    case MyAdapter.TYPE_ITEM:
                        return 1;
                    default:
                        return -1;
                }
            }
        });

mRecyclerView.setLayoutManager(glm); 
mRecyclerView.setAdapter(mAdapter);
RoShan Shan
  • 2,924
  • 1
  • 18
  • 38
  • What is MyAdapter.TYPE_ADS and TYPE_ITEM refer to? is it an int defined in the adapter class? – Jack Jan 10 '17 at 19:43
  • Correct. Having 2 types int in your `RecyclerView.Adapter`. Based on item types have relevant layout. You can see this [link](http://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type) – RoShan Shan Jan 11 '17 at 02:00