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 /**
28  * Grid style layout manager for AppGridRecyclerView.
29  */
30 public class AppGridLayoutManager extends GridLayoutManager {
31     boolean mShouldLayoutChildren = true;
32 
AppGridLayoutManager(Context context, int numOfCols, int numOfRows, @PageOrientation int pageOrientation)33     public AppGridLayoutManager(Context context, int numOfCols, int numOfRows,
34             @PageOrientation int pageOrientation) {
35         super(context, isHorizontal(pageOrientation) ? numOfRows : numOfCols,
36                 isHorizontal(pageOrientation)
37                         ? GridLayoutManager.HORIZONTAL : GridLayoutManager.VERTICAL, false);
38     }
39 
40     /**
41      * By default, RecyclerView and GridLayoutManager performs many predictive animations and
42      * predictive scrolling in response to MotionEvents, such as events emitted by DragEvent.
43      *
44      * During drag and drop operations, we don't expect any predictive scrolling or margin
45      * adjustment. By calling #setShouldLayoutChildren(false), we let the animation be exclusively
46      * handled by ViewHolders themselves, rather than the parent ViewGroup.
47      */
setShouldLayoutChildren(boolean shouldLayoutChildren)48     public void setShouldLayoutChildren(boolean shouldLayoutChildren) {
49         mShouldLayoutChildren = shouldLayoutChildren;
50     }
51 
52     @Override
onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state)53     public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
54         if (mShouldLayoutChildren) {
55             super.onLayoutChildren(recycler, state);
56         }
57     }
58 }
59