57

Is it possible to create pinterest like layout on Android using GridView? I want to create image gallery using GridView but I am not sure if it is good solution. I do not want create three LinearLayouts (I think that this solution is not good: Pinterest style listview or gridview in android)

Any ideas ;)?

enter image description here

Community
  • 1
  • 1
radzio
  • 2,862
  • 4
  • 26
  • 35

7 Answers7

25

I've been playing with this also (used LinearLayout) but at the end I had lot of problems with memory consumption (especially when I had to reload the items). I settled on simple solution which uses two synchronized ListViews. This way I can exploit internal caching which helps a lot. To do this I had to use OnTouchListener and OnScrollListener who synchronize lists. Here's an example:

https://github.com/vladexologija/PinterestListView

enter image description here

vladexologija
  • 6,857
  • 4
  • 29
  • 28
  • can you post the sample code for this? – eugene Nov 16 '12 at 07:07
  • What exactly do you need? Whole Activity source? – vladexologija Nov 16 '12 at 13:14
  • well anything more than the above code actually. Being a novice android developer, it's going to take me a while to grasp the concepts that you laid out. – eugene Nov 19 '12 at 03:38
  • 3
    There you go. There are still a lot of things to do (fix few bugs, optimize, simplify...) but this version works fine. What's most important it uses recycling! – vladexologija Nov 21 '12 at 10:31
  • @vladexologija Thank you so much for such a great post. – San Jan 03 '14 at 05:56
  • @vladexologija I want to set the images from my SDCard. As you have used Integer array, I am not able to pass the path to the images. Any idea on how to achieve this ?? It would be really helpful. Thanks in advance. – San Jan 04 '14 at 07:39
  • What this example does is that it just syncs two lists. If you build any two lists with their custom adapters (in your case loading image with some lazy loading) they should work with minor changes to posted code. – vladexologija Jan 06 '14 at 08:46
  • does anyone know how to handle onItemClick, it doesn't work for me – fish40 Sep 05 '14 at 12:23
22

Create layout like as follow

<ScrollView...>
<LinearLayout....
   android:id="@+id/linear1"
   orientation="horizontal">

   <LinearLayout....
     android:id="@+id/linear2"
     android:layout_weight="0.33"
     orientation="vertical">

   <LinearLayout....
     android:id="@+id/linear3"
     android:layout_weight="0.33"
     orientation="vertical">

   <LinearLayout....
     android:layout_weight="0.33"
     orientation="vertical">

</LinearLayout>
</ScrollView>

Now add your ImageView dynamically in layouts

linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
linear3 = (LinearLayout) findViewById(R.id.linear3);

for(int i=0;i<n;i++)
{
   ImageView iv = new ImageView(this);
   iv.setImageResource(R.id.icon);

   int j = count % 3;  <---- 
   if(j==0)
       linear1.addView(iv);
   else if(j==1)
       linear2.addView(iv);
   else
       linear3.addView(iv); 
}

output:

enter image description here

MAC
  • 15,799
  • 8
  • 54
  • 95
21

Some useful libraries for implementing Pinterest-like grid view:

petrnohejl
  • 7,581
  • 3
  • 51
  • 63
  • Is there an example with horizontal scroll? – Gorets Feb 05 '14 at 15:18
  • used the staggeredGridView library in my app https://play.google.com/store/apps/details?id=com.taureano.amazicons . Worked great so up voting this answer. There is a small bug in orientation change in the library but i found the fix in stack overflow. – Swati Rawat Jun 15 '14 at 09:20
7

For Recent visitors to this question , I would suggest using RecyclerView with StaggedGridLayoutManager. It's having more than enough functions and flexibility.

MohK
  • 1,873
  • 1
  • 18
  • 29
6

A standalone helper for synchronizing scrolling of 2 ListViews: https://gist.github.com/yanchenko/6179793

yanchenko
  • 56,576
  • 33
  • 147
  • 165
3

I am using this lib: https://github.com/huewu/PinterestLikeAdapterView.

It works pretty well. The only issue I have is that the setOnItemClickListener and setOnItemLongClickListener are a bit buggy so I set the listeners directly on the convertView.

emerix
  • 611
  • 5
  • 9
2

This library comes from the Etsy application: https://github.com/etsy/AndroidStaggeredGrid

HannahMitt
  • 1,010
  • 11
  • 14