11package com .d4rk .androidtutorials .java .ui .screens .android ;
22
3+ import android .content .Context ;
34import android .content .Intent ;
45import android .content .res .XmlResourceParser ;
6+ import android .graphics .Rect ;
57import android .net .Uri ;
68import android .os .Bundle ;
9+ import android .util .TypedValue ;
710import android .view .LayoutInflater ;
811import android .view .Menu ;
912import android .view .MenuInflater ;
2629import com .google .android .gms .ads .AdRequest ;
2730import com .google .android .gms .ads .LoadAdError ;
2831import 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
3036import org .xmlpull .v1 .XmlPullParser ;
3137import 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
0 commit comments