1313import android .view .MenuItem ;
1414import android .view .View ;
1515import android .view .ViewGroup ;
16- import android .widget .TextView ;
1716
1817import androidx .annotation .NonNull ;
1918import androidx .annotation .Nullable ;
19+ import androidx .appcompat .widget .AppCompatImageView ;
2020import androidx .appcompat .widget .SearchView ;
2121import androidx .core .view .MenuHost ;
2222import androidx .core .view .MenuProvider ;
2929import com .d4rk .androidtutorials .java .R ;
3030import com .d4rk .androidtutorials .java .ads .views .NativeAdBannerView ;
3131import com .d4rk .androidtutorials .java .utils .ConsentUtils ;
32+ import com .d4rk .androidtutorials .java .databinding .FragmentAndroidStudioBinding ;
33+ import com .d4rk .androidtutorials .java .databinding .ItemAndroidStudioCategoryBinding ;
34+ import com .d4rk .androidtutorials .java .databinding .ItemAndroidStudioLessonBinding ;
3235import com .google .android .gms .ads .AdListener ;
3336import com .google .android .gms .ads .LoadAdError ;
3437import com .google .android .gms .ads .MobileAds ;
3538import com .google .android .material .card .MaterialCardView ;
39+ import com .google .android .material .textview .MaterialTextView ;
3640import com .google .android .material .shape .CornerFamily ;
3741import com .google .android .material .shape .ShapeAppearanceModel ;
3842
@@ -54,12 +58,14 @@ public class AndroidStudioFragment extends Fragment {
5458 private final List <Object > allItems = new ArrayList <>();
5559 private LessonsAdapter adapter ;
5660 private boolean showAds ;
61+ private FragmentAndroidStudioBinding binding ;
5762
5863 @ Nullable
5964 @ Override
6065 public View onCreateView (@ NonNull LayoutInflater inflater , @ Nullable ViewGroup container ,
6166 @ Nullable Bundle savedInstanceState ) {
62- return inflater .inflate (R .layout .fragment_android_studio , container , false );
67+ binding = FragmentAndroidStudioBinding .inflate (inflater , container , false );
68+ return binding .getRoot ();
6369 }
6470
6571 @ Override
@@ -69,7 +75,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
6975 if (showAds ) {
7076 ensureMobileAdsInitialized ();
7177 }
72- RecyclerView list = view . findViewById ( R . id . lessons_list ) ;
78+ RecyclerView list = binding . lessonsList ;
7379 list .setLayoutManager (new LinearLayoutManager (requireContext ()));
7480 adapter = new LessonsAdapter ();
7581 list .setAdapter (adapter );
@@ -124,6 +130,12 @@ public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
124130 }, getViewLifecycleOwner (), Lifecycle .State .RESUMED );
125131 }
126132
133+ @ Override
134+ public void onDestroyView () {
135+ super .onDestroyView ();
136+ binding = null ;
137+ }
138+
127139 private void ensureMobileAdsInitialized () {
128140 if (!mobileAdsInitialized ) {
129141 MobileAds .initialize (requireContext ());
@@ -350,13 +362,13 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
350362 adView .setNativeAdLayout (R .layout .ad_android_studio_list );
351363 return new AdHolder (adView );
352364 } else if (viewType == TYPE_CATEGORY ) {
353- View view = LayoutInflater . from ( parent . getContext ())
354- . inflate ( R . layout . item_android_studio_category , parent , false );
355- return new CategoryHolder (view );
365+ ItemAndroidStudioCategoryBinding binding = ItemAndroidStudioCategoryBinding . inflate (
366+ LayoutInflater . from ( parent . getContext ()) , parent , false );
367+ return new CategoryHolder (binding );
356368 } else {
357- View view = LayoutInflater . from ( parent . getContext ())
358- . inflate ( R . layout . item_android_studio_lesson , parent , false );
359- return new LessonHolder (view );
369+ ItemAndroidStudioLessonBinding binding = ItemAndroidStudioLessonBinding . inflate (
370+ LayoutInflater . from ( parent . getContext ()) , parent , false );
371+ return new LessonHolder (binding );
360372 }
361373 }
362374
@@ -400,16 +412,16 @@ static class AdHolder extends RecyclerView.ViewHolder {
400412
401413 static class LessonHolder extends RecyclerView .ViewHolder {
402414 final MaterialCardView card ;
403- final ImageView icon ;
404- final TextView title ;
405- final TextView summary ;
406-
407- LessonHolder (@ NonNull View itemView ) {
408- super (itemView );
409- card = ( MaterialCardView ) itemView ;
410- icon = itemView . findViewById ( R . id . lesson_icon ) ;
411- title = itemView . findViewById ( R . id . lesson_title ) ;
412- summary = itemView . findViewById ( R . id . lesson_summary ) ;
415+ final AppCompatImageView icon ;
416+ final MaterialTextView title ;
417+ final MaterialTextView summary ;
418+
419+ LessonHolder (@ NonNull ItemAndroidStudioLessonBinding binding ) {
420+ super (binding . getRoot () );
421+ card = binding . lessonCard ;
422+ icon = binding . lessonIcon ;
423+ title = binding . lessonTitle ;
424+ summary = binding . lessonSummary ;
413425 }
414426
415427 void bind (Lesson lesson , boolean first , boolean last ) {
@@ -449,11 +461,11 @@ private void applyCorners(boolean first, boolean last) {
449461 }
450462
451463 static class CategoryHolder extends RecyclerView .ViewHolder {
452- final TextView title ;
464+ final MaterialTextView title ;
453465
454- CategoryHolder (@ NonNull View itemView ) {
455- super (itemView );
456- title = itemView . findViewById ( R . id . category_title ) ;
466+ CategoryHolder (@ NonNull ItemAndroidStudioCategoryBinding binding ) {
467+ super (binding . getRoot () );
468+ title = binding . categoryTitle ;
457469 }
458470
459471 void bind (Category category ) {
0 commit comments