Skip to content

Commit 591d14a

Browse files
committed
Show the User Images
1 parent 2ab2d0e commit 591d14a

7 files changed

+344
-0
lines changed

app/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ dependencies {
2727
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
2828
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
2929
compile 'com.google.code.gson:gson:2.8.2'
30+
compile 'com.android.support:recyclerview-v7:26.1.+'
31+
implementation 'com.squareup.picasso:picasso:2.71828'
3032
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
}

app/src/main/java/dragosholban/com/bestphotos/ImagesActivity.java

+20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package dragosholban.com.bestphotos;
22

3+
import android.content.Intent;
4+
import android.net.Uri;
35
import android.support.v7.app.AppCompatActivity;
46
import android.os.Bundle;
7+
import android.support.v7.widget.RecyclerView;
58
import android.util.Log;
9+
import android.view.View;
610

711
import com.facebook.AccessToken;
812
import com.facebook.GraphRequest;
@@ -21,12 +25,16 @@ public class ImagesActivity extends AppCompatActivity {
2125
private static final String TAG = ImagesActivity.class.getName();
2226

2327
private ArrayList<FacebookImage> images = new ArrayList<>();
28+
private RecyclerView recyclerView;
2429

2530
@Override
2631
protected void onCreate(Bundle savedInstanceState) {
2732
super.onCreate(savedInstanceState);
2833
setContentView(R.layout.activity_images);
2934

35+
recyclerView = this.findViewById(R.id.recyclerView);
36+
final ImagesActivity activity = this;
37+
3038
GraphRequest.Callback callback = new GraphRequest.Callback() {
3139

3240
@Override
@@ -81,11 +89,23 @@ public void onCompleted(GraphResponse response) {
8189
if (fbPhotos.paging != null && fbPhotos.paging.cursors.after != null) {
8290
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/photos?fields=picture,reactions.limit(1).summary(true),link,images,created_time&type=uploaded&limit=500&after=" + fbPhotos.paging.cursors.after, null, HttpMethod.GET, this);
8391
request.executeAsync();
92+
} else {
93+
recyclerView.setAdapter(new ImageRecyclerViewAdapter(activity, images));
8494
}
8595
}
8696
};
8797

8898
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), "me/photos?fields=picture,reactions.limit(1).summary(true),link,images,created_time&type=uploaded&limit=500", null, HttpMethod.GET, callback);
8999
request.executeAsync();
90100
}
101+
102+
public void onImageClick(View view) {
103+
String link = (String) view.getTag();
104+
if (link != null) {
105+
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
106+
if (intent.resolveActivity(getPackageManager()) != null) {
107+
startActivity(intent);
108+
}
109+
}
110+
}
91111
}

app/src/main/res/layout/activity_images.xml

+7
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@
66
android:layout_height="match_parent"
77
tools:context="dragosholban.com.bestphotos.ImagesActivity">
88

9+
<android.support.v7.widget.RecyclerView
10+
android:id="@+id/recyclerView"
11+
android:scrollbars="vertical"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
android:drawSelectorOnTop="true" />
15+
916
</android.support.constraint.ConstraintLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_height="wrap_content"
4+
android:layout_width="wrap_content">
5+
<item>
6+
<shape android:shape="rectangle">
7+
<solid android:color="#3b5998"/>
8+
<corners android:radius="20dp" />
9+
<padding
10+
android:top="2dp"
11+
android:bottom="2dp"
12+
android:left="4dp"
13+
android:right="4dp" />
14+
</shape>
15+
</item>
16+
</selector>
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
android:orientation="vertical"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
app:layout_constraintDimensionRatio="H,1:1"
7+
android:gravity="center"
8+
android:layout_margin="1dp"
9+
>
10+
11+
<ImageView
12+
android:id="@+id/gridImageview"
13+
android:layout_width="0dp"
14+
android:layout_height="0dp"
15+
android:onClick="onImageClick"
16+
android:scaleType="centerCrop"
17+
app:layout_constraintDimensionRatio="H,1:1"
18+
app:layout_constraintEnd_toEndOf="parent"
19+
app:layout_constraintStart_toStartOf="parent"
20+
app:layout_constraintTop_toTopOf="parent" />
21+
22+
<TextView
23+
android:id="@+id/likesCount"
24+
android:background="@layout/rounded_border_textview"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_marginBottom="8dp"
28+
android:layout_marginEnd="8dp"
29+
android:layout_marginRight="8dp"
30+
android:paddingTop="2dp"
31+
android:paddingBottom="2dp"
32+
android:paddingStart="4dp"
33+
android:paddingEnd="4dp"
34+
android:text="0"
35+
android:textColor="@android:color/white"
36+
android:textSize="14sp"
37+
android:textStyle="normal"
38+
android:textAlignment="center"
39+
android:shadowDx="0"
40+
android:shadowDy="0"
41+
android:shadowRadius="10"
42+
android:shadowColor="#000000"
43+
app:layout_constraintBottom_toBottomOf="@id/gridImageview"
44+
app:layout_constraintEnd_toEndOf="@id/gridImageview"
45+
/>
46+
</android.support.constraint.ConstraintLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
android:orientation="vertical"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:gravity="center"
7+
android:layout_margin="1dp"
8+
>
9+
10+
<ImageView
11+
android:id="@+id/gridImageview1"
12+
android:layout_width="0dp"
13+
android:layout_height="0dp"
14+
android:onClick="onImageClick"
15+
android:scaleType="centerCrop"
16+
app:layout_constraintDimensionRatio="H,1:1"
17+
app:layout_constraintEnd_toEndOf="parent"
18+
app:layout_constraintStart_toStartOf="parent"
19+
app:layout_constraintTop_toTopOf="parent" />
20+
21+
<TextView
22+
android:id="@+id/likesCount1"
23+
android:background="@layout/rounded_border_textview"
24+
android:layout_width="wrap_content"
25+
android:layout_height="wrap_content"
26+
android:layout_marginBottom="8dp"
27+
android:layout_marginEnd="8dp"
28+
android:layout_marginRight="8dp"
29+
android:paddingTop="2dp"
30+
android:paddingBottom="2dp"
31+
android:paddingStart="4dp"
32+
android:paddingEnd="4dp"
33+
android:text="0"
34+
android:textColor="@android:color/white"
35+
android:textSize="14sp"
36+
android:textStyle="normal"
37+
android:textAlignment="center"
38+
android:shadowDx="0"
39+
android:shadowDy="0"
40+
android:shadowRadius="10"
41+
android:shadowColor="#000000"
42+
app:layout_constraintBottom_toBottomOf="@id/gridImageview1"
43+
app:layout_constraintEnd_toEndOf="@id/gridImageview1"
44+
/>
45+
46+
<ImageView
47+
android:id="@+id/gridImageview2"
48+
android:layout_width="0dp"
49+
android:layout_height="0dp"
50+
android:onClick="onImageClick"
51+
android:scaleType="centerCrop"
52+
android:layout_marginTop="2dp"
53+
app:layout_constraintTop_toBottomOf="@id/gridImageview1"
54+
app:layout_constraintDimensionRatio="H,1:1"
55+
app:layout_constraintEnd_toEndOf="parent"
56+
app:layout_constraintStart_toStartOf="parent" />
57+
58+
<TextView
59+
android:id="@+id/likesCount2"
60+
android:background="@layout/rounded_border_textview"
61+
android:layout_width="wrap_content"
62+
android:layout_height="wrap_content"
63+
android:layout_marginBottom="8dp"
64+
android:layout_marginEnd="8dp"
65+
android:layout_marginRight="8dp"
66+
android:paddingTop="2dp"
67+
android:paddingBottom="2dp"
68+
android:paddingStart="4dp"
69+
android:paddingEnd="4dp"
70+
android:text="0"
71+
android:textColor="@android:color/white"
72+
android:textSize="14sp"
73+
android:textStyle="normal"
74+
android:textAlignment="center"
75+
android:shadowDx="0"
76+
android:shadowDy="0"
77+
android:shadowRadius="10"
78+
android:shadowColor="#000000"
79+
app:layout_constraintBottom_toBottomOf="@id/gridImageview2"
80+
app:layout_constraintEnd_toEndOf="@id/gridImageview2"
81+
/>
82+
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)