Gridview with Different Cells Sizes, Pinterest Style

gridView with different cells sizes, pinterest style

I think a RecyclerView with StaggeredGridLayoutManager can solve this issue, and that if you wish to also have a fast-scroller, you can try this library.

So, no more trick are needed, as Google provided a solution...

GridView: GridView with different cells sizes and layout,

User RecyclerView with GridLayoutManager that have set SpanSizeLookup. So it will be as follows:

int fullSpanSize = 3;
int normalSpanSize = 1;

GridLayoutManager layout = new GridLayoutManager(context, fullSpanSize);
layout.setSpaneSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == 3 ? fullSpanSize : normalSpanSize;
}
});
recyclerView.setLayoutManager(layout);

Different Cell size grid for custom views

I used the following code to programmatically create views and set up their PercentRelativeLayout.LayoutParams :

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_percent_relative);

ViewGroup root = (ViewGroup)findViewById(R.id.perecent_layout_view);

View blueWidget = new View(this);
blueWidget.setBackground(blue);
root.addView(blueWidget);

setUpLayout(blueWidget, 2.0f/8.0f, 6.0f/8.0f, 6.0f/8.0f, 0.0f, 2.0f/8.0f, 0.0f);

View redWidget = new View(this);
redWidget.setBackground(red);
root.addView(redWidget);

setUpLayout(redWidget, 5.0f/8.0f, 2.0f/8.0f, 3.0f/8.0f, 0.0f, 0.0f, 6.0f/8.0f);

}

private void setUpLayout(View view, float widthPercent,
float heightPercent,
float leftMarginPercent,
float rightMarginPercent,
float topMarginPercent,
float bottomMarginPercent) {

PercentRelativeLayout.LayoutParams layoutParams = new PercentRelativeLayout.LayoutParams(view.getLayoutParams());
PercentLayoutHelper.PercentLayoutInfo percentLayoutInfo = layoutParams.getPercentLayoutInfo();

percentLayoutInfo.heightPercent = heightPercent;
percentLayoutInfo.widthPercent = widthPercent;
percentLayoutInfo.topMarginPercent= topMarginPercent;
percentLayoutInfo.bottomMarginPercent= bottomMarginPercent;
percentLayoutInfo.leftMarginPercent= leftMarginPercent;
percentLayoutInfo.rightMarginPercent= rightMarginPercent;

view.setLayoutParams(layoutParams);

}

The view looks like this:
Sample Image

How to design GridView with variable size of cell(images) in Android?

You may want to try out this third-party library called StaggeredGridView.

  • http://www.androidviews.net/2013/01/pinterest-like-adapterview/

UICollectionView Dynamic Cell Height as per the content | Pinterest Layouts

What you have mentioned can be achieved using flow layout — a layout class provided by UIKit.

This is what you are looking for:

https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest

I hope you can put some effort into reading this very simple post and follow each step carefully.



Related Topics



Leave a reply



Submit