1 /*
2  * Copyright (C) 2023 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.car.carlauncher.recyclerview;
18 
19 import static com.android.car.carlauncher.AppGridConstants.PageOrientation;
20 import static com.android.car.carlauncher.AppGridConstants.isHorizontal;
21 
22 import android.content.Context;
23 
24 import androidx.recyclerview.widget.GridLayoutManager;
25 import androidx.recyclerview.widget.RecyclerView;
26 
27 import com.android.car.carlauncher.AppGridRecyclerView;
28 import com.android.car.carlauncher.pagination.PageMeasurementHelper;
29 
30 /**
31  * Grid style layout manager for {@link AppGridRecyclerView}
32  */
33 public class AppGridLayoutManager extends GridLayoutManager {
34     boolean mShouldLayoutChildren = true;
35 
36     private static final int DEFAULT_SPAN_COUNT = 3;
37 
38     /**
39      * Initializes the layoutManager.
40      * Note: The spanCount is updated when dimensions are available,
41      * check:{@link
42      * AppGridRecyclerView#onDimensionsUpdated(PageMeasurementHelper.PageDimensions,
43      * PageMeasurementHelper.GridDimensions)}
44      */
AppGridLayoutManager(Context context, @PageOrientation int pageOrientation)45     public AppGridLayoutManager(Context context,
46             @PageOrientation int pageOrientation) {
47         super(context, DEFAULT_SPAN_COUNT,
48                 isHorizontal(pageOrientation)
49                         ? GridLayoutManager.HORIZONTAL : GridLayoutManager.VERTICAL, false);
50     }
51 
52     /**
53      * By default, RecyclerView and GridLayoutManager performs many predictive animations and
54      * predictive scrolling in response to MotionEvents, such as events emitted by DragEvent.
55      *
56      * During drag and drop operations, we don't expect any predictive scrolling or margin
57      * adjustment. By calling #setShouldLayoutChildren(false), we let the animation be exclusively
58      * handled by ViewHolders themselves, rather than the parent ViewGroup.
59      */
setShouldLayoutChildren(boolean shouldLayoutChildren)60     public void setShouldLayoutChildren(boolean shouldLayoutChildren) {
61         mShouldLayoutChildren = shouldLayoutChildren;
62     }
63 
64     @Override
onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)65     public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
66         if (mShouldLayoutChildren) {
67             super.onLayoutChildren(recycler, state);
68         }
69     }
70 }
71