Skip to content

Commit 5273ef2

Browse files
Tweak native ad layout and list spacing
1 parent 0c9d355 commit 5273ef2

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/android/AndroidStudioFragment.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.d4rk.androidtutorials.java.ui.screens.android;
22

3+
import android.content.Context;
34
import android.content.Intent;
45
import android.content.res.XmlResourceParser;
6+
import android.graphics.Rect;
57
import android.net.Uri;
68
import android.os.Bundle;
9+
import android.util.TypedValue;
710
import android.view.LayoutInflater;
811
import android.view.Menu;
912
import android.view.MenuInflater;
@@ -26,6 +29,9 @@
2629
import com.google.android.gms.ads.AdRequest;
2730
import com.google.android.gms.ads.LoadAdError;
2831
import com.google.android.gms.ads.MobileAds;
32+
import com.google.android.material.card.MaterialCardView;
33+
import com.google.android.material.shape.CornerFamily;
34+
import com.google.android.material.shape.ShapeAppearanceModel;
2935

3036
import org.xmlpull.v1.XmlPullParser;
3137
import org.xmlpull.v1.XmlPullParserException;
@@ -60,6 +66,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
6066
list.setLayoutManager(new LinearLayoutManager(requireContext()));
6167
adapter = new LessonsAdapter();
6268
list.setAdapter(adapter);
69+
list.addItemDecoration(new LessonAdSpacingDecoration(requireContext()));
6370
allItems.clear();
6471
allItems.addAll(loadItems());
6572
populateAdapter(allItems);
@@ -235,10 +242,32 @@ private static class Category {
235242
int iconRes;
236243
}
237244

245+
private static class LessonAdSpacingDecoration extends RecyclerView.ItemDecoration {
246+
private final int spacing;
247+
248+
LessonAdSpacingDecoration(@NonNull Context context) {
249+
spacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2,
250+
context.getResources().getDisplayMetrics());
251+
}
252+
253+
@Override
254+
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
255+
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
256+
RecyclerView.Adapter<?> adapter = parent.getAdapter();
257+
if (!(adapter instanceof LessonsAdapter)) return;
258+
int position = parent.getChildAdapterPosition(view);
259+
if (position == RecyclerView.NO_POSITION) return;
260+
int type = ((LessonsAdapter) adapter).getItemViewType(position);
261+
if (type == LessonsAdapter.TYPE_LESSON || type == LessonsAdapter.TYPE_AD) {
262+
outRect.bottom = spacing;
263+
}
264+
}
265+
}
266+
238267
private static class LessonsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
239-
private static final int TYPE_LESSON = 0;
240-
private static final int TYPE_AD = 1;
241-
private static final int TYPE_CATEGORY = 2;
268+
static final int TYPE_LESSON = 0;
269+
static final int TYPE_AD = 1;
270+
static final int TYPE_CATEGORY = 2;
242271
private final List<Object> items = new ArrayList<>();
243272

244273
void setItems(List<Object> newItems) {
@@ -292,7 +321,9 @@ public void onAdFailedToLoad(@NonNull LoadAdError error) {
292321
((CategoryHolder) holder).bind(category);
293322
} else {
294323
Lesson lesson = (Lesson) items.get(position);
295-
((LessonHolder) holder).bind(lesson);
324+
boolean first = position > 0 && getItemViewType(position - 1) == TYPE_CATEGORY;
325+
boolean last = position == getItemCount() - 1 || getItemViewType(position + 1) == TYPE_CATEGORY;
326+
((LessonHolder) holder).bind(lesson, first, last);
296327
}
297328
}
298329

@@ -310,18 +341,20 @@ static class AdHolder extends RecyclerView.ViewHolder {
310341
}
311342

312343
static class LessonHolder extends RecyclerView.ViewHolder {
344+
final MaterialCardView card;
313345
final ImageView icon;
314346
final TextView title;
315347
final TextView summary;
316348

317349
LessonHolder(@NonNull View itemView) {
318350
super(itemView);
351+
card = (MaterialCardView) itemView;
319352
icon = itemView.findViewById(R.id.lesson_icon);
320353
title = itemView.findViewById(R.id.lesson_title);
321354
summary = itemView.findViewById(R.id.lesson_summary);
322355
}
323356

324-
void bind(Lesson lesson) {
357+
void bind(Lesson lesson, boolean first, boolean last) {
325358
if (lesson.iconRes != 0) {
326359
icon.setImageResource(lesson.iconRes);
327360
icon.setVisibility(View.VISIBLE);
@@ -340,6 +373,20 @@ void bind(Lesson lesson) {
340373
v.getContext().startActivity(lesson.intent);
341374
}
342375
});
376+
applyCorners(first, last);
377+
}
378+
379+
private void applyCorners(boolean first, boolean last) {
380+
float dp4 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,
381+
itemView.getResources().getDisplayMetrics());
382+
float dp24 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24,
383+
itemView.getResources().getDisplayMetrics());
384+
ShapeAppearanceModel.Builder builder = card.getShapeAppearanceModel().toBuilder()
385+
.setTopLeftCorner(CornerFamily.ROUNDED, first ? dp24 : dp4)
386+
.setTopRightCorner(CornerFamily.ROUNDED, first ? dp24 : dp4)
387+
.setBottomLeftCorner(CornerFamily.ROUNDED, last ? dp24 : dp4)
388+
.setBottomRightCorner(CornerFamily.ROUNDED, last ? dp24 : dp4);
389+
card.setShapeAppearanceModel(builder.build());
343390
}
344391
}
345392

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
style="@style/Widget.Material3.CardView.Outlined"
66
android:layout_width="match_parent"
77
android:layout_height="wrap_content"
8-
android:layout_marginHorizontal="16dp">
8+
android:layout_marginHorizontal="16dp"
9+
app:cardCornerRadius="4dp">
910

1011
<LinearLayout
1112
android:layout_width="match_parent"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
android:layout_width="match_parent"
1111
android:layout_height="wrap_content"
1212
android:layout_marginHorizontal="16dp"
13-
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.CardViewBottomRounded">
13+
app:cardCornerRadius="4dp">
1414

1515
<LinearLayout
1616
android:layout_width="match_parent"
@@ -27,8 +27,8 @@
2727
android:gravity="center_vertical">
2828

2929
<FrameLayout
30-
android:layout_width="60dp"
31-
android:layout_height="60dp"
30+
android:layout_width="120dp"
31+
android:layout_height="120dp"
3232
android:layout_marginEnd="16dp">
3333

3434
<com.google.android.gms.ads.nativead.MediaView

0 commit comments

Comments
 (0)