• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.wallpaper.picker.customization.ui.view.listener
18 
19 import androidx.recyclerview.widget.RecyclerView
20 import com.android.wallpaper.picker.category.ui.view.adapter.CuratedPhotosAdapter
21 import com.google.android.material.carousel.CarouselLayoutManager
22 
23 /**
24  * This scroll listener implementation finds the positions of the first visible carousel item, and
25  * updates the [CuratedPhotosAdapter] with that position
26  */
27 class WallpaperCarouselScrollListener : RecyclerView.OnScrollListener() {
28     private var lastScrollState = RecyclerView.NO_POSITION
29 
onScrollStateChangednull30     override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
31         super.onScrollStateChanged(recyclerView, newState)
32 
33         if (
34             newState == RecyclerView.SCROLL_STATE_IDLE &&
35                 lastScrollState != RecyclerView.SCROLL_STATE_IDLE
36         ) {
37             updateCarouselTitle(recyclerView)
38         }
39         lastScrollState = newState
40     }
41 
onScrollednull42     override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
43         super.onScrolled(recyclerView, dx, dy)
44         if (lastScrollState == RecyclerView.NO_POSITION) {
45             updateCarouselTitle(recyclerView)
46         }
47     }
48 
updateCarouselTitlenull49     private fun updateCarouselTitle(recyclerView: RecyclerView) {
50         // Get the LayoutManager
51         val layoutManager = recyclerView.layoutManager as CarouselLayoutManager
52 
53         // Find the first completely visible item position
54         var firstVisiblePosition = RecyclerView.NO_POSITION
55 
56         for (i in 0 until layoutManager.childCount) {
57             val child = layoutManager.getChildAt(i)
58             if (child != null) {
59                 val position = recyclerView.getChildAdapterPosition(child)
60                 if (
61                     position != RecyclerView.NO_POSITION &&
62                         layoutManager.isViewPartiallyVisible(child, true, true)
63                 ) {
64                     if (
65                         firstVisiblePosition == RecyclerView.NO_POSITION ||
66                             position < firstVisiblePosition
67                     ) {
68                         firstVisiblePosition = position
69                     }
70                 }
71             }
72         }
73 
74         // Update the adapter with the new visible position
75         if (firstVisiblePosition != RecyclerView.NO_POSITION) {
76             recyclerView.post {
77                 (recyclerView.adapter as CuratedPhotosAdapter).setVisiblePosition(
78                     firstVisiblePosition
79                 )
80             }
81         }
82     }
83 }
84