• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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.pump.ui;
18 
19 import android.util.SparseBooleanArray;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.UiThread;
23 import androidx.recyclerview.widget.RecyclerView.RecycledViewPool;
24 import androidx.recyclerview.widget.RecyclerView.ViewHolder;
25 
26 import com.android.pump.R;
27 
28 @UiThread
29 public class CustomRecycledViewPool extends RecycledViewPool {
30     private static final int MAX_VIEWS = 17;
31 
32     private final SparseBooleanArray mInitialized = new SparseBooleanArray();
33 
CustomRecycledViewPool()34     public CustomRecycledViewPool() {
35         setMaxRecycledViews(R.layout.header, 1);
36         setMaxRecycledViews(R.layout.movie, MAX_VIEWS);
37     }
38 
39     @Override
setMaxRecycledViews(int viewType, int maxViews)40     public void setMaxRecycledViews(int viewType, int maxViews) {
41         super.setMaxRecycledViews(viewType, maxViews);
42         mInitialized.put(viewType, true);
43     }
44 
45     @Override
putRecycledView(@onNull ViewHolder scrap)46     public void putRecycledView(@NonNull ViewHolder scrap) {
47         int viewType = scrap.getItemViewType();
48         if (!mInitialized.get(viewType)) {
49             setMaxRecycledViews(viewType, MAX_VIEWS);
50             throw new IllegalArgumentException("Unknown view type"); // TODO tuning only -- remove this
51         }
52         super.putRecycledView(scrap);
53     }
54 }
55