1 /*
2  * Copyright (C) 2021 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 package com.android.launcher3.recyclerview;
17 
18 import android.view.ViewGroup;
19 
20 import androidx.annotation.IntDef;
21 import androidx.recyclerview.widget.RecyclerView.ViewHolder;
22 
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 import java.util.List;
26 
27 /**
28  * Creates and populates views with data
29  *
30  * @param <T> A data model which is used to populate the {@link ViewHolder}.
31  * @param <V> A subclass of {@link ViewHolder} which holds references to views.
32  */
33 public interface ViewHolderBinder<T, V extends ViewHolder> {
34 
35     int POSITION_DEFAULT = 0;
36     int POSITION_FIRST = 1 << 0;
37     int POSITION_LAST = 1 << 1;
38 
39     @Retention(RetentionPolicy.SOURCE)
40     @IntDef(value = {POSITION_DEFAULT, POSITION_FIRST, POSITION_LAST}, flag = true)
41     @interface ListPosition {}
42 
43     /**
44      * Creates a new view, and attach it to the parent {@link ViewGroup}. Then, populates UI
45      * references in a {@link ViewHolder}.
46      */
newViewHolder(ViewGroup parent)47     V newViewHolder(ViewGroup parent);
48 
49     /** Populate UI references in {@link ViewHolder} with data. */
bindViewHolder(V viewHolder, T data, @ListPosition int position, List<Object> payloads)50     void bindViewHolder(V viewHolder, T data, @ListPosition int position, List<Object> payloads);
51 
52     /**
53      * Called when the view is recycled. Views are recycled in batches once they are sufficiently
54      * far off screen that it is unlikely the user will scroll back to them soon. Optionally
55      * override this to free expensive resources.
56      */
unbindViewHolder(V viewHolder)57     default void unbindViewHolder(V viewHolder) {}
58 }
59