• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.deskclock;
18 
19 import android.content.Context;
20 import androidx.annotation.Nullable;
21 import androidx.recyclerview.widget.RecyclerView;
22 import android.util.AttributeSet;
23 import android.view.MotionEvent;
24 
25 /**
26  *  Thin wrapper around RecyclerView to prevent simultaneous layout passes, particularly during
27  *  animations.
28  */
29 public class AlarmRecyclerView extends RecyclerView {
30 
31     private boolean mIgnoreRequestLayout;
32 
AlarmRecyclerView(Context context)33     public AlarmRecyclerView(Context context) {
34         this(context, null);
35     }
36 
AlarmRecyclerView(Context context, @Nullable AttributeSet attrs)37     public AlarmRecyclerView(Context context, @Nullable AttributeSet attrs) {
38         this(context, attrs, 0);
39     }
40 
AlarmRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle)41     public AlarmRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
42         super(context, attrs, defStyle);
43         addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
44             @Override
45             public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
46                 // Disable scrolling/user action to prevent choppy animations.
47                 return rv.getItemAnimator().isRunning();
48             }
49         });
50     }
51 
52     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)53     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
54         mIgnoreRequestLayout = true;
55         super.onLayout(changed, left, top, right, bottom);
56         mIgnoreRequestLayout = false;
57     }
58 
59     @Override
requestLayout()60     public void requestLayout() {
61         if (!mIgnoreRequestLayout &&
62                 (getItemAnimator() == null || !getItemAnimator().isRunning())) {
63             super.requestLayout();
64         }
65     }
66 
67 }