|
| 1 | +package dragosholban.com.bestphotos; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.support.v7.widget.GridLayoutManager; |
| 5 | +import android.support.v7.widget.RecyclerView; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.ImageView; |
| 10 | +import android.widget.TextView; |
| 11 | + |
| 12 | +import com.squareup.picasso.Picasso; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | + |
| 16 | +public class ImageRecyclerViewAdapter extends RecyclerView.Adapter { |
| 17 | + private static final int TYPE_SQUARE = 0; |
| 18 | + private static final int TYPE_VERTICAL = 1; |
| 19 | + |
| 20 | + public static class SquareImageViewHolder extends RecyclerView.ViewHolder { |
| 21 | + |
| 22 | + ImageView imageView; |
| 23 | + TextView likesTextView; |
| 24 | + |
| 25 | + public SquareImageViewHolder(View itemView) { |
| 26 | + super(itemView); |
| 27 | + imageView = itemView.findViewById(R.id.gridImageview); |
| 28 | + likesTextView = itemView.findViewById(R.id.likesCount); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static class VerticalImagesViewHolder extends RecyclerView.ViewHolder { |
| 33 | + |
| 34 | + ImageView imageView1; |
| 35 | + ImageView imageView2; |
| 36 | + TextView likesTextView1; |
| 37 | + TextView likesTextView2; |
| 38 | + |
| 39 | + public VerticalImagesViewHolder(View itemView) { |
| 40 | + super(itemView); |
| 41 | + imageView1 = itemView.findViewById(R.id.gridImageview1); |
| 42 | + likesTextView1 = itemView.findViewById(R.id.likesCount1); |
| 43 | + imageView2 = itemView.findViewById(R.id.gridImageview2); |
| 44 | + likesTextView2 = itemView.findViewById(R.id.likesCount2); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private static class MyImage { |
| 49 | + FacebookImage image; |
| 50 | + int span = 1; |
| 51 | + |
| 52 | + public MyImage(FacebookImage image, int span) { |
| 53 | + this.image = image; |
| 54 | + this.span = span; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private Context mContext; |
| 59 | + private ArrayList<ArrayList<MyImage>> images = new ArrayList<>(); |
| 60 | + |
| 61 | + public ImageRecyclerViewAdapter(Context context, ArrayList<FacebookImage> images) { |
| 62 | + this.mContext = context; |
| 63 | + distributeImages(images); |
| 64 | + } |
| 65 | + |
| 66 | + private void distributeImages(ArrayList<FacebookImage> images) { |
| 67 | + ArrayList<MyImage> arrayOfImages = new ArrayList<>(); |
| 68 | + int i = 0; |
| 69 | + int rightPos = 0; |
| 70 | + boolean left = true; |
| 71 | + int span; |
| 72 | + for (FacebookImage image : images) { |
| 73 | + i++; |
| 74 | + span = 1; |
| 75 | + if (left) { |
| 76 | + if (i % 6 == 1) { |
| 77 | + span = 2; |
| 78 | + rightPos = i + 8; |
| 79 | + } |
| 80 | + arrayOfImages.add(new MyImage(image, span)); |
| 81 | + if (i % 6 == 2) { |
| 82 | + left = false; |
| 83 | + continue; |
| 84 | + } |
| 85 | + } else { |
| 86 | + if (i % rightPos == 0) { |
| 87 | + span = 2; |
| 88 | + left = true; |
| 89 | + } |
| 90 | + arrayOfImages.add(new MyImage(image, span)); |
| 91 | + if (i % rightPos == rightPos - 2) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + } |
| 95 | + this.images.add(arrayOfImages); |
| 96 | + arrayOfImages = new ArrayList<>(); |
| 97 | + } |
| 98 | + |
| 99 | + if(arrayOfImages.size() > 0) { |
| 100 | + this.images.add(arrayOfImages); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onAttachedToRecyclerView(RecyclerView recyclerView) { |
| 106 | + super.onAttachedToRecyclerView(recyclerView); |
| 107 | + |
| 108 | + GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3); |
| 109 | + |
| 110 | + // Create a custom SpanSizeLookup where the first item spans both columns |
| 111 | + layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { |
| 112 | + @Override |
| 113 | + public int getSpanSize(int position) { |
| 114 | + return images.get(position).get(0).span; |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + recyclerView.setLayoutManager(layoutManager); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 123 | + if(viewType == TYPE_VERTICAL) { |
| 124 | + LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 125 | + View view = inflater.inflate(R.layout.vertical_square_images, parent, false); |
| 126 | + VerticalImagesViewHolder holder = new VerticalImagesViewHolder(view); |
| 127 | + |
| 128 | + return holder; |
| 129 | + } else { |
| 130 | + LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 131 | + View view = inflater.inflate(R.layout.square_image, parent, false); |
| 132 | + SquareImageViewHolder holder = new SquareImageViewHolder(view); |
| 133 | + |
| 134 | + return holder; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 140 | + ArrayList<MyImage> imgs = images.get(position); |
| 141 | + if (imgs.size() > 1) { |
| 142 | + VerticalImagesViewHolder myHolder = (VerticalImagesViewHolder) holder; |
| 143 | + Picasso.get().load(imgs.get(0).image.url).into(myHolder.imageView1); |
| 144 | + myHolder.likesTextView1.setText(String.valueOf(imgs.get(0).image.reactions)); |
| 145 | + myHolder.imageView1.setTag(imgs.get(0).image.link); |
| 146 | + |
| 147 | + Picasso.get().load(imgs.get(1).image.url).into(myHolder.imageView2); |
| 148 | + myHolder.likesTextView2.setText(String.valueOf(imgs.get(1).image.reactions)); |
| 149 | + myHolder.imageView2.setTag(imgs.get(1).image.link); |
| 150 | + } else { |
| 151 | + SquareImageViewHolder myHolder = (SquareImageViewHolder) holder; |
| 152 | + Picasso.get().load(imgs.get(0).image.url).into(myHolder.imageView); |
| 153 | + myHolder.likesTextView.setText(String.valueOf(imgs.get(0).image.reactions)); |
| 154 | + myHolder.imageView.setTag(imgs.get(0).image.link); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public int getItemViewType(int position) { |
| 160 | + if (images.get(position).size() > 1) { |
| 161 | + return TYPE_VERTICAL; |
| 162 | + } |
| 163 | + |
| 164 | + return TYPE_SQUARE; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public int getItemCount() { |
| 169 | + return images.size(); |
| 170 | + } |
| 171 | +} |
0 commit comments