package Ġxyz . g ill all . demo app . ui . pix ab ay . image gallery Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroidx . databinding . Data Binding Util Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . navigation . Nav Controller Ċ import Ġandroidx . navigation . fragment . Nav Host Fragment . find Nav Controller Ċ import Ġandroidx . recyclerview . widget . Grid LayoutManager Ċ import Ġorg . k oin . android x . view model . ext . android . get Shared ViewModel Ċ import Ġorg . k oin . android x . view model . ext . android . get ViewModel Ċ import Ġxyz . g ill all . demo app . R Ċ import Ġxyz . g ill all . demo app . databinding . Fragment Image Gallery Binding Ċ Ċ import Ġxyz . g ill all . demo app . shared . Shared ViewModel Ċ import Ġxyz . g ill all . demo app . ui . pix ab ay . ImageType Ċ Ċ class ĠImage Gallery Fragment Ġ: ĠFragment () Ġ{ ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġnav Controller : ĠNav Controller ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġshared ViewModel : ĠShared ViewModel ĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠviewModel : ĠImage Gallery ViewModel ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġhits Adapter : ĠImage Gallery Adapter ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate View ( ĊĠĠĠĠĠĠĠ Ġinflater : ĠLayoutInflater , Ġcontainer : ĠViewGroup ?, ĊĠĠĠĠĠĠĠ ĠsavedInstanceState : ĠBundle ? ĊĠĠĠ Ġ): ĠView Ġ{ ĊĠĠĠĠĠĠĠ Ġshared ViewModel Ġ= Ġget Shared ViewModel () ĊĠĠĠĠĠĠĠ Ġshared ViewModel . shared Call (" Called Ġfrom Ġfragment ") ĊĠĠĠĠĠĠĠ ĠviewModel Ġ= Ġget ViewModel () ĊĠĠĠĠĠĠĠ Ġval Ġbinding : ĠFragment Image Gallery Binding Ġ= ĠData Binding Util . inflate ( in flater , ĠR . layout . fragment _ image _ gallery , Ġcontainer , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġbinding . lifecycle Owner Ġ= Ġthis ĊĠĠĠĠĠĠĠ Ġbinding . viewModel Ġ= ĠviewModel ĊĠĠĠĠĠĠĠ Ġnav Controller Ġ= Ġfind Nav Controller ( this ) ĊĠĠĠĠĠĠĠ Ġsetup Hits ( binding ) ĊĠĠĠĠĠĠĠ Ġreturn Ġbinding . root ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġsetup Hits ( binding : ĠFragment Image Gallery Binding ) Ġ{ ĊĠĠĠĠĠĠĠ Ġhits Adapter Ġ= ĠImage Gallery Adapter ( Hit ClickListener () Ġ{ Ġhit , Ġview Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġbundle Ġ= ĠBundle () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġbundle . put Parcelable (" hit ", Ġhit ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnav Controller . navigate ( R . id . action _ pix ab ay Fragment _ to _ image Viewer , Ġbundle ) ĊĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠĠĠĠĠ Ġbinding . recycle Pix ab ay . adapter Ġ= Ġhits Adapter ĊĠĠĠĠĠĠĠ Ġbinding . recycle Pix ab ay . layout Manager Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠGrid LayoutManager ( activity , Ġ 2 , ĠGrid LayoutManager . VERTICAL , Ġfalse ) ĊĠĠĠĠĠĠĠ ĠviewModel . image hits . observe ( view Lifecycle Owner , Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġhits Adapter . submit List ( it . hits ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġbinding . recycle Pix ab ay . schedule Layout Animation () ĊĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠĠĠĠĠ ĠviewModel . get Images (" random ", ĠImage Type . PH OTO . type , Ġ" 1 0 ") ĊĠĠĠĠĠĠĠ Ġ// viewModel . get Videos (" flower ", ĠVideo Type . FIL M . type , Ġ" 1 0 ") ĊĠĠĠ Ġ} Ċ }