package Ġcom . ar ast hel . span ned grid layout manager . sample Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroidx . recyclerview . widget . RecyclerView Ċ import Ġcom . ar ast hel . span ned grid layout manager . Span Size Ċ import Ġcom . ar ast hel . span ned grid layout manager . Sp anned Grid LayoutManager Ċ import Ġcom . ar ast hel . span ned grid layout manager . Sp anned Grid LayoutManager . Orientation .* Ċ Ċ /** Ċ Ġ* ĠCreated Ġby ĠJ orge ĠMart ÃŃn Ġon Ġ 2 4 / 5 / 1 7 . Ċ Ġ*/ Ċ class ĠMainActivity : Ġandroidx . appcompat . app . AppCompatActivity () Ġ{ ĊĊĠĠĠ Ġval Ġre cycler view : Ġandroidx . recyclerview . widget . RecyclerView Ġby Ġlazy Ġ{ ĠfindViewById < android x . recyclerview . widget . RecyclerView >( R . id . re cyclerView ) Ġ} ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate ( savedInstanceState : Ġandroid . os . Bundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . onCreate ( savedInstanceState ) ĊĊĠĠĠĠĠĠĠ ĠsetContentView ( R . layout . activity _ main ) ĊĊĠĠĠĠĠĠĠ Ġval Ġsp anned Grid LayoutManager Ġ= ĠSp anned Grid LayoutManager ( orientation Ġ= ĠV ERT ICAL , Ġspans Ġ= Ġ 4 ) ĊĠĠĠĠĠĠĠ Ġsp anned Grid LayoutManager . item Order Is Stable Ġ= Ġtrue ĊĊĠĠĠĠĠĠĠ Ġre cycler view . layout Manager Ġ= Ġsp anned Grid LayoutManager ĊĊĠĠĠĠĠĠĠ Ġre cycler view . addItem Decoration ( Space Item Decorator ( left Ġ= Ġ 1 0 , Ġtop Ġ= Ġ 1 0 , Ġright Ġ= Ġ 1 0 , Ġbottom Ġ= Ġ 1 0 )) ĊĊĠĠĠĠĠĠĠ Ġval Ġadapter Ġ= ĠGrid Item Adapter () ĊĊĠĠĠĠĠĠĠ Ġif Ġ( savedInstanceState Ġ!= Ġnull Ġ&& ĠsavedInstanceState . containsKey (" clicked ")) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġclicked Ġ= ĠsavedInstanceState . getBoolean Array (" clicked ") !! ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadapter . clicked Items . clear () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadapter . clicked Items . addAll ( clicked . toList ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġsp anned Grid LayoutManager . span Size Lookup Ġ= ĠSp anned Grid LayoutManager . Span Size Lookup Ġ{ Ġposition Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( adapter . clicked Items [ position ]) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠSpan Size ( 2 , Ġ 2 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠSpan Size ( 1 , Ġ 1 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġre cycler view . adapter Ġ= Ġadapter ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Save InstanceState ( out State : ĠBundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on Save InstanceState ( out State ) ĊĊĠĠĠĠĠĠĠ Ġout State ?. put Boolean Array (" clicked ", Ġ( recyclerview . adapter Ġas ĠGrid Item Adapter ). clicked Items . to Boolean Array ()) ĊĊĠĠĠ Ġ} Ċ Ċ }