Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Show the User Images
- Loading branch information
1 parent
2ab2d0e
commit 591d14a
Showing
7 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
app/src/main/java/dragosholban/com/bestphotos/ImageRecyclerViewAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package dragosholban.com.bestphotos; | ||
|
||
import android.content.Context; | ||
import android.support.v7.widget.GridLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import com.squareup.picasso.Picasso; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class ImageRecyclerViewAdapter extends RecyclerView.Adapter { | ||
private static final int TYPE_SQUARE = 0; | ||
private static final int TYPE_VERTICAL = 1; | ||
|
||
public static class SquareImageViewHolder extends RecyclerView.ViewHolder { | ||
|
||
ImageView imageView; | ||
TextView likesTextView; | ||
|
||
public SquareImageViewHolder(View itemView) { | ||
super(itemView); | ||
imageView = itemView.findViewById(R.id.gridImageview); | ||
likesTextView = itemView.findViewById(R.id.likesCount); | ||
} | ||
} | ||
|
||
public static class VerticalImagesViewHolder extends RecyclerView.ViewHolder { | ||
|
||
ImageView imageView1; | ||
ImageView imageView2; | ||
TextView likesTextView1; | ||
TextView likesTextView2; | ||
|
||
public VerticalImagesViewHolder(View itemView) { | ||
super(itemView); | ||
imageView1 = itemView.findViewById(R.id.gridImageview1); | ||
likesTextView1 = itemView.findViewById(R.id.likesCount1); | ||
imageView2 = itemView.findViewById(R.id.gridImageview2); | ||
likesTextView2 = itemView.findViewById(R.id.likesCount2); | ||
} | ||
} | ||
|
||
private static class MyImage { | ||
FacebookImage image; | ||
int span = 1; | ||
|
||
public MyImage(FacebookImage image, int span) { | ||
this.image = image; | ||
this.span = span; | ||
} | ||
} | ||
|
||
private Context mContext; | ||
private ArrayList<ArrayList<MyImage>> images = new ArrayList<>(); | ||
|
||
public ImageRecyclerViewAdapter(Context context, ArrayList<FacebookImage> images) { | ||
this.mContext = context; | ||
distributeImages(images); | ||
} | ||
|
||
private void distributeImages(ArrayList<FacebookImage> images) { | ||
ArrayList<MyImage> arrayOfImages = new ArrayList<>(); | ||
int i = 0; | ||
int rightPos = 0; | ||
boolean left = true; | ||
int span; | ||
for (FacebookImage image : images) { | ||
i++; | ||
span = 1; | ||
if (left) { | ||
if (i % 6 == 1) { | ||
span = 2; | ||
rightPos = i + 8; | ||
} | ||
arrayOfImages.add(new MyImage(image, span)); | ||
if (i % 6 == 2) { | ||
left = false; | ||
continue; | ||
} | ||
} else { | ||
if (i % rightPos == 0) { | ||
span = 2; | ||
left = true; | ||
} | ||
arrayOfImages.add(new MyImage(image, span)); | ||
if (i % rightPos == rightPos - 2) { | ||
continue; | ||
} | ||
} | ||
this.images.add(arrayOfImages); | ||
arrayOfImages = new ArrayList<>(); | ||
} | ||
|
||
if(arrayOfImages.size() > 0) { | ||
this.images.add(arrayOfImages); | ||
} | ||
} | ||
|
||
@Override | ||
public void onAttachedToRecyclerView(RecyclerView recyclerView) { | ||
super.onAttachedToRecyclerView(recyclerView); | ||
|
||
GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3); | ||
|
||
// Create a custom SpanSizeLookup where the first item spans both columns | ||
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { | ||
@Override | ||
public int getSpanSize(int position) { | ||
return images.get(position).get(0).span; | ||
} | ||
}); | ||
|
||
recyclerView.setLayoutManager(layoutManager); | ||
} | ||
|
||
@Override | ||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
if(viewType == TYPE_VERTICAL) { | ||
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
View view = inflater.inflate(R.layout.vertical_square_images, parent, false); | ||
VerticalImagesViewHolder holder = new VerticalImagesViewHolder(view); | ||
|
||
return holder; | ||
} else { | ||
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
View view = inflater.inflate(R.layout.square_image, parent, false); | ||
SquareImageViewHolder holder = new SquareImageViewHolder(view); | ||
|
||
return holder; | ||
} | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | ||
ArrayList<MyImage> imgs = images.get(position); | ||
if (imgs.size() > 1) { | ||
VerticalImagesViewHolder myHolder = (VerticalImagesViewHolder) holder; | ||
Picasso.get().load(imgs.get(0).image.url).into(myHolder.imageView1); | ||
myHolder.likesTextView1.setText(String.valueOf(imgs.get(0).image.reactions)); | ||
myHolder.imageView1.setTag(imgs.get(0).image.link); | ||
|
||
Picasso.get().load(imgs.get(1).image.url).into(myHolder.imageView2); | ||
myHolder.likesTextView2.setText(String.valueOf(imgs.get(1).image.reactions)); | ||
myHolder.imageView2.setTag(imgs.get(1).image.link); | ||
} else { | ||
SquareImageViewHolder myHolder = (SquareImageViewHolder) holder; | ||
Picasso.get().load(imgs.get(0).image.url).into(myHolder.imageView); | ||
myHolder.likesTextView.setText(String.valueOf(imgs.get(0).image.reactions)); | ||
myHolder.imageView.setTag(imgs.get(0).image.link); | ||
} | ||
} | ||
|
||
@Override | ||
public int getItemViewType(int position) { | ||
if (images.get(position).size() > 1) { | ||
return TYPE_VERTICAL; | ||
} | ||
|
||
return TYPE_SQUARE; | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return images.size(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_height="wrap_content" | ||
android:layout_width="wrap_content"> | ||
<item> | ||
<shape android:shape="rectangle"> | ||
<solid android:color="#3b5998"/> | ||
<corners android:radius="20dp" /> | ||
<padding | ||
android:top="2dp" | ||
android:bottom="2dp" | ||
android:left="4dp" | ||
android:right="4dp" /> | ||
</shape> | ||
</item> | ||
</selector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintDimensionRatio="H,1:1" | ||
android:gravity="center" | ||
android:layout_margin="1dp" | ||
> | ||
|
||
<ImageView | ||
android:id="@+id/gridImageview" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:onClick="onImageClick" | ||
android:scaleType="centerCrop" | ||
app:layout_constraintDimensionRatio="H,1:1" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<TextView | ||
android:id="@+id/likesCount" | ||
android:background="@layout/rounded_border_textview" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="8dp" | ||
android:layout_marginEnd="8dp" | ||
android:layout_marginRight="8dp" | ||
android:paddingTop="2dp" | ||
android:paddingBottom="2dp" | ||
android:paddingStart="4dp" | ||
android:paddingEnd="4dp" | ||
android:text="0" | ||
android:textColor="@android:color/white" | ||
android:textSize="14sp" | ||
android:textStyle="normal" | ||
android:textAlignment="center" | ||
android:shadowDx="0" | ||
android:shadowDy="0" | ||
android:shadowRadius="10" | ||
android:shadowColor="#000000" | ||
app:layout_constraintBottom_toBottomOf="@id/gridImageview" | ||
app:layout_constraintEnd_toEndOf="@id/gridImageview" | ||
/> | ||
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:layout_margin="1dp" | ||
> | ||
|
||
<ImageView | ||
android:id="@+id/gridImageview1" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:onClick="onImageClick" | ||
android:scaleType="centerCrop" | ||
app:layout_constraintDimensionRatio="H,1:1" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<TextView | ||
android:id="@+id/likesCount1" | ||
android:background="@layout/rounded_border_textview" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="8dp" | ||
android:layout_marginEnd="8dp" | ||
android:layout_marginRight="8dp" | ||
android:paddingTop="2dp" | ||
android:paddingBottom="2dp" | ||
android:paddingStart="4dp" | ||
android:paddingEnd="4dp" | ||
android:text="0" | ||
android:textColor="@android:color/white" | ||
android:textSize="14sp" | ||
android:textStyle="normal" | ||
android:textAlignment="center" | ||
android:shadowDx="0" | ||
android:shadowDy="0" | ||
android:shadowRadius="10" | ||
android:shadowColor="#000000" | ||
app:layout_constraintBottom_toBottomOf="@id/gridImageview1" | ||
app:layout_constraintEnd_toEndOf="@id/gridImageview1" | ||
/> | ||
|
||
<ImageView | ||
android:id="@+id/gridImageview2" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:onClick="onImageClick" | ||
android:scaleType="centerCrop" | ||
android:layout_marginTop="2dp" | ||
app:layout_constraintTop_toBottomOf="@id/gridImageview1" | ||
app:layout_constraintDimensionRatio="H,1:1" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" /> | ||
|
||
<TextView | ||
android:id="@+id/likesCount2" | ||
android:background="@layout/rounded_border_textview" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="8dp" | ||
android:layout_marginEnd="8dp" | ||
android:layout_marginRight="8dp" | ||
android:paddingTop="2dp" | ||
android:paddingBottom="2dp" | ||
android:paddingStart="4dp" | ||
android:paddingEnd="4dp" | ||
android:text="0" | ||
android:textColor="@android:color/white" | ||
android:textSize="14sp" | ||
android:textStyle="normal" | ||
android:textAlignment="center" | ||
android:shadowDx="0" | ||
android:shadowDy="0" | ||
android:shadowRadius="10" | ||
android:shadowColor="#000000" | ||
app:layout_constraintBottom_toBottomOf="@id/gridImageview2" | ||
app:layout_constraintEnd_toEndOf="@id/gridImageview2" | ||
/> | ||
</android.support.constraint.ConstraintLayout> |